JSP Web应用程序数据库连接无法加载

时间:2019-03-02 12:42:02

标签: java mysql jsp tomcat jdbc

我做了一个简单的MySQL连接类和JSP,但是它不起作用。

不要给我任何东西,没有错误,没有结果!

Java类:

package com.indemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestConn {
    public static Connection getConnection(){
           Connection con = null ; 
          try {
              Class.forName("com.mysql.jdbc.Driver");
              con = DriverManager.getConnection("jdbc:mysql://localhost:8012/demojade","root"," ");
              return con ; 
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }
}

JSP页面:

<%@page import="java.sql.Connection" %>
<%@page import="com.indemo.TestConn" %>
<%@page import="java.sql.DriverManager" %>

<%@ page language="java" contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"%>
<!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=windows-1256">
<title>Insert title here</title>
</head>
<body>
<%
      TestConn db = new TestConn(); 
      Connection conn = db.getConnection(); 

      if(conn != null){
          out.println("Connect");
      }else{
          out.println("Nothing");       }


%>
</body>
</html>

我知道了:

enter image description here

tomcat运行良好,当Eclipse控制台未出现任何错误时,xampp也能正常工作。

1 个答案:

答案 0 :(得分:1)

替换e.printStacktrace();而非您的幼稚System.out.println(); 查看错误以及有关如何解决它们的消息。顺便说说, 检查连接器版本和MySQL版本不匹配。 MySQL 8需求 来自JDBC连接器.jar的版本8。 check this link