myeclipse报错严重: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2010-5-20 14:35:20 org.apache.catalina.loader.WebappClassLoader clearReferencesStopTimerThread
严重: A web application appears to have started a TimerThread named [MySQL Statement Cancellation Timer] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.
请问这是怎么回事?但是我能够正常存取数据库我用的代码为:public class Dbunit { private static Connection conn = null;
private static Statement state = null;
private static ResultSet rst = null;
public static void addunit(Testunit t) {
try {
conn = getConnection();
state = conn.createStatement();
String sql = "insert into _unit values("+t.getNum()+",'"+t.getName()+"','"+t.getAdd()+"'," +
"'"+t.getGetdate()+"','"+t.getSenddate()
+"',"+t.getCost()+",'"+t.getAcceptor()+"','"+t.getAcceptdate()+"')";
//System.out.println(sql);
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static ArrayList<Testunit> lookforTestunits(String sql) { ArrayList<Testunit> a = new ArrayList<Testunit>(); try { conn = getConnection();
state = conn.createStatement();
// String o="select * from person where pname like '%"+s+"%'";
rst = state.executeQuery(sql);
//System.out.println(sql); while (rst.next()) {
Testunit p = new Testunit();
p.setAcceptdate(rst.getString("acceptdate"));
p.setAcceptor(rst.getString("acceptor"));
p.setAdd(rst.getString("unitadd"));
p.setCost(rst.getFloat("testcost"));
p.setGetdate(rst.getString("date0"));
p.setName(rst.getString("unitname"));
p.setNum(rst.getInt("unitcardno"));
p.setSenddate(rst.getString("date1"));
a.add(p);
// System.out.println(p.getTitle() + " " + p.getCont());
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rst.close();
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return a; }
public static Connection getConnection() {
Connection conn = null;
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPasswd = "mysql";
String dbName = "sampletest";
// String tableName="person";
String port = "3306";
String url = "jdbc:mysql://localhost:" + port + "/" + dbName + "?user="
+ userName + "&password=" + userPasswd
+ "&useUnicode=true&characterEncoding=utf-8";//
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
2010-5-20 14:35:20 org.apache.catalina.loader.WebappClassLoader clearReferencesStopTimerThread
严重: A web application appears to have started a TimerThread named [MySQL Statement Cancellation Timer] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.
请问这是怎么回事?但是我能够正常存取数据库我用的代码为:public class Dbunit { private static Connection conn = null;
private static Statement state = null;
private static ResultSet rst = null;
public static void addunit(Testunit t) {
try {
conn = getConnection();
state = conn.createStatement();
String sql = "insert into _unit values("+t.getNum()+",'"+t.getName()+"','"+t.getAdd()+"'," +
"'"+t.getGetdate()+"','"+t.getSenddate()
+"',"+t.getCost()+",'"+t.getAcceptor()+"','"+t.getAcceptdate()+"')";
//System.out.println(sql);
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static ArrayList<Testunit> lookforTestunits(String sql) { ArrayList<Testunit> a = new ArrayList<Testunit>(); try { conn = getConnection();
state = conn.createStatement();
// String o="select * from person where pname like '%"+s+"%'";
rst = state.executeQuery(sql);
//System.out.println(sql); while (rst.next()) {
Testunit p = new Testunit();
p.setAcceptdate(rst.getString("acceptdate"));
p.setAcceptor(rst.getString("acceptor"));
p.setAdd(rst.getString("unitadd"));
p.setCost(rst.getFloat("testcost"));
p.setGetdate(rst.getString("date0"));
p.setName(rst.getString("unitname"));
p.setNum(rst.getInt("unitcardno"));
p.setSenddate(rst.getString("date1"));
a.add(p);
// System.out.println(p.getTitle() + " " + p.getCont());
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rst.close();
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return a; }
public static Connection getConnection() {
Connection conn = null;
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPasswd = "mysql";
String dbName = "sampletest";
// String tableName="person";
String port = "3306";
String url = "jdbc:mysql://localhost:" + port + "/" + dbName + "?user="
+ userName + "&password=" + userPasswd
+ "&useUnicode=true&characterEncoding=utf-8";//
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
TimerThread named [MySQL Statement Cancellation Timer] 你的web应用中,有这么个TimerThread吗?最好能搜查一下。
日志:
100526 10:07:51 [Note] Plugin 'FEDERATED' is disabled.
100526 10:07:51 InnoDB: Started; log sequence number 0 629844
100526 10:07:53 [Note] Event Scheduler: Loaded 0 events
100526 10:07:53 [Note] D:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld: ready for connections.
Version: '5.1.41-community' socket: '' port: 3306 MySQL Community Server (GPL)
楼主这个类,有点问题
你定义了一个static 的 conn
但是,在getConnectio里面,有一个本地变量 conn,同时,你每次都重新连接,然后返回这个新的连接
或许你可以改成这样
public static Connection getConnection() {
if(conn == null){
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPasswd = "mysql";
String dbName = "sampletest";
// String tableName="person";
String port = "3306";
String url = "jdbc:mysql://localhost:" + port + "/" + dbName + "?user="
+ userName + "&password=" + userPasswd
+ "&useUnicode=true&characterEncoding=utf-8";//
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return conn;
}
}