首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

好难嗄!求高手···(jsp+servlet+javabeen),该如何处理

2012-06-17 
好难嗄!!!求高手(jsp+servlet+javabeen)java.lang.ClassNotFoundException: com.mysql.jdbc.Driverat org.

好难嗄!!!求高手···(jsp+servlet+javabeen)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at sms.db.DBAccess.getConnection(DBAccess.java:91)
at sms.bean.StudentBean.queryStuInfo(StudentBean.java:126)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:56)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
2012-5-30 21:12:46 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at sms.db.DBAccess.query(DBAccess.java:137)
at sms.db.DBAccess.getSysDate(DBAccess.java:246)
at sms.bean.StudentBean.queryStuInfo(StudentBean.java:128)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:56)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)



//附上数据库连接的类
package sms.db;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.sql.DriverManager;
import javax.naming.*;

public class DBAccess {
  /**
  * 数据库连接
  */
  private Connection conn = null;

  /**
  * jdbc的Statement类型变量
  */
  private Statement stmt = null;

  /**
  *jdbc的结果集类型变量
  */
  private ResultSet rs = null;

  /**
  * 用于实现数据库连接的数据源
  */
  // private DataSource dataSource = null;

  /**
  * 是否用于调试
  */
  private boolean debug = true;

  /**
  * 直接使用JDBC连接数据据库时的连接字符串
  */
  private String dbUrlStr = "jdbc:mysql://localhost:3306/smsdb?user=root&password=111111&useUnicode=true&characterEncoding=gbk";

  /**
  * 直接使用JDBC连接数据据库时的驱动程序名
  */
  private String driverName = "com.mysql.jdbc.Driver";


  /**
  * 构造函数
  */
  public DBAccess() {}

  public DBAccess(boolean debug) {
  this.debug = debug;
  }

  /**
  * 返回一个数据库连接
  * @return Connection
  */
  public void setDbUrlParameter(String driverName, String urlStr) {
  if (driverName == null) {
  driverName = "com.mysql.jdbc.Driver";
  } else {
  this.driverName = driverName;
  }
  if (urlStr == null) {
  this.dbUrlStr = "jdbc:mysql://localhost:3306/smsdb?user=root&password=111111&useUnicode=true&characterEncoding=gbk";
  } else {
  this.dbUrlStr = urlStr;
  }
  }

  /**
  * 返回一个数据库连接
  * @return Connection
  */
  public Connection getConn() {
  if (conn == null) {
  getConnection();
  }
  return conn;
  }

  /**
  * 取得一个数据库连接
  */
  public void getConnection() {
  try {
  System.out.println("ccc");
  if (this.debug) {
  Class.forName(this.driverName);
  this.conn = DriverManager.getConnection(dbUrlStr);
  this.stmt = this.conn.createStatement();
  System.out.println("数据库连接建立!");
  } else {
  javax.naming.Context ctx = new javax.naming.InitialContext();
  javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(
  "java:comp/env/jdbc/smsdb");
  this.conn = ds.getConnection();
  this.stmt = this.conn.createStatement();
  System.out.println("数据库连接建立(连接池)!");
  }
  } catch (NamingException ex1) {
  System.out.println("请检查数据库连接池配置是否正确!");
  ex1.printStackTrace();
  } catch (SQLException ex2) {
  System.out.println("请检查数据库是否启动!");
  ex2.printStackTrace();
  } catch (ClassNotFoundException ex3) {
  System.out.println("请检查数据库驱动程序是否正确!");
  ex3.printStackTrace();
  }


  }

  /**
  * 通过连接池建立连接
  * @param dataSource DataSource
  */
  public void getConnection(DataSource dataSource) {
  try {
  conn = dataSource.getConnection();
  stmt = conn.createStatement();
  System.out.println("bbb");
  } catch (SQLException ex) {
  System.out.println("请检查数据库是否启动!");
  ex.printStackTrace();
  }
  }

  /**
  * 按sql查询数据,返回数据结果集
  */
  public ResultSet query(String strSql) {
  //System.out.println("sql:" + strSql);
  ResultSet rs = null;
  try {
  rs = stmt.executeQuery(strSql);
  return rs;
  } catch (SQLException ex) {
  ex.printStackTrace();
  return rs;
  }
  }

   
  /**
  * 数据库中数据更新的方法
  * @param strSql String
  * @return int
  */
  public int executeSql(String strSql) {
  System.out.println("sql:" + strSql);
  int result = 0;
  try {
  stmt = conn.createStatement();
  result = stmt.executeUpdate(strSql);
  } catch (SQLException ex) {
  System.out.println("产生异常,:at DBAccess.executeSql()");
  ex.printStackTrace();
  }
  return result;
  }

   
  /**
  * 关闭数据库连接
  */
  public void closeConnection() {
  try {
  if (rs != null) {
  rs.close();
  rs = null;
  }
  if (stmt != null) {
  stmt.close();
  stmt = null;
  }
  if (conn != null) {
  conn.close();
  conn = null;
  }
  System.out.println("数据库连接关闭!");
  } catch (SQLException ex) {
  System.out.println("产生异常:at DBAccess.closeConnection()");
  ex.printStackTrace();
  }
  }

  /**
  * 得到系统时间的方法
  * @return String
  */
  public static String getSysDate() {
  DBAccess dba = new DBAccess();
  String sql = "select sysdate() sysdate;";
  try {
  dba.getConnection();
  ResultSet rs = dba.query(sql);
  String currentDate = null;
  if (rs.next()) {
  currentDate = rs.getString("sysdate");
  }
  return currentDate;
  } catch (SQLException ex) {
  System.out.println("产生异常:at DBAccess.getSysDate()");
  ex.printStackTrace();
  return null;
  } finally {
  dba.closeConnection();
  }
  }

  public static void main(String[] args) {
  DBAccess db= new DBAccess();
  System.out.println("aaa");
  }
}


[解决办法]
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

根本就是漏了把MySQL的驱动jar包加入Classpath或者引入工程嘛。。。
------解决方案--------------------


java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
换个驱动
[解决办法]
mysql-connector-java-5.0.8.zip ?!

怎么会是zip?应该是 jar !

热点排行