Web Logic Server Question:
Download Questions PDF

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

Answer:

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

Download Web Logic Server Interview Questions And Answers PDF

Previous QuestionNext Question
Web Logic Server job interview questions part five!How to write a program to call the store procedure of Oracle that takes no parameter?