Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:14:55+00:00 2026-05-16T05:14:55+00:00

I’ve been trying to figure out why the following code is not generating any

  • 0

I’ve been trying to figure out why the following code is not generating any data in my ResultSet:

String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL = ? ";
PreparedStatement prepStmt = conn.prepareStatement(sql);
prepStmt.setString(1, "Waterloo");
ResultSet rs = prepStmt.executeQuery();

On the other hand, the following runs properly:

String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL = 'Waterloo' ";
PreparedStatement prepStmt = conn.prepareStatement(sql);
ResultSet rs = prepStmt.executeQuery();

The data type for SCHOOL is CHAR (9 Byte). Instead of setString, I also tried:

String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL = ? ";
PreparedStatement prepStmt = conn.prepareStatement(sql);
String school = "Waterloo";
Reader reader = new CharArrayReader(school.toCharArray());
prepStmt.setCharacterStream(1, reader, 9);
prepStmt.setString(1, "Waterloo");
ResultSet rs = prepStmt.executeQuery();

I’m completely stuck on what to investigate next; the Eclipse debugger says the SQL query doesn’t change even after setString or setCharacterStream. I’m not sure if it’s because setting parameters isn’t working, or if the debugger simply can’t pick up changes in the PreparedStatement.

Any help will be greatly appreciated, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:14:56+00:00Added an answer on May 16, 2026 at 5:14 am

    I think the problem is that your datatype is CHAR(9) and “Waterloo” has only 8 chars.
    I assume that this would return the expected results (LIKE and %). Or add the missing space.

    String sql = "SELECT STUDENT FROM SCHOOL WHERE SCHOOL LIKE ? ";
    PreparedStatement prepStmt = conn.prepareStatement(sql);
    prepStmt.setString(1, "Waterloo%");
    ResultSet rs = prepStmt.executeQuery();
    

    The best way would by to use varchar instead of char if your Strings have a flexible length. Then the PreparedStatement would work as expected.

    A workaround would be to use the Oracle specific setFixedCHAR method (but it’s better to change the datatype to varchar if possible).

    The following is from Oracle’s PreparedStatement JavaDoc:


    CHAR data in the database is padded to the column width. This leads to a limitation in using the setCHAR() method to bind character data into the WHERE clause of a SELECT statement–the character data in the WHERE clause must also be padded to the column width to produce a match in the SELECT statement. This is especially troublesome if you do not know the column width.

    setFixedCHAR() remedies this. This method executes a non-padded comparison.

    Notes:

    • Remember to cast your prepared statement object to OraclePreparedStatement to use the setFixedCHAR() method.
    • There is no need to use setFixedCHAR() for an INSERT statement. The database always automatically pads the data to the column width as it inserts it.

    The following example demonstrates the difference between the setString(), setCHAR() and setFixedCHAR() methods.

    // Schema is : create table my_table (col1 char(10));
    //             insert into my_table values ('JDBC');
    PreparedStatement pstmt = conn.prepareStatement
    ("select count() from my_table where col1 = ?");
    ResultSet rs;
    
    pstmt.setString (1, "JDBC");  // Set the Bind Value
    rs = pstmt.executeQuery();    // This does not match any row
    // ... do something with rs
    CHAR ch = new CHAR("JDBC      ", null);
    ((OraclePreparedStatement)pstmt).setCHAR(1, ch); // Pad it to 10 bytes
    rs = pstmt.executeQuery();     // This matches one row
    // ... do something with rs
    ((OraclePreparedStatement)pstmt).setFixedCHAR(1, "JDBC");
    rs = pstmt.executeQuery();     // This matches one row
    // ... do something with rs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.