我编写了一个简单的JDBC程序来执行查询并尝试创建预准备语句,但结果集变为空。但是,当通过一般语句创建执行的相同查询给出输出时,也通过SQL加上。
package com.aexp.balu.jdbc;
import java.io.Reader;
import java.sql.*;
public class SimpleConnection {
public static void main(String[] args) {
// TODO Auto-generated method stub
// String url="URL";
// String uname="uname";
// String pass="password";
String url = "URL";
String uname = "uname";
String pass = "password";
String query = "select CARD_NO from MDC.CAS_UNMCH_ROCS where CARD_NO = ?";
String cardNumber = "3222222222222222223";
try {
// step1 load the driver class
System.out.println("registering the driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
// step2 create the connection object . We need to have the type of
// connection
// The DriverManager class has a method get connection which give
// sthe instance of Connection
System.out.println("creating the connection....");
Connection con = DriverManager.getConnection(url, uname, pass);
// step3 create the statement object
System.out.println("creating the statement");
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, cardNumber);
// step4 execute query -- the stmnt object of the method execute
// query results an object
// of result set
System.out.println("Executing the query");
ResultSet rs = stmt.executeQuery();
System.out.println("After Executing the query");
while (rs.next()) {
System.out.println(" in reading result set");
String cardnumber = rs.getString("CARD_NO");
System.out.println(cardNumber);
}
// step5 close the connection object
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出如下:
registering the driver
creating the connection....
creating the statement
Executing the query
After Executing the query