我在Java类中有以下String
方法:
public String getStudentId(Id userId) throws Exception {
String sid = null;
try {
UserDbLoader userDbLoader = UserDbLoader.Default.getInstance();
User ul = userDbLoader.loadById(userId);
sid = ul.getStudentId();
System.out.println("");
System.out.println("***Debug Section 1***");
System.out.println("Student ID: " + sid);
}
catch (Exception e) {
e.printStackTrace();
}
return sid;
}
我想将变量sid
传递给下一个String
方法,该方法使用dbcp2
连接到数据库以返回相应的值:pidm
第二个String方法是:
public String dbQuery(String sid) {
String student_id = sid;
String pdTemp = null;
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
BasicDataSource bds = MapSource.getInstance().getBds();
connection = bds.getConnection();
statement = connection.prepareStatement("select pidm from b21_toolbar_timetable_mapping where student_id = ?");
statement.setObject(1, student_id);
rs = statement.executeQuery();
while (rs.next()) {
pdTemp = rs.getNString("pidm");
}
System.out.println("");
System.out.println("**** Debug Section 2 ***");
System.out.println("**** SID: " + sid);
System.out.println("**** student_id: " + student_id);
System.out.println("**** PIDM: " + pdTemp);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
if (rs != null)
rs.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
return pdTemp;
}
我的问题是我错过了什么? sid
变量未传递到try
中的dbQuery
语句,因此Debug Section 2语句不包含任何输出。