JSP配置连接Mysql及tomcat5.5错误
1.jsp连接Mysql数据库:加载驱动、创建Connection连接、Statement发送sql语句、ResultSet获取结果集
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectMySql {
//
private String username;
private String password;
private String DBDriver="com.mysql.jdbc.Driver";//声明驱动
private String DBConn="jdbc:mysql://localhost:3306/userinfo?useUnicode=true&characterEncoding=utf8";//连接
private String user="root";//用户名
private String pass="sa";//密码
public Connection conn=null;
public ResultSet rs=null;
public PreparedStatement pstmt=null;
//初始化连接
public ConnectMySql(){
try{
Class.forName(DBDriver);//加载JDBC驱动程序
}catch(ClassNotFoundException e){
System.out.println("加载驱动器错误:"+e.getMessage());
}
}
//查询语句
public ResultSet executeQuery(String sql,ArrayList list){
try{
conn=DriverManager.getConnection(DBConn, user, pass);
pstmt=conn.prepareStatement(sql);
//赋值
username=(String)list.get(0);
password=(String)list.get(1);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs=pstmt.executeQuery();
}catch(SQLException e){
System.out.println("查询错误:"+e.getMessage());
e.printStackTrace();
}
//CloseDataBase();
return rs;
}
//修改语句
public int executeUpdate(String sql,ArrayList list){
int num=0;
try {
conn=DriverManager.getConnection(DBConn, user, pass);
pstmt=conn.prepareStatement(sql);
//赋值
num=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CloseDataBase();
return num;
}
//关闭数据库
public void CloseDataBase(){
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.关闭tomcat时出现如下错误,导致无法关闭,暂时还未解决
StandardServer.await: Invalid command '' received