我可以从 Eclipse DataSource Explorer 连接到 MySQL 数据库(ping 成功)但是从 Eclipse 在 Tomcat 服务器中运行下面的代码不会连接到数据库。我收到“无法连接..”消息。
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:8889/LenderDetails", "root", "root");
out.println("connection ="+connection.toString());
if(!connection.isClosed())
%>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
<%
out.println("Unable to connect to database.");
}
%>
JSP 代码中的 URL 与 DataSourceExplorer 中的完全相同。
Eclipse Luna (4.4.2)、Tomcat V7.0、mysql-connenctor-java-5.1.24-bin.jar、Java SE 7。
为什么我无法从 JSP 连接或在哪里可以找到更多详细信息(Tomcat 日志)?
最佳答案
您在 .
连接 connection = DriverManager.getConnection("com.mysql.jdbc:mysql://localhost:8889/LenderDetails", "root", "root
");
只需替换为 .
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:8889/LenderDetails", "root", "root");
您的代码将如下所示。
%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:8889/LenderDetails", "root", "root");
out.println("connection ="+connection.toString());
if(!connection.isClosed())
%>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
<%
out.println("Unable to connect to database.");
}
%>
关于mysql - 可以从 Eclipse ping MySQL 但不能在 Tomcat 中使用 JSP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315464/