jsp 连接mysql报错

跑跑鱼 2010-05-20 02:45:05
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.
请问这是怎么回事?
...全文
133 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
跑跑鱼 2010-05-22
  • 打赏
  • 举报
回复
各位帮忙看看上边的程序啊,我从尚学堂 拷贝的源码,报错了,有没有问题啊?
跑跑鱼 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 beblong 的回复:]
连接未能正确关闭,会导致内存泄漏
but failed to unregister it when the web application was stopped.
但是在应用关闭的时候未能卸载
To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
为防止内存泄漏,该JDBC驱动被强制卸载了

……
[/Quote]


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-05-20
  • 打赏
  • 举报
回复
给个能用的代码你对比一下好了

<%@ page contentType="text/html;charset=gb2312"%>
<%-- 导入java.sql包,表示要使用数据库操作 --%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<center>
<h1>登陆范例——用户名及密码固定</h1>
<hr>
<br>
<br>
<%
String name = request.getParameter("uname") ;
String password = request.getParameter("upassword") ;


boolean flag = false ;
%>
<%
final String DBDRIVER = "com.mysql.jdbc.Driver" ;
final String DBURL = "jdbc:mysql://localhost:3306/test";
final String DBUSER = "root" ;
final String DBPASSWORD = "123" ;
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
String sql = null ;
%>
<%
// 进行数据库操作
try
{
sql = "SELECT name FROM person WHERE name=? and password=?";
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
pstmt = conn.prepareStatement(sql) ;

System.out.println(name);
System.out.println(password);

pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
if(rs.next())
{
System.out.println("true");
flag = true ;
}
System.out.println(flag);
rs.close() ;
pstmt.close() ;
conn.close() ;
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("exception");
}
%>
<%
if(flag)
{
%>
<jsp:forward page="login_success.jsp"/>
<%
}
else
{
%>
<jsp:forward page="login_failure.jsp"/>
<%
}
%>
</center>
</body>
</html>
焙焙龙 2010-05-20
  • 打赏
  • 举报
回复
连接未能正确关闭,会导致内存泄漏
but failed to unregister it when the web application was stopped.
但是在应用关闭的时候未能卸载
To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
为防止内存泄漏,该JDBC驱动被强制卸载了

请处理好你的连接
gan158qiang 2010-05-20
  • 打赏
  • 举报
回复
com.mysql.jdbc.Driver 你的jdbc参数错误!!! userName ,password ,对一下是否正确! 还有就是你的jdbc驱动程序是否正确!

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧