[英]Cannot get JDBC driver to work in tomcat jsp
I try to make a simple example connection to a mysql database in jsp running on tomcat. 我尝试在tomcat上运行的jsp中建立到mysql数据库的简单示例连接。
Running the code below I get the response: No suitable driver found for jdbc:mysql://localhost/TestingDB 运行下面的代码,我得到响应:没有为jdbc找到合适的驱动程序:mysql:// localhost / TestingDB
<HTML>
<HEAD><TITLE>Testing mysql</TITLE></HEAD>
<BODY>
<%@ page import = "java.sql.*"%>
<%
String url = "jdbc:mysql://localhost/TestingDB";
String user = "mysql";
String password = "pwd";
try {
Connection con = DriverManager.getConnection(url, user, password);
out.println("Success");
Statement st = null;
ResultSet rs = null;
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM TestTable");
while(rs.next()){
out.println(rs.getString("String1"));
}
} catch (Exception e) {
out.println(e.getMessage());
e.printStackTrace();
}
%>
</BODY>
</HTML>
I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory and when it did not work I tried to put it in the {appname}/WEB-INF/lib directory. 我已经将mysql-connector-java-5.1.xx-bin.jar放在CATALINA_HOME / lib目录中,当它不起作用时,我尝试将其放在{appname} / WEB-INF / lib目录中。
I have restarted tomcat between each try using services tomcat restart . 我已经在每次尝试之间使用服务 重新启动了 tomcat 。
There should not be any problems accessing the database as it works fine using (pretty much) the same code in a standalone java class. 访问数据库应该没有任何问题,因为在独立的Java类中使用(几乎)相同的代码可以正常工作。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
class JDBCTest {
private static final String url = "jdbc:mysql://localhost/TestingDB?useSSL=false";
private static final String user = "mysql";
private static final String password = "pwd";
public static void main(String args[]) {
try {
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Success");
Statement st = null;
ResultSet rs = null;
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM TestTable");
while(rs.next()){
System.out.println(rs.getString("String1"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I'm really running out of ideas. 我的想法真的耗尽了。 All help is welcome!
欢迎所有帮助!
I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory
我已经将mysql-connector-java-5.1.xx-bin.jar放在CATALINA_HOME / lib目录中
That should work, but you would have to restart Tomcat before testing it. 那应该可以,但是您必须在测试之前重新启动Tomcat。
and when it did not work I tried to put it in the {appname}/WEB-INF/ directory.
当它不起作用时,我尝试将其放在{appname} / WEB-INF /目录中。
Wrong. 错误。 Should be the
{appname}/WEB-INF/lib
directory. 应该是
{appname}/WEB-INF/lib
目录。
Howver you should be defining this database as a Resource in your META-INF/context.xml and getting it from Tomcat via a JNDI lookup on the java:comp/env/jdbc
namespace. 无论如何,您应该在META-INF / context.xml中将此数据库定义为资源,然后通过
java:comp/env/jdbc
名称空间上的JNDI查找从Tomcat获取它。 What you're doing is poor practice. 您正在做的是不好的练习。
You forgot to put the port in the URL access. 您忘记将端口放入URL访问。
Here is a simple example for a connection URL: 这是连接URL的简单示例:
jdbc:mysql://localhost: 3306 /TestingDB; jdbc:mysql:// localhost: 3306 / TestingDB;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.