1.

How Do I Bind String Values In A Preparedstatement?

Answer»

Suppose you are trying to tget the PreparedStatement class to bind Strings in a statement. The setString() method doesn't SEEM to work.

Here is how you have SET up the PreparedStatement:

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

The preceding code does not work because the COMPLETE value needs to be specified in a String (without using embedded quotes) and then bound to an unquoted question-mark (?).

Here is the corrected code:

String matchvalue = "smit%";
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();

Suppose you are trying to tget the PreparedStatement class to bind Strings in a statement. The setString() method doesn't seem to work.

Here is how you have set up the PreparedStatement:

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

The preceding code does not work because the complete value needs to be specified in a String (without using embedded quotes) and then bound to an unquoted question-mark (?).

Here is the corrected code:

String matchvalue = "smit%";
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();



Discussion

No Comment Found