Guest User

Untitled

a guest
Aug 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. preparedStatement.setString taking over 5 seconds longer then hardcoding parameters
  2. package testdbconnection;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class Main {
  11.  
  12. public static void main(String[] args) throws SQLException
  13. {
  14. Connection con = getConnection();
  15.  
  16. PreparedStatement statement = con.prepareStatement("select col1 from tableName where username = ? and password = ?");
  17.  
  18. statement.setString(1, "UName");
  19. statement.setString(2, "PWord");
  20.  
  21. long start = System.currentTimeMillis();
  22.  
  23. ResultSet rs = statement.executeQuery();
  24.  
  25. long stop = System.currentTimeMillis();
  26.  
  27. System.out.println("took: " + (stop - start));
  28.  
  29. rs.close();
  30. con.close();
  31.  
  32. }// end main
  33.  
  34. private static Connection getConnection()
  35. {
  36. Connection connection = null;
  37.  
  38. try {
  39. // Load the JDBC driver
  40. String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  41. Class.forName(driverName);
  42.  
  43. // Create a connection to the database
  44. String url = "jdbc:sqlserver://DBSERVERNAME;databaseName=DBNAME;";
  45. String username = "dbUname";
  46. String password = "dbPword";
  47.  
  48. connection = DriverManager.getConnection(url, username, password);
  49. }
  50. catch (ClassNotFoundException e)
  51. {
  52. e.printStackTrace();
  53. } catch (SQLException e)
  54. {
  55. e.printStackTrace();
  56. }
  57.  
  58. return connection;
  59. }// end getConnection()
  60.  
  61. }
  62.  
  63. package testdbconnection;
  64.  
  65. import java.sql.Connection;
  66. import java.sql.DriverManager;
  67. import java.sql.PreparedStatement;
  68. import java.sql.ResultSet;
  69. import java.sql.SQLException;
  70.  
  71. public class Main {
  72.  
  73. public static void main(String[] args) throws SQLException
  74. {
  75. Connection con = getConnection();
  76.  
  77. PreparedStatement statement = con.prepareStatement("select col1 from tableName where username = 'UName' and password = 'PWord'");
  78.  
  79. long start = System.currentTimeMillis();
  80.  
  81. ResultSet rs = statement.executeQuery();
  82.  
  83. long stop = System.currentTimeMillis();
  84.  
  85. System.out.println("took: " + (stop - start));
  86.  
  87. rs.close();
  88. con.close();
  89.  
  90. }// end main
  91.  
  92. private static Connection getConnection()
  93. {
  94. Connection connection = null;
  95.  
  96. try {
  97. // Load the JDBC driver
  98. String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  99. Class.forName(driverName);
  100.  
  101. // Create a connection to the database
  102. String url = "jdbc:sqlserver://DBSERVERNAME;databaseName=DBNAME;";
  103. String username = "dbUname";
  104. String password = "dbPword";
  105.  
  106. connection = DriverManager.getConnection(url, username, password);
  107. }
  108. catch (ClassNotFoundException e)
  109. {
  110. e.printStackTrace();
  111. } catch (SQLException e)
  112. {
  113. e.printStackTrace();
  114. }
  115.  
  116. return connection;
  117. }// end getConnection()
  118.  
  119. }
Add Comment
Please, Sign In to add comment