0

我正在尝试学习 jsp 页面中的数据库连接。这是我的代码:

这是我的小服务程序:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    out.println("servlet");

    String username=request.getParameter("username"), password=request.getParameter("password");
    out.println("Username: "+username+" Password: "+password);

    CheckUser(username,password);
}

protected void CheckUser(String username, String password)
{
    dbConnect.connect();

}

这是我的 dbConnect 课程:

import java.sql.DriverManager;

import com.mysql.jdbc.Connection;


public class dbConnect {

  static String dbUrl="jdbc:mysql://localhost:3306/Bank";
  String username="root";
  String password="";

  static Connection con=null;

  public static void connect()
  {
      try {
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       con=(Connection) DriverManager.getConnection(dbUrl);
       System.out.println("Connected!");

      } 
      catch (Exception e) {

          System.out.println("not connected");

      }
  }

}

问题是,在 connect() 函数中,当涉及到 Class.forName("com.mysql.jdbc.Driver").newInstance(); 它产生一个错误并进入 catch 块。这里有什么问题有人可以帮忙吗?谢谢。

编辑:我做了 Nambari 所说的,现在我在con=(Connection) DriverManager.getConnection(dbUrl);行中得到错误。这是一个例外:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'bank'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:353)
at com.mysql.jdbc.Util.getInstance(Util.java:336)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2938)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:855)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3337)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1930)
at com.mysql.jdbc.Connection.<init>(Connection.java:716)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:353)
at com.mysql.jdbc.Connection.getInstance(Connection.java:295)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at dbConnect.connect(dbConnect.java:18)
at loginServlet.CheckUser(loginServlet.java:41)
at loginServlet.service(loginServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
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:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

未连接

4

3 回答 3

1

Class.forName("com.mysql.jdbc.Driver").newInstance(); 它产生一个错误并进入 catch 块。

基于此信息,我的猜测是您mysql.jar不在运行时类路径中。

于 2013-07-01T21:03:18.307 回答
1

替换e.printStacktrace();而不是您的幼稚System.out.println();以查看错误和有关如何解决它们的消息。顺便说一句,看起来你缺少数据库连接 jar(在本例中是连接 MySQL 的 jar),这个 jar 和其他第三方库应该放在WEB-INF/lib目录中。

对于您的第二个错误,您忘记使用userpassword变量。只需将其更改为

con=(Connection) DriverManager.getConnection(dbUrl, user, password);

请注意,如果使用数据库连接池,您可以摆脱所有这些问题。请参阅将 jdbc 连接代码放在 servlet 类中是否是个好主意?学习处理数据库连接的最佳方法。

于 2013-07-01T21:15:16.207 回答
1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'bank'

表示您的连接参数mysql错误。请检查userpasswordhost参数driver

于 2013-07-01T21:16:03.677 回答