javaweb实现数据库增删改查的小项目代码
时间: 2023-08-30 08:12:29 浏览: 116
java代码-使用java解决数据库的增删改查的源代码
5星 · 资源好评率100%
很抱歉,作为AI语言模型,我无法提供完整的JavaWeb项目代码。但是,我可以给你一些指导,帮助你实现JavaWeb项目中的增删改查功能。
下面是一个简单的示例代码,实现了一个简单的学生信息管理系统,包括增加学生信息、删除学生信息、修改学生信息和查询学生信息等功能。
首先是数据库连接的代码:
```java
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
接下来是学生信息的实体类:
```java
public class Student {
private int id;
private String name;
private int age;
private String address;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
```
然后是增加学生信息的Servlet代码:
```java
public class AddStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String address = request.getParameter("address");
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into student(id, name, age, address) values (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.setString(2, name);
stmt.setInt(3, age);
stmt.setString(4, address);
stmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
}
}
```
接下来是删除学生信息的Servlet代码:
```java
public class DeleteStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "delete from student where id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
}
}
```
然后是修改学生信息的Servlet代码:
```java
public class EditStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String address = request.getParameter("address");
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "update student set name=?, age=?, address=? where id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setInt(2, age);
stmt.setString(3, address);
stmt.setInt(4, id);
stmt.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
}
}
```
最后是查询学生信息的Servlet代码:
```java
public class QueryStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> studentList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from student";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setAddress(rs.getString("address"));
studentList.add(student);
}
request.setAttribute("studentList", studentList);
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
}
}
```
以上代码仅供参考,具体实现可以根据自己的需求进行修改和扩展。
阅读全文