Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- preparedStatement.setString taking over 5 seconds longer then hardcoding parameters
- package testdbconnection;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Main {
- public static void main(String[] args) throws SQLException
- {
- Connection con = getConnection();
- PreparedStatement statement = con.prepareStatement("select col1 from tableName where username = ? and password = ?");
- statement.setString(1, "UName");
- statement.setString(2, "PWord");
- long start = System.currentTimeMillis();
- ResultSet rs = statement.executeQuery();
- long stop = System.currentTimeMillis();
- System.out.println("took: " + (stop - start));
- rs.close();
- con.close();
- }// end main
- private static Connection getConnection()
- {
- Connection connection = null;
- try {
- // Load the JDBC driver
- String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- Class.forName(driverName);
- // Create a connection to the database
- String url = "jdbc:sqlserver://DBSERVERNAME;databaseName=DBNAME;";
- String username = "dbUname";
- String password = "dbPword";
- connection = DriverManager.getConnection(url, username, password);
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- return connection;
- }// end getConnection()
- }
- package testdbconnection;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Main {
- public static void main(String[] args) throws SQLException
- {
- Connection con = getConnection();
- PreparedStatement statement = con.prepareStatement("select col1 from tableName where username = 'UName' and password = 'PWord'");
- long start = System.currentTimeMillis();
- ResultSet rs = statement.executeQuery();
- long stop = System.currentTimeMillis();
- System.out.println("took: " + (stop - start));
- rs.close();
- con.close();
- }// end main
- private static Connection getConnection()
- {
- Connection connection = null;
- try {
- // Load the JDBC driver
- String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- Class.forName(driverName);
- // Create a connection to the database
- String url = "jdbc:sqlserver://DBSERVERNAME;databaseName=DBNAME;";
- String username = "dbUname";
- String password = "dbPword";
- connection = DriverManager.getConnection(url, username, password);
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- return connection;
- }// end getConnection()
- }
Add Comment
Please, Sign In to add comment