用Eclipse连接MySql的连接问题
[code=Java][/code]
package util;
import java.sql.*;
import java.sql.Connection;
public class Certificate {
private String username;
private String password;
public Certificate()
{
}
public void cert()
{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功");
} catch (
ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("驱动加载失败!");
e.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/goahead","root","admin");
System.out.println("数据库连接成功!");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from credentials");
while(rs.next())
{
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
username = rs.getString("username");
password = rs.getString("password");
}
} catch (
SQLException e) {
// TODO Auto-generated catch block
System.out.print("获取信息出错!");
e.printStackTrace();
}
}
public String getUserName()
{
return username;
}
public String getPassWord()
{
return password;
}
//
// public static void main(String[] args)
// {
// Certificate cert = new Certificate();
// cert.cert();
// }
}
这个是我用来连接mysql的代码,我直接在这个类做测试的时候,连接mysql很快只需要0.几秒。
但是当我启动webserver(tomcat)后,在servlet里面调用这个类来进行连接的时候,就很慢了,需要10多秒,而且打了很多的
log(trace)信息,如下:
Mon Aug 20 20:44:43 CST 2012 TRACE: --> MysqlIO.java:1939 com.mysql.jdbc.MysqlIO.versionMeetsMinimum(4, 1, 0)
Mon Aug 20 20:44:43 CST 2012 TRACE: --> MysqlIO.java:997 com.mysql.jdbc.MysqlIO.getServerMajorVersion()
Mon Aug 20 20:44:43 CST 2012 TRACE: <-- MysqlIO.java:997 com.mysql.jdbc.MysqlIO.getServerMajorVersion(..) returning 5
Mon Aug 20 20:44:43 CST 2012 TRACE: --> MysqlIO.java:997 com.mysql.jdbc.MysqlIO.getServerMajorVersion()
Mon Aug 20 20:44:43 CST 2012 TRACE: <-- MysqlIO.java:997 com.mysql.jdbc.MysqlIO.getServerMajorVersion(..) returning 5
Mon Aug 20 20:44:43 CST 2012 TRACE: <-- MysqlIO.java:1939 com.mysql.jdbc.MysqlIO.versionMeetsMinimum(..) returning true
Mon Aug 20 20:44:43 CST 2012 TRACE: <-- Connection.java:5895 com.mysql.jdbc.Connection.versionMeetsMinimum(..) returning true
Mon Aug 20 20:44:43 CST 2012 TRACE: --> Field.java:794 com.mysql.jdbc.Field.isUnsigned()
Mon Aug 20 20:44:43 CST 2012 TRACE: <-- Field.java:794 com.mysql.jdbc.Field.isUnsigned(..) returning false
Mon Aug 20 20:44:43 CST 2012 TRACE: --> Field.java:861 com.mysql.jdbc.Field.toString()
Mon Aug 20 20:44:43 CST 2012 TRACE: --> Field.java:409 com.mysql.jdbc.Field.getDatabaseName()
Mon Aug 20 20:44:43 CST 2012 TRACE: --> Field.java:582 com.mysql.jdbc.Field.getStringFromBytes(5, 7)
麻烦各位帮忙看下这个是什么问题呢?
谢谢
------解决方案--------------------
把在servlet进行连接的代码粘出来看看。
一般情况下应该不会打出那些日志的,主动调用的时候才会输出那些日志,或者项目里有多个mysql驱动包,楼主检查下lib目录,也检查下tomcat的lib目录,看是不是有多个驱动包?
------解决方案--------------------看你日志,好像是自动匹配驱动啊
------解决方案--------------------1、连接没有正常的关闭,resultset,statemate,connection,这样会耗资源越来越慢