Created
July 21, 2011 15:14
-
-
Save guohai/1097410 to your computer and use it in GitHub Desktop.
quick and stupid test for https://gist.github.com/1097396 in java(need c3p0 to run it directly)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.sql.CallableStatement; | |
| import java.sql.Connection; | |
| import java.sql.PreparedStatement; | |
| import java.sql.SQLException; | |
| import java.sql.Types; | |
| import com.mchange.v2.c3p0.ComboPooledDataSource; | |
| public class InsertingDataWithPS { | |
| // insert2 is faster of course | |
| public static void main(String[] args) { | |
| // insert(); // 32 52 31 | |
| insert2(); // 28 30 25 | |
| } | |
| public static void insert() { | |
| ComboPooledDataSource cpds = new ComboPooledDataSource("fromlocal3307"); | |
| Connection conn = null; | |
| try { | |
| conn = cpds.getConnectionPoolDataSource().getPooledConnection() | |
| .getConnection(); | |
| long start = System.currentTimeMillis(); | |
| conn.setAutoCommit(false); | |
| CallableStatement cstmt = conn | |
| .prepareCall("CALL test_insert_data_null(?,?,?,?,?,?)"); | |
| cstmt.setString(1, "for test ok"); | |
| cstmt.setString(2, "long text long"); | |
| cstmt.setString(3, "2011-07-22 13:30:45"); | |
| cstmt.execute(); | |
| cstmt.registerOutParameter(4, Types.BIGINT); | |
| cstmt.registerOutParameter(5, Types.INTEGER); | |
| cstmt.registerOutParameter(6, Types.VARCHAR); | |
| long newId = cstmt.getLong(4); | |
| cstmt.close(); | |
| conn.commit(); | |
| System.out.println(System.currentTimeMillis() - start); | |
| System.out.println(newId); | |
| } catch (SQLException e) { | |
| try { | |
| conn.rollback(); | |
| } catch (SQLException e1) { | |
| e1.printStackTrace(); | |
| } | |
| e.printStackTrace(); | |
| } finally { | |
| try { | |
| conn.close(); | |
| } catch (SQLException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| public static void insert2() { | |
| ComboPooledDataSource cpds = new ComboPooledDataSource("fromlocal3307"); | |
| Connection conn = null; | |
| try { | |
| conn = cpds.getConnectionPoolDataSource().getPooledConnection() | |
| .getConnection(); | |
| long start = System.currentTimeMillis(); | |
| conn.setAutoCommit(false); | |
| PreparedStatement pstmt = conn | |
| .prepareCall("INSERT INTO str_test(name, ats, dor) VALUES(?, ?, ?)"); | |
| pstmt.setString(1, "for test ok"); | |
| pstmt.setString(2, "long text long"); | |
| pstmt.setString(3, "2011-07-22 13:30:45"); | |
| pstmt.executeUpdate(); | |
| pstmt.close(); | |
| conn.commit(); | |
| System.out.println(System.currentTimeMillis() - start); | |
| } catch (SQLException e) { | |
| try { | |
| conn.rollback(); | |
| } catch (SQLException e1) { | |
| e1.printStackTrace(); | |
| } | |
| e.printStackTrace(); | |
| } finally { | |
| try { | |
| conn.close(); | |
| } catch (SQLException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment