Slow day today, so I finally (after 2 years of saying I’d give it a go) got around to trying to use JDBC.
I wrote a simple class to query a mySql database. It worked like a charm! Now all I need to learn is good design practice (which could, I fear, include Beans). I’m not entirely sure how the object model matches up to the database structure – I guess that this is what I need to learn…
import java.sql.*;
import java.util.Properties;
public class JDBCTest {
public static void main( String args[] ){
Connection conn= null;
try{
String cURL = "jdbc:mysql://localhost/jtest";
Properties p = new Properties();
p.put("user","root");
p.put("password","voodoo");
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection( cURL, p );
Statement s = conn.createStatement();
ResultSet r = s.executeQuery( "SELECT * FROM jtable WHERE iID = 1" );
r.first();
System.out.println ( r.getString("cFEN"));
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if( conn != null ) {
try { conn.close(); }
catch ( Exception e ) {}
}
}
}
}