【问题标题】:Cannot connect to mySQL DB from jsp in Eclipse无法从 Eclipse 中的 jsp 连接到 mySQL DB
【发布时间】: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


【解决方案1】:

在程序顶部导入包

<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Connection"%>

【讨论】:

  • 我也试过了。问题是我必须在同一个文件夹中同时拥有 .html 和 .jsp 文件
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2019-09-01
  • 1970-01-01
  • 2019-06-08
  • 2021-09-16
  • 2016-06-05
相关资源
最近更新 更多