1 |
import java.sql.*; |
2 |
|
3 |
/** |
4 |
* A main method to test how to insert a single row to a mySQL |
5 |
* database table. |
6 |
* |
7 |
* IMPORTANT: This method obtains the database user and database |
8 |
* connection password from the Password class. As such, this |
9 |
* class may be safely placed in a public CVS repository. |
10 |
* |
11 |
* @author $Author: $ |
12 |
* @version $Id: $ |
13 |
*/ |
14 |
class DBInsertTest { |
15 |
|
16 |
public static void main(String[] args){ |
17 |
|
18 |
// Create an instance of the mySQL driver. |
19 |
try{ |
20 |
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); |
21 |
} |
22 |
catch(Exception e){ |
23 |
e.printStackTrace(); |
24 |
} |
25 |
|
26 |
try { |
27 |
// Use the connection string for our group database. |
28 |
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/co600_10_db?user="+Password.mySQLUser+"&password="+Password.mySQLPassword); |
29 |
|
30 |
// Create a statement object. |
31 |
Statement stmt = conn.createStatement(); |
32 |
|
33 |
// Execute the insert statement. |
34 |
stmt.executeUpdate("INSERT INTO cpu (id, cpu_no, cpu_load) VALUES (1, 2, 3);"); |
35 |
|
36 |
// Close the statement and database connection. |
37 |
stmt.close(); |
38 |
conn.close(); |
39 |
} |
40 |
catch (SQLException e) { |
41 |
System.out.println("SQLException: " + e.getMessage()); |
42 |
System.out.println("SQLState: " + e.getSQLState()); |
43 |
System.out.println("VendorError: " + e.getErrorCode()); |
44 |
} |
45 |
} |
46 |
} |