我们可以在JSP中轻松创建CRUD(创建、读取、更新、删除)示例。这里,我们使用DAO文件来处理数据库操作和JSTL来遍历记录。

CRUD示例

Eclipse中的目录结构

8-1.jpg

主要页面和代码

index.jsp

主页面,提供添加用户和查看用户的链接。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP CRUD Example</title>
</head>
<body>
<h1>JSP CRUD Example</h1>
<a href="adduserform.jsp">Add User</a>
<a href="viewusers.jsp">View Users</a>
</body>
</html>

adduserform.jsp

添加用户的表单页面,嵌入了用户表单。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add User Form</title>
</head>
<body>
<jsp:include page="userform.html"></jsp:include>
</body>
</html>

userform.html

用户数据的具体输入表单。

<a href="viewusers.jsp">View All Records</a><br/>
<h1>Add New User</h1>
<form action="adduser.jsp" method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"/></td></tr>
<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>
<tr><td>Sex:</td><td><input type="radio" name="sex" value="male"/>Male
<input type="radio" name="sex" value="female"/>Female</td></tr>
<tr><td>Country:</td><td><select name="country" style="width:155px">
<option>India</option><option>Pakistan</option><option>Afghanistan</option>
<option>Berma</option><option>Other</option></select></td></tr>
<tr><td colspan="2"><input type="submit" value="Add User"/></td></tr>
</table>
</form>

UserDao.java

负责数据库操作的DAO类,包括增加、删除、更新和获取用户。

package cn.javatiku.dao;
import cn.javatiku.bean.User;
import java.sql.*;

public class UserDao {
  // 连接数据库
  public static Connection getConnection(){
    Connection con = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "", "");
    } catch(Exception e) { System.out.println(e); }
    return con;
  }

  // 保存用户
  public static int save(User u){
    int status = 0;
    try {
      Connection con = getConnection();
      PreparedStatement ps = con.prepareStatement("insert into register(name,password,email,sex,country) values(?,?,?,?,?)");
      ps.setString(1, u.getName());
      ps.setString(2, u.getPassword());
      ps.setString(3, u.getEmail());
      ps.setString(4, u.getSex());
      ps.setString(5, u.getCountry());
      status = ps.executeUpdate();
    } catch(Exception e) { System.out.println(e); }
    return status;
  }

  // 更新用户
  public static int update(User u){
    int status = 0;
    try {
      Connection con = getConnection();
      PreparedStatement ps = con.prepareStatement("update register set name=?,password=?,email=?,sex=?,country=? where id=?");
      ps.setString(1, u.getName());
      ps.setString(2, u.getPassword());
      ps.setString(3, u.getEmail());
      ps.setString(4, u.getSex());
      ps.setString(5, u.getCountry());
      ps.setInt(6, u.getId());
      status = ps.executeUpdate();
    } catch(Exception e) { System.out.println(e); }
    return status;
  }

  // 删除用户
  public static int delete(User u){
    int status = 0;
    try {
      Connection con = getConnection();
      PreparedStatement ps = con.prepareStatement("delete from register where id=?");
      ps.setInt(1, u.getId());
      status = ps.executeUpdate();
    } catch(Exception e) { System.out.println(e); }
    return status;
  }

  // 获取所有用户记录
  public static List<User> getAllRecords(){
    List<User> list = new ArrayList<User>();
    try {
      Connection con = getConnection();
      PreparedStatement ps = con.prepareStatement("select * from register");
      ResultSet rs = ps.executeQuery();
      while(rs.next()){
        User u = new User();
        u.setId(rs.getInt("id"));
        u.setName(rs.getString("name"));
        u.setPassword(rs.getString("password"));
        u.setEmail(rs.getString("email"));
        u.setSex(rs.getString("sex"));
        u.setCountry(rs.getString("country"));
        list.add(u);
      }
    } catch(Exception e) { System.out.println(e); }
    return list;
  }

  // 根据ID获取单条记录
  public static User getRecordById(int id){
    User u = null;
    try {
      Connection con = getConnection();
      PreparedStatement ps = con.prepareStatement("select * from register where id=?");
      ps.setInt(1, id);
      ResultSet rs = ps.executeQuery();
      while(rs.next()){
        u = new User();
        u.setId(rs.getInt("id"));
        u.setName(rs.getString("name"));
        u.setPassword(rs.getString("password"));
        u.setEmail(rs.getString("email"));
        u.setSex(rs.getString("sex"));
        u.setCountry(rs.getString("country"));
      }
    } catch(Exception e) { System.out.println(e); }
    return u;
  }
}

结果

8-2.jpg
8-3.jpg
8-4.jpg
8-5.jpg
8-6.jpg
8-7.jpg
8-8.jpg

标签: JSP, JSP教程, JSP技术, JSP快速入门, JSP简单用法, JSP编程, JSP下载, JSP基本语法, JSP安装教程, JSP库, JSP指南, JSP基础教程, JSP初级教程, JSP进阶教程