Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.jar)

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.jar)

    Hi All,

    I ran the following program with sqljdbc4.jar in the class path. There is data in the EMPLOYEE table for the employee name DEMO but the following program is not retrieving data for DEMO. When the same program was run with Merlia.jar in the class path, it was retrieving data for DEMO. The SQL statement which includes "like" keyword in which the bound value does not contain a % fails to return data with sqljdbc4.jar.

    Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
    Connection con = DriverManager.getConnection("jdbc:sqlserver://SERVER23:5000;databaseName=TESTDB", "SYSADM", "SYSADM");
    String sqlSele = "SELECT * FROM EMPLOYEE WHERE EMPNAME like ?" ;
    PreparedStatement sts = con.prepareStatement(sqlSele);
    sts.setString(1, "DEMO" );
    ResultSet rs = sts.executeQuery();
    while(rs.next())
    {
    System.out.println("EMPNAME IS '" + rs.getString("EMPNAME") + "'");
    }

    }
    catch(Exception e)
    {
    System.out.println(e);
    e.printStackTrace();
    }


    But the program retrieves the data when an % is appended at the end of DEMO as "sts.setString(1, "DEMO%" );"

    Can someone help me out from this issue.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.

    The LIKE operator takes % as a wildcard - if your data matches the 'DEMO', then fine. If not, a wildcard might be necessary. See the following:
    SQL LIKE Operator

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.

    I agree with you but I have written the sample program to ensure that the SQL statements with like keyword are working properly with sqljdbc4.jar.
    As per my requirement the statement can expect % in its value or it may get the exact value. So I have used like keyword to ensure both exact value and wild card value.
    If the value received is exact then the above query is same as SELECT * FROM EMPLOYEE WHERE EMPNAME = 'DEMO'.
    But the statement with like keyword and value not containing wildcard character is not working in case of sqljdbc4.jar.

    Please help me with what may be the probelm with sqljdbc4.jar. Did you ever faced these types of issues when usign sqljdbc4.jar?
    Last edited by charan; December 27th, 2011 at 06:17 AM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.

    Not pretty sure but just give it a try,
    DriverManager.getConnection("jdbc:sqlserver://SERVER23:5000;databaseName=TESTDB", "SYSADM", "SYSADM");
    Change this to,
    DriverManager.getConnection("jdbc:sqlserver://SERVER23:5000","TESTDB", "SYSADM", "SYSADM");
    or
    DriverManager.getConnection("jdbc:sqlserver:@SERVER23:5000","TESTDB", "SYSADM", "SYSADM");

Similar Threads

  1. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 03:08 PM
  2. key binding: VK_ALT and VK_SHIFT not working
    By gib65 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 25th, 2010, 04:54 PM
  3. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  4. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM

Tags for this Thread