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