java - 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也运行良好。
解决方案
替换e.printStacktrace();
而不是您的幼稚System.out.println();
以查看错误和有关如何解决它们的消息。顺便检查一下连接器版本和 MySQL 版本不匹配。MySQL 8 需要来自 JDBC 连接器 .jar 的版本 8。
检查这个链接
推荐阅读
- python-3.x - 从 scikit 数组转换为 PIL photoimage 的图像被扭曲
- node.js - 无法读取未定义的属性“取消” - 节点计划
- java - gradle compileJava错误:包org.junit.jupiter.api不存在
- javascript - 水平和垂直对齐伪元素的内容
- prometheus - 如何修复 Prometheus 中的“\"INVALID\" is not a valid start token"'
- python - 如何手动绘制巴特沃斯滤波器?
- spring - 如何使用 JPA 存储库从 Entity Manager 复制回滚?
- java - thread.start() 和调用 run 方法之间发生了什么?需要几十毫秒
- python - 为什么 Python 偶尔会用相同的地址初始化空可变对象?
- php - 教义如何合并不完整的数据