Tasks with the specified report parameters

I want the Java method to pull a record from my Oracle 11g database, but I ran into some problems in not getting any returned records. If I hard code the value by dividing line 4 and commenting on lines 5 and 7, the result will be populated with a record. An exception is not excluded. What am I missing?

  conn = DriverManager.getConnection(url,props); String sql = "select col1, col2, col3" + " from table1" // + " where user_id = 'user123'"; // line 4 + " where user_id = ?"; // line 5 PreparedStatement preStatement = conn.prepareStatement(sql); preStatement.setString(1, "user123"); // line 7 ResultSet result = preStatement.executeQuery(); while(result.next()) { System.out.println("works"); } 
+4
source share
1 answer

Use the trim () function in the query. Perhaps this is due to a problem with the table.

 String sql = "select col1, col2, col3" + " from table1" + " where trim(user_id) = ?"; // line 5 

Leave a space after the question mark (?).

+3
source

Source: https://habr.com/ru/post/1502810/


All Articles