简体   繁体   English

无法使用JSP,Glassfish连接到mysql数据库

[英]Cannot connect to a mysql database with JSP, Glassfish

I have previous JSP experience but with using Tomcat and Resin and I would like to connect to a mySQL database using Glassfish and hoped that more or less copy and pasting the code would work. 我以前有JSP经验,但是使用过Tomcat和Resin,并且我想使用Glassfish连接到mySQL数据库,并希望或多或少的复制和粘贴代码能够正常工作。

The code is: 代码是:

try {
        Class.forName("org.gjt.mm.mysql.Driver");
    } catch (Exception E) {
        System.out.println("First: " + E);
    }

Connection conn = DriverManager.getConnection("jdbc:mysql://xxx.xx.xx.xx:xxxx/DBName", "Username", "Password");

The errors I get when I look into my server logs are 当我查看服务器日志时遇到的错误是

[#|2012-03-09T13:50:21.900+0000|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=67;_ThreadName=Thread-1;|First: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver|#] [#| 2012-03-09T13:50:21.900 + 0000 | INFO | glassfish3.1 | javax.enterprise.system.std.com.sun.enterprise.server.logging | _ThreadID = 67; _ThreadName = Thread-1; |第一个:java.lang.ClassNotFoundException:org.gjt.mm.mysql.Driver |#]

[#|2012-03-09T13:50:22.009+0000|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=67;_ThreadName=Thread-1;|java.sql.SQLException: No suitable driver found for jdbc:mysql://xxx.xx.xx.xx.xx/SmarterStudents|#] [#| 2012-03-09T13:50:22.009 + 0000 | INFO | glassfish3.1 | javax.enterprise.system.std.com.sun.enterprise.server.logging | _ThreadID = 67; _ThreadName = Thread-1; | java.sql.SQLException:找不到适用于jdbc的驱动程序:mysql://xxx.xx.xx.xx.xx/SmarterStudents |#]

I have put the mysql-connector-java-5.0.7-bin.jar into the domain/lib folder and threw it into the WEB-INF/lib folder just to be safe and it still wouldn't work for me. 为了安全起见,我已经将mysql-connector-java-5.0.7-bin.jar放入domain / lib文件夹中,并将其扔到WEB-INF / lib文件夹中,但这仍然对我不起作用。

I'm at my wit's end now, I just don't know what to do. 我现在机智了,我只是不知道该怎么办。 D: D:

You're using the old and deprecated driver class name for the very first releases of the MySQL JDBC driver org.gjt.mm.mysql.Driver when it was still a hobby project, while you're using one of the more recent releases of the MySQL JDBC driver which has the driver class name com.mysql.jdbc.Driver . 当您仍在使用MySQL JDBC驱动程序org.gjt.mm.mysql.Driver第一个发行版时,您仍在使用旧的驱动程序类名称,而该版本仍是一个业余项目。 MySQL JDBC驱动程序,其驱动程序类名为com.mysql.jdbc.Driver

Fix the classname accordingly. 相应地修改类名。

Class.forName("com.mysql.jdbc.Driver");

Make sure that you're reading the official MySQL JDBC driver documentation instead of some random and heavily outdated resource/book/tutorial. 确保您正在阅读官方的MySQL JDBC驱动程序文档,而不是阅读一些随机且过时的资源/书/教程。

As to the placement of the JAR file, when you're managing the connections yourself in your web application, the JAR can be placed in both server's own /lib or webapp's /WEB-INF/lib . 至于JAR文件的放置,当您自己在Web应用程序中管理连接时,可以将JAR放置在服务器自己的/lib或webapp的/WEB-INF/lib But when you're letting the server manage the connections (which will usually use a shared connection pool which is way much faster), then the JAR must be placed in server's own /lib folder. 但是当你让服务器管理连接(这通常会使用一个共享的连接池是方式要快得多),然后将JAR必须放在服务器自己的/lib的文件夹。 The one in the deployed webapp(s) will be ignored anyway. 无论如何,已部署的Webapp中的一个将被忽略。

Try working with JdbcOdbcDriver , but generally they say it should be used as a last resort. 尝试使用JdbcOdbcDriver ,但通常他们说应将其作为最后的手段。

try
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:db","root","root");
}catch(Exception e){
     e.printStackTrace();
}

This should work for MySQL database, you need to install the connector for this as well, and the username and password generally for MySQL is root and root, respectively. 这应该适用于MySQL数据库,您还需要为此安装连接器,而MySQL的用户名和密码通常分别为root和root。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM