【发布时间】:2018-01-02 01:51:07
【问题描述】:
我正在尝试从 eclipse 中的 jsp 连接到数据库,以构建网站。我知道 .java 文件的连接工作正常。我添加了 mysql 连接器和所有内容,但我不知道为什么它无法连接。这是我的代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>
</head>
<body>
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/e-health?useSSL=false","root","root");
System.out.println("Connection successful");
out.println("Connection successful");
}
catch(Exception e){
System.out.println("Connection failed");
}
out.println("Loged in, connected to the DB");
%>
<a href = "welcome.html">Welcome</a><br>
<a href= "index.html">Home</a>
</body>
</html>
【问题讨论】:
-
您遇到的错误是什么?您可能缺少应该在 localhost 之后连接的端口号
-
@user1063108 连接失败 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 。这是错误。但我在引用的库中有 mysql 连接器。
标签: java html mysql eclipse jsp