jsp连接mysql出错
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 21 in the jsp file: /dbaccess.jsp
com.mysql.jdbc.Driver cannot be resolved to a type
18: //加载驱动程序,下面的代码加载MySQL驱动程序
19: Class.forName("com.mysql.jdbc.Driver");
20: //注册MySQL驱动程序
21: DriverManager.registerDriver(new com.mysql.jdbc.Driver());
22: //用适当的驱动程序连接到数据库
23: String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312";
24: String dbUser="root";
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
<%
Connection con;
Statement stmt;
ResultSet rs;
//加载驱动程序,下面的代码加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
//注册MySQL驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//用适当的驱动程序连接到数据库
String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312";
String dbUser="root";
String dbPwd="1234";
//建立数据库连接
con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
%>
[最优解释]
package com.jiangwei.dang.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DbUtil {
//数据源连接池对象
private static DataSource dataSource = null;
//将一个对象和线程绑定
private static ThreadLocal<Connection> connLocal =
new ThreadLocal<Connection>();
static{
try{
Properties props = new Properties();
//加载db.properties配置参数
props.load(DbUtil.class.getClassLoader()
.getResourceAsStream("db.properties"));
//利用dbcp组件创建dataSource对象
dataSource = BasicDataSourceFactory
.createDataSource(props);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
//获取和当前线程相关的connection
Connection conn = connLocal.get();
if(conn == null
[其他解释]
//注册MySQL驱动程序
21: DriverManager.registerDriver(new com.mysql.jdbc.Driver());
这一行就不需要了吧!
[其他解释]
conn.isClosed()){//如果没有,或已关闭
conn = dataSource.getConnection();//获取新的connection
connLocal.set(conn);
}
return conn;
}
public static void closeConnection(){
//获取和当前线程相关的connection
Connection conn = connLocal.get();
//清除和线程绑定的conn
connLocal.set(null);//为下一次调用getConnection要新建
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
配置文件db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/dang
username=root
password=1234
#初始连接对象的大小
initialSize=2
#连接对象最大值
maxActive=15
#最大空闲连接数(大于5个销毁)
maxIdle=5
#最小空闲连接数(小于2个创建)
minIdle=2
#最大等待时间(ms)
maxWait=30000