我目前在CentOS机器上部署了一个MySQL数据库。我可以通过MySQL Workbench查询数据库,我也可以在本地开发环境中使用MySQL JDBC来连接数据库。这两个连接都没有问题。但是,我正在构建一个使用Jenkins构建并部署到Tomcat Web服务器上的Web应用程序。但是,部署后,连接失败。我不知道我哪里出错了。我出于安全原因将代码放在下面但省略了密码(这是正确的,我已多次验证)。在服务器日志中,抛出和捕获的错误是conn为null的结果。这意味着它永远不会被设置,因为参数不正确。我唯一的猜测是,当应用程序移动到Web服务器上时,JDBC URL会发生变化。
enter code here//Connection for database
private static Connection conn;
//Database parameters
private String url = "jdbc:mysql://autograder.clarkson.edu:3306/autograder_db";
private String localdevuser = "autograder_dev";
private String user = "autograder_local";
private String password = "-------";
private String getUsername(){
//LOG.publish(new LogRecord(Level.INFO, "user is: " + (AssertionHolder.getAssertion().getPrincipal().getName())));
//return AssertionHolder.getAssertion().getPrincipal().getName();
return "testuser";
}
public List<Course> queryCourses(){
//Debug statement
LOG.publish(new LogRecord(Level.INFO, "#establishConn"));
Connection conn = null;
List<Course> courseList = new ArrayList<Course>();
try{
//create connection to database
conn = DriverManager.getConnection(url, user, password);
LOG.publish(new LogRecord(Level.INFO, "Conn = " + conn));
}
catch (SQLException e){
//Handle exception here
LOG.publish(new LogRecord(Level.INFO, "#DatabaseQuery Failed: " + e.toString()));
}
finally {
try{
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: DB Connection Successful"));
//Temp course selection
String sql = "SELECT c.course_id, c.course_title, c.course_num, c.course_descr "
+ "FROM enrollment e LEFT JOIN courses c "
+ "ON e.enr_cid = c.course_id WHERE e.enr_username = \"" + getUsername() + "\";";
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: String entered"));
Statement stmt;
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: stmt"));
stmt = conn.createStatement();
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: conn.createStatement()"));
ResultSet rs = stmt.executeQuery(sql);
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: RS"));
while(rs.next()){
courseList.add(new Course(Integer.parseInt(rs.getString("course_id")), rs.getString("course_num"), rs.getString("course_title"), true));
LOG.publish(new LogRecord(Level.INFO, "Course: " + rs.getString("course_title")));
}
LOG.publish(new LogRecord(Level.INFO, "#establishConnection: Statement run"));
return courseList;
}
catch (SQLException ex){
}
}
return courseList;
答案 0 :(得分:1)
错误的可能原因是您设计逻辑的方式。您拥有finally
块中的大部分逻辑。这是一个坏主意。例如,如果DriverManager.getConnection()
失败并引发异常,则finally
块中的代码仍然会在conn
仍为null
的情况下执行,从而生成NullPointerException
我将您的代码重新构建为:
List<Course> courseList = new ArrayList<Course>();
try (Connection conn = DriverManager.getConnection(url, user, password)) {
String sql = ...
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
courseList.add(new Course(...));
}
} catch (SQLException e) {
//Handle exception here
LOG.publish(new LogRecord(Level.INFO, "#DatabaseQuery Failed: " + e.toString()));
}
return courseList;