Oracle JDBC can query variable-length fields through placeholders, and cannot query fixed-size fields

Source: Internet
Author: User

This question in six months ago, took me two days to solve, now just want to get up, by the way to record to prevent forgetting.

With JDBC, precompiled SQL cannot be queried for fixed-length fields.

To speak with an example:

Create a table with only one field and a fixed type of char for length. and insert a piece of data.

CREATE TABLE Tblchar

(Name char (20));

A precompiled SQL query is used in Java code and the results cannot be queried. and using the concatenation of SQL, you can query normally.

1  Public Static voidMain (string[] args) {2         Try {3Class.forName ("Oracle.jdbc.driver.OracleDriver");4String url = "Jdbc:oracle:thin: @localhost: 1521:xe";5Connection Connection = drivermanager.getconnection (URL, "sys as SYSDBA",6"Changeme123");7             8PreparedStatement statement =Connection.preparestatement (9"SELECT * from Tblchar t where T.name =?");TenStatement.setstring (1, "a"); One  AResultSet ResultSet =statement.executequery (); -SYSTEM.OUT.PRINTLN ("First query result is:"); -              while(NULL! = ResultSet &&Resultset.next ()) { theSystem.out.println (resultset.getstring (1)); -             } -PreparedStatement Statement2 =Connection.preparestatement ( -"SELECT * from Tblchar t where T.name = ' a '"); +  -ResultSet ResultSet2 =statement2.executequery (); +System.out.println ("Second query result is:"); A              while(NULL! = ResultSet2 &&Resultset2.next ()) { atSystem.out.println (resultset2.getstring (1)); -             } - connection.close (); -}Catch(ClassNotFoundException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); in}Catch(SQLException e) { -             //TODO auto-generated Catch block to e.printstacktrace (); +         } -}
View Code

The results of the operation are as follows:

The variable-length query process is a little, and countless practices have proven to be queryable.

Conclusion:

Database tables are designed with as few fixed lengths as possible.

1. Fixed length wasted space 2. The JDBC precompiled statement was unable to query the results.

Oracle JDBC can query variable-length fields through placeholders, and cannot query fixed-size fields

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.