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.
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
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?
Re: Binding in PreparedStatement is not working with MS SQLServerJDBCDriver(sqljdbc4.
Not pretty sure but just give it a try,
Code :
DriverManager.getConnection("jdbc:sqlserver://SERVER23:5000;databaseName=TESTDB", "SYSADM", "SYSADM");
Change this to,
Code :
DriverManager.getConnection("jdbc:sqlserver://SERVER23:5000","TESTDB", "SYSADM", "SYSADM");
or
Code :
DriverManager.getConnection("jdbc:sqlserver:@SERVER23:5000","TESTDB", "SYSADM", "SYSADM");