我在我的小型 JSP 项目中遇到了 MySQL 的问题。我之前问过一个关于这个的问题(http://stackoverflow.com/questions/9860129/jsp-mysql-jar-file-stops-working),这是一个基于该问题的信息表单的后续问题。
该类工作,页面从数据库中获取信息几天,然后它停止工作,直到我重新启动 Tomcat 服务器或再次部署 war 文件。
这是我的 MySQL 连接类,任何想法将不胜感激:
public class Connect {
final String host = "localhost";
final String username = "admin";
final String password = "admin";
String connectionURL = "jdbc:mysql://" + host + ":3306/db?user=;password=";
Connection connection = null;
Statement statement = null;
public Connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = (Connection) DriverManager.getConnection(connectionURL, username, password);
statement = (Statement) connection.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ResultSet executeQuery(String query) {
ResultSet result = null;
try {
if (statement != null) {
result = statement.executeQuery(query);
} else {
System.out.println("Null");
}
} catch (SQLException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
public boolean execute(String query) {
System.out.println(query);
boolean result = false;
try {
if (statement != null) {
result = statement.execute(query);
} else {
System.out.println("Null");
}
} catch (SQLException ex) {
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}