ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/DBInterface/DBMultipleInsertTest.java
Revision: 1.1
Committed: Mon Dec 4 09:54:03 2000 UTC (23 years, 5 months ago) by pjm2
Branch: MAIN
Log Message:
A class to test inserting multiple data into a mySQL table using a single
transaction.

File Contents

# Content
1 import java.sql.*;
2
3 /**
4 * A main method to test how to insert MULTIPLE rows to a mySQL
5 * database table in a single transaction. This is required so
6 * that the id references may be inserted to multiple tables
7 * using an autoincrement field.
8 *
9 * IMPORTANT: This method obtains the database user and database
10 * connection password from the Password class. As such, this
11 * class may be safely placed in a public CVS repository.
12 *
13 * @author $Author: $
14 * @version $Id: $
15 */
16 class DBMultipleInsertTest {
17
18 public static void main(String[] args){
19
20 // Create an instance of the mySQL driver.
21 try{
22 Class.forName("org.gjt.mm.mysql.Driver").newInstance();
23 }
24 catch(Exception e){
25 e.printStackTrace();
26 }
27
28 try {
29 // Use the connection string for our group database.
30 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/co600_10_db?user="+Password.mySQLUser+"&password="+Password.mySQLPassword);
31
32 // Create a statement object.
33 Statement stmt = conn.createStatement();
34
35 // Do NOT commit after each statement.
36 conn.setAutoCommit(false);
37
38 // A couple of SQL statements to try out...
39 String sql1 = "INSERT INTO cpu (id, cpu_no, cpu_load) VALUES (3, 4, 5);";
40 String sql2 = "INSERT INTO cpu (id, cpu_no, cpu_load) VALUES (7, 8, 9);";
41
42 // Execute the insert statements.
43 stmt.executeUpdate(sql1);
44 stmt.executeUpdate(sql2);
45
46 // Now commit the results.
47 conn.commit();
48
49 // Restore autocommit.
50 conn.setAutoCommit(true);
51
52 // Close the statement and database connection.
53 stmt.close();
54 conn.close();
55 }
56 catch (SQLException e) {
57 System.out.println("SQLException: " + e.getMessage());
58 System.out.println("SQLState: " + e.getSQLState());
59 System.out.println("VendorError: " + e.getErrorCode());
60 }
61 }
62 }