vorlesung-aufgaben

Werbung
Aufgabe zu JDBC
Folgendes Java-Programm erstellt die unten abgebildete Tabelle Employee:
Die Tabelle EMPLOYEE
+----------+-------+---------+-----------+------------+--------------------------+------+----------+-----------+------+
| FNAME
| MINIT | LNAME
| SSN
| BDATE
| ADDRESS
| SEX | SALARY
| SUPERSSN | DNO |
+----------+-------+---------+-----------+------------+--------------------------+------+----------+-----------+------+
| John
| B
| Smith
| 123456789 | 1965-01-09 | 731 Fondren, Houston, TX | M
| 30000.00 | 333445555 |
5 |
| Franklin | T
| Wong
| 333445555 | 1955-12-08 | 638 Voss, Houston, TX
| M
| 40000.00 | 888665555 |
5 |
| Joyce
| A
| English | 453453453 | 1972-07-31 | 5631 Rice, Houston, TX
| F
| 25000.00 | 333445555 |
5 |
| Ramesh
| K
| Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble, TX | M
| 38000.00 | 333445555 |
5 |
| James
| E
| Borg
| 888665555 | 1937-11-10 | 450 Stone, Houston, TX
| M
| 55000.00 |
NULL |
1 |
| Jennifer | S
| Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire, TX | F
| 43000.00 | 888665555 |
4 |
| Ahmad
| V
| Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston, TX | M
| 25000.00 | 987654321 |
4 |
| Alicia
| J
| Zelaya | 999887777 | 1968-07-19 | 3321 Castle, Spring, TX | F
| 25000.00 | 987654321 |
4 |
+----------+-------+---------+-----------+------------+--------------------------+------+----------+-----------+------+
Programm:
import
import
import
import
java.sql.Connection;
java.sql.SQLException;
com.mysql.jdbc.Statement;
de.atbits.database.ConnectionHelper;
public class JDBCCreateTable {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Driver class not found");
e.printStackTrace();
}
Connection con = null;
try {
con = ConnectionHelper.getConnection(ConnectionHelper.MYSQL);
} catch (SQLException e1) {
System.err.println("Error establishing database connection");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Statement stmt = null;
try {
stmt = (Statement) con.createStatement();
} catch (SQLException e2) {
System.err.println("Error creating SQL-Statement");
}
String createTab = new String("CREATE TABLE EMPLOYEE("
+ "FNAME VARCHAR(10) NOT NULL," + "MINIT VARCHAR(1),"
+ "LNAME VARCHAR(10) NOT NULL," + "SSN INTEGER(9) NOT NULL,"
+ "BDATE DATE," + "ADDRESS VARCHAR(30),"
+ "SEX ENUM('M','F'),"
+ "SALARY REAL(7,2) UNSIGNED," + "SUPERSSN INTEGER(9),"
+ "DNO INTEGER(1));");
try {
System.out.println("result=" + stmt.executeUpdate(createTab));
} catch (SQLException e3) {
System.err.println("Error creating table EMPLOYEE");
}
1.) Das folgende Programm würde in diese Tabelle einige Datensätze einfügen. Jeder der
Einfügevorgänge führt im Rahmen einer separaten Datenbankkommunikation sequentiell genau einen
Einfügevorgang durch, was durch den Rückgabewert 1 dokumentiert wird.
Zwar ist dieses Verfahren praktikabel und erzielt die angestrebten Resultate, jedoch ist es unter
Zeiteffizienzgesichtspunkten inadäquat, da sich Einfüge- und Kommunikationsvorgänge zahlenmäßig
entsprechen.
Aus diesem Grunde bietet die Schnittstelle Statement die Möglichkeit zur Bündelung einzelner SQLAufrufe in einem sog. Batch an. – Formulieren Sie also das Programm so um, dass der Batchmode
verwendet wird.
Programm:
import
import
import
import
java.sql.Connection;
java.sql.SQLException;
java.sql.Statement;
de.atbits.database.ConnectionHelper;
public class JDBCInsert1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Driver class not found");
e.printStackTrace();
}
Connection con = null;
try {
con = ConnectionHelper.getConnection(ConnectionHelper.MYSQL);
} catch (SQLException e1) {
System.err.println("Error establishing database connection");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Statement stmt = null;
try {
stmt = (Statement) con.createStatement();
} catch (SQLException e2) {
System.err.println("Error creating SQL-Statement");
}
try {
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('John', 'B', 'Smith', 123456789, '1965-01-09', '731 Fondren, Houston, TX', 'M', 30000,
333445555, 5);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Franklin', 'T', 'Wong', 333445555, '1955-12-08', '638 Voss, Houston, TX', 'M', 40000,
888665555, 5);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Alicia', 'J', 'Zelaya', 999887777, '1968-07-19', '3321 Castle, Spring, TX', 'F',
25000, 987654321, 4);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Jennifer', 'S', 'Wallace', 987654321, '1941-06-20', '291 Berry, Bellaire, TX', 'F',
43000, 888665555, 4);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Ramesh', 'K', 'Narayan', 666884444, '1962-09-15', '975 Fire Oak, Humble, TX', 'M',
38000, 333445555, 5);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Joyce', 'A', 'English', 453453453, '1972-07-31', '5631 Rice, Houston, TX', 'F', 25000,
333445555, 5);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('Ahmad', 'V', 'Jabbar', 987987987, '1969-03-29', '980 Dallas, Houston, TX', 'M', 25000,
987654321, 4);"));
System.out.println("result="+ stmt.executeUpdate("INSERT INTO EMPLOYEE
VALUES('James', 'E', 'Borg', 888665555, '1937-11-10', '450 Stone, Houston, TX', 'M', 55000,
null, 1);"));
} catch (SQLException e3) {
System.err.println("Error inserting values into table EMPLOYEE");
}
}
}
2.) Das nachfolgende Programm ergänzt zunächst mittels ALTER TABLE eine neue Tabellenspalte
zur Aufnahme des Wochentages der Geburt und anschließend werden durch SQL UPDATEAnweisungen die benötigten Daten aus dem vorhandenen Geburtsdatum ermittelt. Auch dieses
Beispiel bedient sich zur Performancebeschleunigung der Möglichkeiten des Batchaufrufes.
Die nähere Betrachtung des Quellcodes zeigt, dass diese Batchaufrufe im Kern denselben Vorgang
ausführen, nur jeweils mit variierenden Parametern.
Zur Behandlung von Fällen dieser Problemstellung definiert die JDBC-API die Schnittstelle
PreparedStatement als Spezialisierung von Statement. Diese Schnittstelle gestattet es, Anweisungen,
die später an die Datenbank übermittelt werden sollen, mit Platzhaltern zu versehen und diese vor der
Übermittlung mit Werten zu befüllen.
Modifizieren Sie das Beispiel entsprechend, so dass ein PreparedStatement verwendet wird.
import
import
import
import
java.sql.Connection;
java.sql.SQLException;
java.sql.Statement;
de.atbits.database.ConnectionHelper;
public class JDBCUpdate1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Driver class not found");
e.printStackTrace();
}
Connection con = null;
try {
con = ConnectionHelper.getConnection(ConnectionHelper.MYSQL);
} catch (SQLException e1) {
System.err.println("Error establishing database connection");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Statement stmt = null;
try {
stmt = (Statement) con.createStatement();
} catch (SQLException e2) {
System.err.println("Error creating SQL-Statement");
}
try {
stmt.addBatch("ALTER TABLE EMPLOYEE ADD BDAY VARCHAR(10);");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Sunday' WHERE
DAYOFWEEK(BDATE)=1;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Monday' WHERE
DAYOFWEEK(BDATE)=2;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Tuesday' WHERE
DAYOFWEEK(BDATE)=3;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Wednesday' WHERE
DAYOFWEEK(BDATE)=4;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Thursday' WHERE
DAYOFWEEK(BDATE)=5;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Friday' WHERE
DAYOFWEEK(BDATE)=6;");
stmt.addBatch("UPDATE EMPLOYEE SET BDAY='Saturday' WHERE
DAYOFWEEK(BDATE)=7;");
}
}
int[] result = stmt.executeBatch();
for (int i = 0; i < result.length; i++) {
System.out.println("Statement No " + i + " changed "
+ result[i] + " rows");
}
} catch (SQLException e3) {
System.err.println("Error inserting values into table EMPLOYEE");
}
Herunterladen