Create a file simpleJDBC.java containing the
following
code:
import java.sql.* ;
class simpleJDBC
{
public static void main ( String [ ] args ) throws SQLException
{
// Unique table names. Either the user supplies a unique identifier as a command line argument, or the program makes one up.
String tableName = "";
int sqlCode=0; // Variable to hold SQLCODE
String sqlState="00000"; // Variable to hold SQLSTATE
if ( args.length > 0 ){
tableName += args [ 0 ] ;
}
else {
tableName += "example3.tbl";
}
// Register the driver. You must register the driver before you can use it.
try {
DriverManager.registerDriver ( new com.ibm.db2.jcc.DB2Driver() ) ;
} catch (Exception cnfe){
System.out.println("Class not found");
}
// This is the url you must use for DB2.
//Note: This url may not valid now !
String url = "jdbc:db2://db2:50000/cs421";
Connection con = DriverManager.getConnection (url,your_userid,your_password) ;
Statement statement = con.createStatement ( ) ;
// Creating a table
try {
String createSQL = "CREATE TABLE " + tableName + " (id INTEGER, name VARCHAR (25)) ";
System.out.println (createSQL ) ;
statement.executeUpdate (createSQL ) ;
System.out.println ("DONE");
}catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Inserting Data into the table
try {
String insertSQL = "INSERT INTO " + tableName + " VALUES ( 1 , \'Vicki\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
insertSQL = "INSERT INTO " + tableName + " VALUES ( 2 , \'Vera\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
insertSQL = "INSERT INTO " + tableName + " VALUES ( 3 , \'Franca\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Querying a table
try {
String querySQL = "SELECT id, name from " + tableName + " WHERE NAME = \'Vicki\'";
System.out.println (querySQL) ;
java.sql.ResultSet rs = statement.executeQuery ( querySQL ) ;
while ( rs.next ( ) ) {
int id = rs.getInt ( 1 ) ;
String name = rs.getString (2);
System.out.println ("id: " + id);
System.out.println ("name: " + name);
}
System.out.println ("DONE");
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
//Updating a table
try {
String updateSQL = "UPDATE " + tableName + " SET NAME = \'Mimi\' WHERE id = 3";
System.out.println(updateSQL);
statement.executeUpdate(updateSQL);
System.out.println("DONE");
// Dropping a table
String dropSQL = "DROP TABLE " + tableName;
System.out.println ( dropSQL ) ;
statement.executeUpdate ( dropSQL ) ;
System.out.println ("DONE");
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Finally but importantly close the statement and connection
statement.close ( ) ;
con.close ( ) ;
}
}