你试试这是我写的连接mysql的tomcat的javabean
package com.cf;
//定义Bean所属的套件import java.io.*;
import java.sql.*;//定义DBCon类型实做HttpSessionBindingListener接口
public class DBCon
{
//与数据库链接有关的Bean属性
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
String ConStr="jdbc:mysql://localhost:3306/hotweb?user=admin&password=admin";public Connection getConnection() throws Exception //连接数据库的方法
{
try{
Class.forName("org.gjt.mm.mysql.Driver");
if(con==null){
con=DriverManager.getConnection(ConStr);
}
}
catch(ClassNotFoundException classnotfoundexception)
{
System.err.println("DBconn (): " + classnotfoundexception.getMessage());
}
return this.con;
}
//查询数据库
public ResultSet query(Connection con,String sql)
{
try{
this.con=con;
stmt=this.con.createStatement(); rs=stmt.executeQuery(sql);
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
return this.rs;
}
//更新数据库(包括插入,修改,删除)
public void update(Connection con,String strsql)
{
try{
//this.con=con;
stmt=con.createStatement(); stmt.executeUpdate(strsql);
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
}
//关闭连接!
public void close()
{
try{
con.close();
con=null;
}
catch(Exception sex)
{
System.out.println(sex.toString());
}
}}
其中数据库的用户名和密码均为admin,你可以该成你自己的用户名和密码
package com.cf;
//定义Bean所属的套件import java.io.*;
import java.sql.*;//定义DBCon类型实做HttpSessionBindingListener接口
public class DBCon
{
//与数据库链接有关的Bean属性
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
String ConStr="jdbc:mysql://localhost:3306/hotweb?user=admin&password=admin";public Connection getConnection() throws Exception //连接数据库的方法
{
try{
Class.forName("org.gjt.mm.mysql.Driver");
if(con==null){
con=DriverManager.getConnection(ConStr);
}
}
catch(ClassNotFoundException classnotfoundexception)
{
System.err.println("DBconn (): " + classnotfoundexception.getMessage());
}
return this.con;
}
//查询数据库
public ResultSet query(Connection con,String sql)
{
try{
this.con=con;
stmt=this.con.createStatement(); rs=stmt.executeQuery(sql);
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
return this.rs;
}
//更新数据库(包括插入,修改,删除)
public void update(Connection con,String strsql)
{
try{
//this.con=con;
stmt=con.createStatement(); stmt.executeUpdate(strsql);
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
}
//关闭连接!
public void close()
{
try{
con.close();
con=null;
}
catch(Exception sex)
{
System.out.println(sex.toString());
}
}}
其中数据库的用户名和密码均为admin,你可以该成你自己的用户名和密码
如:E:\My Webs\Tomcat 4.1\common\lib\mysqldriver.jar