mysql - 可以从 Eclipse ping MySQL 但不能在 Tomcat 中使用 JSP 连接

标签 mysql eclipse jsp tomcat

我可以从 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/

相关文章:

php - CodeIgniter 和计算数据库中的特定行

php - 什么是 php 中纯全文搜索的快速替代方案?

php - 从多个表中选择并显示到表中

android - 如何使用 Eclipse 将 .apk 文件传输到设备

php - 排名更新问题

java - 在企业应用程序中同时使用 hibernate 和 spring 时是否会创建两个单独的 JVM 实例?

java - 多次尝试后,R无法解析为变量

jsp - 适合初级 JSP 开发人员的良好 JSP 框架

java - Spring Boot - 使用 s 时出现问题 :mvcUrl tag

css - 如何修复 apache-tomcat-8.5.39 中的默认内容类型呈现问题?