簡體   English   中英

可以從Eclipse ping MySQL,但無法在Tomcat中使用JSP進行連接

[英]Can ping MySQL from Eclipse but can't connect using JSP in Tomcat

我可以從Eclipse DataSource Explorer連接到MySQL數據庫(ping成功),但是從Eclipse在Tomcat服務器中運行下面的代碼無法連接到DB。 我收到“無法連接..”消息。

<%@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中的URL完全相同。

Eclipse Luna(4.4.2),Tomcat V7.0,mysql-connenctor-java-5.1.24-bin.jar,Java SE 7。

有什么想法為什么我無法從JSP連接或在哪里可以找到更多詳細信息(Tomcat日志)?

您在中有問題。 連接連接= 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-connector-java-5.1.24-bin.jar包含在項目的Deployment Assembly中,可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM