import java.sql.*; public class TestMysql { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestMysql tm = new TestMysql(); tm.testInsert(); tm.dropConnection(tm.mySqlConnNew); } public void testInsert(){ String insert = "INSERT INTO test (TEXT) VALUES (?)"; java.sql.PreparedStatement ps = null; try { Connection conn = this.getMySqlConnectionNew(); ps = conn.prepareStatement(insert); int pos = 0; ps.setString(++pos, "abc''def"); ps.executeUpdate(); if (ps != null) { try { ps.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } ps = null; } } catch (java.sql.SQLException sqlEx) { sqlEx.printStackTrace(); } catch (Exception exx) { exx.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } ps = null; } } } private Connection mySqlConnNew = null; private void dropConnection(Connection conn) { if (conn != null) { try { conn.close(); } catch (java.sql.SQLException ex) { ex.printStackTrace(); } conn = null; } } private java.sql.Connection getMySqlConnectionNew() throws Exception { if (mySqlConnNew == null || mySqlConnNew.isClosed()) { Class.forName("org.gjt.mm.mysql.Driver"); mySqlConnNew = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "yourlogin", "yourpassword"); System.out.println("Getting new connection "); } return mySqlConnNew; } }