Cs3431: Using JDBC To Connect Mysql Database: 1 Setup
Cs3431: Using JDBC To Connect Mysql Database: 1 Setup
1 Setup
Installing JDBC (MySQL Connector/J) Driver Download the JDBC driver (mySQL Connector/J, either .zip or .tar.gz) from http://www.mysql.com/downloads/api-jdbc-stable.html. You only need to unzip the "mysql-connector-java-3.0.9-stable-bin.jar" file and put it on ccc.wpi.edu. Setting the CLASSPATH setenv CLASSPATH directory/mysql-connector-java-3.0.9-stable-bin.jar. You can put this line in your .cshrc file to avoid setting the path each time you logon ccc.
// 2. Create select statement and resultset; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from a"); while (rs.next()) { // iterate through the result set and print result for column a1 System.out.println("a1 = " + rs.getString("a1")); } } catch (SQLException sqlEx) { System.out.println("Caught SQL Exception: " + sqlEx.toString()); } // 3. now close the statement and connection if they exist if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { System.out.println("Could not close: " + sqlEx.toString()); } } if (conn != null) { try { conn.close(); } catch (SQLException sqlEx) { System.out.println("Could not close: " + sqlEx.toString()); } } } } Compile: javac mySQLDB Execute: java classpath .:$CLASSPATH mySQLDB