public User findUser(String u) throws Exception { Connection conn = null; // 更新数据库连接信息 Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/bookmanagement"; String username = "root"; String password1 = "password"; // 连接数据库 conn = DriverManager.getConnection(url, username, password1); String sql = "select * from bookmanagement.user where account=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, u); ResultSet rs = pstmt.executeQuery(); rs.beforeFirst(); User user=null; if(rs.next()) { String sid=rs.getString("sid"); String name=rs.getString("name"); String sex=rs.getString("sex"); String major=rs.getString("major"); int lend=rs.getInt("lend"); String account=rs.getString("account"); String password=rs.getString("password"); user=new User(sid,name,sex,major,lend,account,password); } JdbcUtil.free(rs, pstmt, conn); return user; }结合代码和以上错误找出错误原因
时间: 2024-04-28 12:27:08 浏览: 134
根据代码和错误报告,我认为可能的原因是数据库连接出现了问题,导致在执行 SQL 查询时出现异常。具体地说,可能是以下原因之一:
1. 数据库连接信息不正确,例如数据库 URL、用户名或密码不正确。
2. 数据库不存在或不可用。
3. SQL 查询语句中的参数不正确,例如在第86行中传入的参数可能为空值或不合法。
为了找到问题的确切原因,您可以在 UserDao 类中添加一些调试语句,例如打印出 SQL 查询语句或捕获并打印出任何异常。您还可以检查数据库连接是否正确关闭,是否有任何资源泄漏。
相关问题
在Eclipse中创建了Dynamic Web Project项目,项目名称为login1,请编写一个简单的学生信息管理系统需要连接MySQL数据库,请把哪个文件和创在哪和哪个文件所有代码发给我
你需要创建以下文件和代码:
1. 在WebContent文件夹下创建index.jsp文件,用于展示学生信息管理系统的主页面。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<a href="login.jsp">登录</a>
</body>
</html>
```
2. 在WebContent文件夹下创建login.jsp文件,用于用户登录。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
<label for="username">用户名:</label>
<input type="text" name="username"><br>
<label for="password">密码:</label>
<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
3. 在src文件夹下创建com.login1.dao包,用于存放数据库操作相关的类。在该包下创建StudentDao类,用于操作学生信息表。
```java
package com.login1.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.login1.entity.Student;
public class StudentDao {
private Connection conn;
private void getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/login1";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
}
public List<Student> findAll() throws SQLException {
List<Student> list = new ArrayList<Student>();
getConnection();
String sql = "SELECT * FROM student";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setMajor(rs.getString("major"));
list.add(student);
}
rs.close();
pstmt.close();
conn.close();
return list;
}
public Student findByName(String name) throws SQLException {
Student student = null;
getConnection();
String sql = "SELECT * FROM student WHERE name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setMajor(rs.getString("major"));
}
rs.close();
pstmt.close();
conn.close();
return student;
}
}
```
4. 在src文件夹下创建com.login1.entity包,用于存放实体类。在该包下创建Student类,用于封装学生信息。
```java
package com.login1.entity;
public class Student {
private int id;
private String name;
private int age;
private String major;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
5. 在src文件夹下创建com.login1.servlet包,用于存放Servlet类。在该包下创建LoginServlet类,用于处理用户登录请求。
```java
package com.login1.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.login1.dao.StudentDao;
import com.login1.entity.Student;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
StudentDao studentDao = new StudentDao();
try {
Student student = studentDao.findByName(username);
if (student == null) {
request.setAttribute("msg", "用户名不存在");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} else if (!password.equals("123456")) {
request.setAttribute("msg", "密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} else {
request.getSession().setAttribute("student", student);
response.sendRedirect("list.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
request.setAttribute("msg", "系统错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
```
6. 在WebContent文件夹下创建list.jsp文件,用于展示学生信息列表。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息列表</title>
</head>
<body>
<h1>学生信息列表</h1>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>专业</th>
</tr>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.major}</td>
</tr>
</c:forEach>
</table>
<p><a href="logout.jsp">退出登录</a></p>
</body>
</html>
```
7. 在WebContent文件夹下创建logout.jsp文件,用于处理用户退出登录请求。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>退出登录</title>
</head>
<body>
<%
request.getSession().invalidate();
response.sendRedirect("index.jsp");
%>
</body>
</html>
```
以上就是一个简单的学生信息管理系统的所有代码,其中需要连接MySQL数据库的部分在StudentDao类中实现。你需要自行创建MySQL数据库,并在getConnection()方法中修改url、user和password变量的值以连接到你的数据库。
((1)创建t_users表,字段为id、name、password.email、birthday; (2)创建User类,属性与数据库的七users表中字段一一对应,用于封装数据; 3)使用JDBC相关接口和类实现数据库中数据的查询; (4)创建Servlet程
序实现以下功能:
- 注册:将用户提交的信息插入到t_users表中;
- 登录:根据用户输入的用户名和密码查询t_users表中是否存在对应的记录;
- 修改密码:更新t_users表中对应用户的密码字段;
- 查询用户信息:根据用户输入的用户名查询t_users表中对应的记录,并返回给前端页面。
1. 创建t_users表,字段为id、name、password、email、birthday
```sql
CREATE TABLE t_users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
birthday DATE NOT NULL
);
```
2. 创建User类,属性与数据库的t_users表中字段一一对应,用于封装数据
```java
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
// 构造方法、getters、setters
}
```
3. 使用JDBC相关接口和类实现数据库中数据的查询
```java
public class UserDao {
private Connection conn;
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
// 根据用户名和密码查询用户信息
public User findUser(String name, String password) {
User user = null;
try {
String sql = "SELECT * FROM t_users WHERE name=? AND password=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
// 将用户信息插入到t_users表中
public void addUser(User user) {
try {
String sql = "INSERT INTO t_users(name, password, email, birthday) VALUES(?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setDate(4, user.getBirthday());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 根据用户名查询用户信息
public User findUserByName(String name) {
User user = null;
try {
String sql = "SELECT * FROM t_users WHERE name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
// 更新用户密码
public void updatePassword(User user) {
try {
String sql = "UPDATE t_users SET password=? WHERE name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getName());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
4. 创建Servlet程序实现以下功能:
- 注册:将用户提交的信息插入到t_users表中;
- 登录:根据用户输入的用户名和密码查询t_users表中是否存在对应的记录;
- 修改密码:更新t_users表中对应用户的密码字段;
- 查询用户信息:根据用户输入的用户名查询t_users表中对应的记录,并返回给前端页面。
```java
public class UserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("register")) {
register(request, response);
} else if (action.equals("login")) {
login(request, response);
} else if (action.equals("changepassword")) {
changePassword(request, response);
} else if (action.equals("finduser")) {
findUser(request, response);
}
}
// 注册
private void register(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
String email = request.getParameter("email");
String birthday = request.getParameter("birthday");
User user = new User();
user.setName(name);
user.setPassword(password);
user.setEmail(email);
user.setBirthday(Date.valueOf(birthday));
userDao.addUser(user);
response.sendRedirect("success.jsp");
}
// 登录
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = userDao.findUser(name, password);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("fail.jsp");
}
}
// 修改密码
private void changePassword(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = new User();
user.setName(name);
user.setPassword(password);
userDao.updatePassword(user);
response.sendRedirect("success.jsp");
}
// 查询用户信息
private void findUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
User user = userDao.findUserByName(name);
request.setAttribute("user", user);
request.getRequestDispatcher("userinfo.jsp").forward(request, response);
}
public void destroy() {
userDao.close();
}
}
```
阅读全文