【问题标题】:Can ping MySQL from Eclipse but can't connect using JSP in Tomcat可以从 Eclipse ping MySQL,但无法在 Tomcat 中使用 JSP 进行连接
【发布时间】:2015-03-28 09:15:33
【问题描述】:

我可以从 Eclipse 数据源资源管理器连接到 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 中的 URL 完全相同。

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

任何想法为什么我无法从 JSP 连接或在哪里可以找到更多详细信息(Tomcat 日志)?

【问题讨论】:

    标签: mysql eclipse jsp tomcat


    【解决方案1】:

    您在 . 连接连接 = 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.");
    }
    %>
    

    【讨论】:

    • 感谢您的想法。我试过"Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:8889/LenderDetails", "root", "root"); 但还是连接不上。
    • 你遇到了什么问题?
    • 您好,Praveen,感谢您的跟进。我通过将 mysql-connector-java-5.1.24-bin.jar 包含到项目的部署程序集中修复了连接问题。
    【解决方案2】:

    通过将 mysql-connector-java-5.1.24-bin.jar 包含到项目的部署程序集中解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2014-10-16
      • 1970-01-01
      • 2011-03-30
      • 2020-10-12
      • 1970-01-01
      • 2020-04-27
      • 2011-10-26
      • 1970-01-01
      相关资源
      最近更新 更多