Hi over there,

is it possible to execute several SQL Statements with one(!) command?

For example something like that:

Statement s = con.createStatement();
sql=
"CREATE DATABASE MeineDb+
SHOW databases+
USE MeineDb+
CREATE TABLE MeineTestTabelle ( i INT, c CHAR(3), s VARCHAR(8), dt DATE )+
CREATE TABLE MeineAdressen ( Name VARCHAR(16), Plz INT, Ort VARCHAR(16) )+
SHOW tables+
INSERT INTO MeineTestTabelle VALUES ( 11, 'ABC', 'Blubb', '2000-01-01' )+
INSERT INTO MeineTestTabelle VALUES ( 12, 'ab', 'Blabla', '2000-12-31' )+
INSERT INTO MeineTestTabelle VALUES ( 42, 'xy', 'äöüߧ€', '2005-06-06' )+
SELECT * FROM MeineTestTabelle+
INSERT INTO MeineAdressen VALUES ( 'Achim',     52078, 'Aachen' )+
INSERT INTO MeineAdressen VALUES ( 'Alexander', 52134, 'Herzogenrath' )+
INSERT INTO MeineAdressen VALUES ( 'Helmut',    52066, 'Aachen' )+
INSERT INTO MeineAdressen VALUES ( 'Josef',     52070, 'Aachen' )+
INSERT INTO MeineAdressen VALUES ( 'Manfred',   52146, 'Würselen' )+
INSERT INTO MeineAdressen VALUES ( 'Patrick',   52074, 'Aachen' )+
INSERT INTO MeineAdressen VALUES ( 'Roland',    52134, 'Herzogenrath' )+
INSERT INTO MeineAdressen VALUES ( 'Torsten',   52072, 'Aachen' )+
INSERT INTO MeineAdressen VALUES ( 'Werner',    52066, 'Aachen' )";

s.execute(sql);

In words:
i want to create a database, some tables and some tuples with one command. a kind of installer, if you want.

i tried the above shown code, and some modifications, but it doesn't help..
any chance?


TY

Recommended Answers

All 3 Replies

No, a statement is a single SQL statement. You can batch them into transactions and commit them as a unit if you wish, but each statement still needs to be executed individually. You could write a stored procedure and call it with a CallableStatement if you wanted to go that route.

not the answer i wanted to hear ( :D ), but thanks a lot !

//edit:
the batch-thing looks good. ty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.