Advertisement
Guest User

Postgre JDBC Update Count Test Programm

a guest
Oct 19th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Properties;
  7.  
  8.  
  9. public class TestUpdateCount
  10. {
  11.  
  12. public static void main( String[] args ) throws SQLException, ClassNotFoundException, IOException
  13. {
  14. //////////////////////////////////////////////////////////////////////////////////////
  15. // CHANGE THIS
  16. String dbUrl = "jdbc:postgresql://localhost:5432/db?charSet=UTF-8";
  17. String dbUser = "user";
  18. String dbPassword = "xxxx";
  19. //////////////////////////////////////////////////////////////////////////////////////
  20.  
  21. try
  22. {
  23. Connection con = openConnection( dbUrl, dbUser, dbPassword );
  24. con.setAutoCommit( false );
  25.  
  26. try
  27. {
  28. // Prepare Table
  29. Statement stmtDrop = con.createStatement();
  30. stmtDrop.execute( "DROP TABLE IF EXISTS TESTTABLE " );
  31. con.commit();
  32. Statement stmtCreate = con.createStatement();
  33. stmtCreate.execute( "CREATE TABLE TESTTABLE (id INTEGER PRIMARY KEY)" );
  34. con.commit();
  35. Statement stmtData = con.createStatement();
  36. stmtData.execute( "INSERT INTO TESTTABLE VALUES (5)" );
  37. con.commit();
  38.  
  39. // TestCase: Insert 10 statements via jdbc batch.
  40. // 5h statement should fail because of duplicate key error
  41. con = openConnection( dbUrl, dbUser, dbPassword );
  42. con.setAutoCommit( false );
  43.  
  44. Statement stmt = con.createStatement();
  45. for( int i = 0; i < 10; i++ )
  46. {
  47. stmt.addBatch( "INSERT INTO TESTTABLE VALUES('" + i + "')" );
  48. }
  49.  
  50. stmt.executeBatch();
  51.  
  52. con.commit();
  53. }
  54. catch( java.sql.BatchUpdateException x )
  55. {
  56. final int[] updateCounts = x.getUpdateCounts();
  57. for( int i = 0; i < updateCounts.length; i++ )
  58. {
  59. System.err.println( "updateCounts[" + i + "]=" + updateCounts[i] );
  60. }
  61. // First 5 statements are successfully, so update count should be 1
  62. for( int i = 0; i < 5; i++ )
  63. {
  64. if( updateCounts[i] != 1 )
  65. System.err.println( "Wrong information returned by driver for update Count " + i );
  66. }
  67. // 5th statement
  68. if( updateCounts[5] == -3 )
  69. System.err.println( "Correct information returned by driver for update Count " + 5 );
  70.  
  71. }
  72. }
  73. catch( SQLException s )
  74. {
  75.  
  76. s.printStackTrace();
  77.  
  78. if( s.getNextException() != null )
  79. {
  80. s.getNextException().printStackTrace();
  81.  
  82. }
  83.  
  84. }
  85. catch( Exception s )
  86. {
  87. s.printStackTrace();
  88.  
  89. }
  90. }
  91.  
  92.  
  93. private static Connection openConnection( String url, String user, String password )
  94. throws ClassNotFoundException, SQLException, IOException
  95. {
  96. try
  97. {
  98. Class.forName( "org.postgresql.Driver" );
  99. }
  100. catch( ClassNotFoundException e )
  101. {
  102. System.err.println( "Could not load driver!" );
  103. throw e;
  104. }
  105. Properties props = new Properties();
  106. // Activate for logging
  107. // props.setProperty( "loglevel", "2" );
  108. // FileWriter fw = new FileWriter( "C:\\temp\\test12345.txt" );
  109. // DriverManager.setLogWriter( new PrintWriter( fw ) );
  110.  
  111. props.setProperty( "user", user );
  112. props.setProperty( "password", password );
  113. return DriverManager.getConnection( url, props );
  114.  
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement