- 爱易网页
 
                        - 
                            Apache教程
 
                        - jsp连接mysql出错解决方案 
 
                         
                    
                    
                    日期:2014-05-16  浏览次数:21021 次 
                    
                        
                         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 
------解决方案--------------------
 conn.isClosed()){//如果没有,或已关闭
			conn = dataSource.getConnection();//获取新的connection