I'm having a problem with MySQL in my small JSP project. I have asked a question about this earlier (http://stackoverflow.com/questions/9860129/jsp-mysql-jar-file-stops-working) and this is a follow up question based on the info form that question.
The class works and the page fetches info from the database for a few days, then it stops working until I either restart the Tomcat server or deploy the war file again.
Here is my MySQL connection class, any ideas would be much appreciated:
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;
}