我正在尝试将信息从注册表单页面存储到相应的数据库,但代码抛出异常,如“无法连接到数据库”所述,我是初学者,很难弄清楚出了什么问题在这。有人可以帮忙吗?
<%@page import="java.sql.*"%>
<html>
<head>
<meta charset="utf-8">
<title>Sign Up</title>
<link rel="stylesheet" type="text/css" href="reg.css">
</head>
<body>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String date = request.getParameter("date");
String sex = request.getParameter("sex");
Connection con = null;
PreparedStatement ps = null;
String connectionURL = "jdbc:mysql://localhost:3306/table";
String driverName = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
Class.forName(driverName).newInstance();
try {
con = DriverManager.getConnection(connectionURL, user, pass);
String queryString = "INSERT INTO detail(name,password,email,date,sex) VALUES (?,?,?,?,?)";
ps = con.prepareStatement(queryString);
ps.setString(1, name);
ps.setString(2, password);
ps.setString(3, email);
ps.setString(4, date);
ps.setString(5, sex);
int updateQuery = ps.executeUpdate();
if (updateQuery != 0) {
out.println("Successful Registration");
}
}
catch (Exception ex) {
out.println("Unable to connect to database.");
}
finally {
// close all the connections.
ps.close();
con.close();
}
%>
</body>
</html>
最佳答案
- 您的项目中是否包含了驱动程序 jar 文件?
- 在
String connectionURL = "jdbc:mysql://localhost:3306/table";
中,table 是您要连接的数据库名称吗? 在
catch (Exception ex) { out.println("Unable to connect to database."); }
包含 ex.printStackTrace()
这将显示确切的错误。
关于mysql - 使用jsp插入mysql数据库抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49558779/