我刚开始使用JSP。我开始写一些简单的程序来显示日期和系统信息。然后我尝试连接一个MySQL database --我有一个免费的托管帐户,但是我无法连接到MySQL数据库。这是我的代码:
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status</h1>
<%
try {
String connectionURL = "jdbc:mysql://mysql2.000webhost.com/a3932573_product";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "a3932573_dibya", "******");
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.");
}
%>
</font>
</body>
</html>当连接状态无法连接到数据库时,我收到消息。我使用相同的用户名、密码和数据库名使用PHP测试了这个连接。我在哪里搞错了?
发布于 2012-11-22 04:54:31
原因是驱动程序没有加载到库中,它没有在连接中实例化,因此连接失败:
try {
String connectionURL = "jdbc:mysql://host/db";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "username", "password");
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"+ex);
} 发布于 2014-09-18 16:00:52
我也有同样的问题。我很确定您的程序有什么问题:您没有将.jar添加到web中。复制它并粘贴到WEB/lib中。
很抱歉没有使用正确的格式发布答案(我是新来的,这是我的第一个anwser:( )
发布于 2012-11-22 04:58:06
https://stackoverflow.com/questions/13505833
复制相似问题