我有以下代码似乎可以正常工作,但它不显示 personCode 字符串的任何值。PERSON_CODE 是 Oracle 9i 数据库中的 VARCHAR2。
我正在为我的项目使用 Java SE 1.7 和 ojdbc7.jar。我是 Java 新手,有人可以帮我解决这个问题吗?
private static void GetEmployee(String input) {
String output = "";
Connection con=null;
PreparedStatement stmt = null;
String sql ="SELECT ALL BADGE_NUMBER, PERSON_CODE FROM BADGETABLE WHERE BADGE_NUMBER = ?";
try {
//load driver
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:username/password@host:1521:database");
//declaring statement
stmt = con.prepareStatement(sql);
stmt.setString(1, input);
// execute query
ResultSet rows = stmt.executeQuery();
int i = 0;
while(rows.next()) {
i++;
String badgeCode = rows.getString(1);
String personCode = rows.getString(2);
String personType = rows.getString(3);
System.out.println("Badge number: " + badgeCode);
System.out.println("Employee ID: " + personCode);
}
System.out.println("Number of results: " + i);
rows.close(); // All done with that resultset
stmt.close(); // All done with that statement
con.close(); // All done with that DB connection
}
catch (SQLException e) {
System.err.println(e);
}
catch (ClassNotFoundException e) {
System.err.println(e);
}
return;
}