Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6680935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:31:59+00:00 2026-05-26T04:31:59+00:00

I am connecting to a Microsoft sql server database via java jdbc and having

  • 0

I am connecting to a Microsoft sql server database via java jdbc and having a very strange issue. Whenever I use the place holder parameters (?) in my query in the where clause and then do preparedStatement.setString(..) method my simple query takes anywhere from 4800 to 5800 milliseconds to run. When I hardcode the where clause inside of the query itself, it takes from 1 to 36 milliseconds to run. This doesn’t make sense to me, because I thought using the placeholders was supposed to be faster…

The table does have a lot of rows (8 million or so), however the parameters that I pass in are just a few characters, I only pass in 2 parameters, the statement always returns 1 (or 0) rows and the data it returns is not huge. Indexes on the table don’t help. Why such a HUGE time difference? 5-6 seconds is a really long time for such a query. Both of the columns in the where clause are string-type (varchar(20)) so there is no implicit type conversion on the databse side (that I know of).

I’ve tried a different version of the sqljdbc4.jar driver, but it’s doing the same thing.

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()

}

Output:
run:
took: 4891
BUILD SUCCESSFUL (total time: 5 seconds)

Now if I do this:

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()

}

Output:
run:
took: 32
BUILD SUCCESSFUL (total time: 2 seconds)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:32:00+00:00Added an answer on May 26, 2026 at 4:32 am

    Generally speaking, running an entirely hardcoded statement will be faster than an equivalent parameterized statement. The reason for this has to do with the execution planning of the database. When you provide all of the information from the start, the database can perform optimizations and choose shorter paths specific to the exact data you provided. When you parameterize the statement, it can only perform those optimizations that would be helpful for any value that might be inserted.

    Parameterized statements can be helpful when you need to run many similar queries and want to avoid the overhead of preparing the statement every time, but for a single query on a large data set (as in your use case) a hardcoded query will be better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a SQL Server CE database via Microsoft's OLEDB 3.5 SQL CE Driver.
In my .NET application I am connecting to Microsoft SQL Server 2005 or 2008
I have a table named t_Student in Microsoft SQL Server 2005 database. In that
I can connect to my SQL Server database via sqlcmd from a DOS command
I've tryed to create a connection with a Microsoft SQl Server 2008 database through
I have a very simple application in c# it has a Microsoft SQL Server
Errors Database administrators report Microsoft SQL Server 2008 server-side error Invalid Login (error 18456,
I have a connection to a Microsoft SQL Server and want the change the
Converting a couple stored procedures from MySQL to Microsoft SQL server. Everything is going
I am developing large data collecting ASP.Net/Windows service application-pair that uses Microsoft SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.