首页 > 解决方案 > JSP web应用程序数据库连接无法加载

问题描述

我做了一个简单的 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>

我懂了 :

在此处输入图像描述

当eclipse控制台没有给出任何错误时,tomcat和xampp也运行良好。

标签: javamysqljsptomcatjdbc

解决方案


替换e.printStacktrace();而不是您的幼稚System.out.println(); 以查看错误和有关如何解决它们的消息。顺便检查一下连接器版本和 MySQL 版本不匹配。MySQL 8 需要来自 JDBC 连接器 .jar 的版本 8。 检查这个链接


推荐阅读