import java.sql.*; public class JDBCTest { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); String username = System.getProperty("username", "user"); String password = System.getProperty("password", "password"); String database = System.getProperty("database", "test"); boolean profileSQL = Boolean.getBoolean("profileSQL"); boolean traceProtocol = Boolean.getBoolean("traceProtocol"); boolean useServerPrepStmts = Boolean.getBoolean("useServerPrepStmts"); String charset = System.getProperty("charset", "utf8"); String jdbcUrl = "jdbc:mysql://localhost/" + database + "?characterEncoding=" + charset + "&dumpQueriesOnException=true&profileSQL=" + profileSQL + "&traceProtocol=" + traceProtocol + "&useServerPrepStmts=" + useServerPrepStmts; System.out.println("Using connection string: " + jdbcUrl); Connection conn = DriverManager.getConnection(jdbcUrl, username, password); System.out.println("==========================="); System.out.println("Using Statement"); System.out.println("==========================="); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT 'non-blank string' AS works"); dump(rs); rs.close(); rs = null; s.close(); s = null; System.out.println("==========================="); System.out.println("Using PreparedStatement"); System.out.println("==========================="); PreparedStatement ps = conn.prepareStatement("SELECT 'non-blank string' AS works"); rs = ps.executeQuery(); dump(rs); rs.close(); rs = null; ps.close(); ps = null; System.out.println("==========================="); System.out.println("Using Parameterized PreparedStatement"); System.out.println("==========================="); ps = conn.prepareStatement("SELECT ? AS broken"); ps.setString(1, "non-blank string"); rs = ps.executeQuery(); dump(rs); } private static void dump(ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); for(int i=0; i