How to write a program to bind string values in a PreparedStatement?

Submitted by: Administrator
The PreparedStatement class allow to bind the string values in the class. The setString() method that can be used with it won't work due to incompatibility. The example shows the usage of the PreparedStatement:

String pstmt = "select n_name from n_table where n_name LIKE '?%'";
PreparedStatement ps = conn.prepareStatement(pstmt);
ps.setString(1, "Rohit");
ResultSet rs = ps.executeQuery();

This PreparedStatement doesn't work due to the fact that the value needs to be specified in a string and not be embedded in quotes. The corrected statement that gets executed is as follows:
String matchvalue = "rohit%";
String pstmt = "select n_name from n_table where n_name LIKE ?";
PreparedStatement ps = conn.prepareStatement(pstmt);
ps.setString(1, matchvalue);
ResultSet rs = ps.executeQuery();
Submitted by: Administrator

Read Online Web Logic Server Job Interview Questions And Answers