Skip to content

Java Bean

前端-后端:

  • 前端提交的参数一般是面向过程的基本类型,后端操作时需要注入到对象中

后端-数据库:

  • 关系数据库在企业级应用开发中占主导地位,而主要的后端程序开发语言都是面向对象的,关系与对象之间就需要进行双向转换

组件开发


public int create(String no, String name, int age) {
  int res = 0;

  Connection con = null;
  PreparedStatement pstmt = null;
  String sql = "insert into Student values (?, ?, ?)";
  con = ConnectionUtil.getConnection();
  if (o != null && con != null) {
    try {
      pstmt = con.prepareStatement(sql);
      pstmt.setString(1, no);
      pstmt.setString(2, name);
      pstmt.setInt(3, age);
      res = pstmt.executeUpdate();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ConnectionUtil.closePstmt(pstmt);
      ConnectionUtil.closeConnection(con);
    }
  }

  return res;
}

JavaBean介绍

JavaBean的来源

  • Bean:豆子

  • JavaBean是应用程序的组成部分

JavaBean的分类

  • 封装数据

  • 封装业务方法


Getter&Setter

Getter&setter方法用于访问 JavaBean 的属性

java bean

xust.demo.stu.domain.Student
/**
 * 学号
 */
private String no;
public String getNo(){
    return no;
}

public void setNo(String newValue){
    no = newValue;
}
源代码
xust.demo.stu.domain.Student
package xust.demo.stu.domain;

/**
 * Class Student
 * 学生表
 * @author XUST
 * @version 1.0, 2023-04-20
 */
public class Student{


    /**
     * 学号
     */
    private String no;
    public String getNo(){
        return no;
    }

    public void setNo(String newValue){
        no = newValue;
    }
    /**
     * 姓名
     */
    private String name;
    public String getName(){
        return name;
    }

    public void setName(String newValue){
        name = newValue;
    }
    /**
     * 性别
     */
    private String gender;
    public String getGender(){
        return gender;
    }

    public void setGender(String newValue){
        gender = newValue;
    }
    /**
     * 年龄
     */
    private Integer age;
    public Integer getAge(){
        return age;
    }

    public void setAge(Integer newValue){
        age = newValue;
    }
    /**
     * 所在系
     */
    private String dept;
    public String getDept(){
        return dept;
    }

    public void setDept(String newValue){
        dept = newValue;
    }

    @Override
    public String toString() {
    return "{" + "\"no\": " + "\"" + no + "\"" + ", " + "\"name\": " + "\"" + name + "\"" + ", " + "\"gender\": " + "\"" + gender + "\"" + ", " + "\"age\": " + age + ", " + "\"dept\": " + "\"" + dept + "\"" +  "}";
    }
}

数据封装案例

xust.demo.stu.domain.Student
/**
 * 学号
 */
private String no;
public String getNo(){
    return no;
}

public void setNo(String newValue){
    no = newValue;
}
源代码
xust.demo.stu.domain.Student
package xust.demo.stu.domain;

/**
 * Class Student
 * 学生表
 * @author XUST
 * @version 1.0, 2023-04-20
 */
public class Student{


    /**
     * 学号
     */
    private String no;
    public String getNo(){
        return no;
    }

    public void setNo(String newValue){
        no = newValue;
    }
    /**
     * 姓名
     */
    private String name;
    public String getName(){
        return name;
    }

    public void setName(String newValue){
        name = newValue;
    }
    /**
     * 性别
     */
    private String gender;
    public String getGender(){
        return gender;
    }

    public void setGender(String newValue){
        gender = newValue;
    }
    /**
     * 年龄
     */
    private Integer age;
    public Integer getAge(){
        return age;
    }

    public void setAge(Integer newValue){
        age = newValue;
    }
    /**
     * 所在系
     */
    private String dept;
    public String getDept(){
        return dept;
    }

    public void setDept(String newValue){
        dept = newValue;
    }

    @Override
    public String toString() {
    return "{" + "\"no\": " + "\"" + no + "\"" + ", " + "\"name\": " + "\"" + name + "\"" + ", " + "\"gender\": " + "\"" + gender + "\"" + ", " + "\"age\": " + age + ", " + "\"dept\": " + "\"" + dept + "\"" +  "}";
    }
}
xust.demo.stu.dao.StudentDaoImpl.java
public Result create(Student o) {
  Result res = new Result(false);

  Connection con = null;
  PreparedStatement stmt = null;
  String sql = "insert into Student values (?, ?, ?, ?, ?)";
  con = ConnectionUtil.getConnection();
  if (o != null && con != null) {
    try {
      stmt = con.prepareStatement(sql);
      toR(o, stmt);
      if(stmt.executeUpdate() > 0){
        res.code = 0;
      }
    } catch (Exception e) {
      res.message = e.getMessage();
      e.printStackTrace();
    } finally {
      ConnectionUtil.closePstmt(stmt);
      ConnectionUtil.closeConnection(con);
    }
  }

  return res;
}
源代码
xust.demo.stu.dao.StudentDaoImpl.java
package xust.demo.stu.dao;

import java.sql.*;
import java.util.ArrayList;
import xust.stu.Result;
import xust.stu.ConnectionUtil;
import xust.demo.stu.domain.Student;

public class StudentDaoImpl implements StudentDao{
  public Result init() {
    Result res = new Result(false);

    Connection con = null;
    Statement stmt = null;
    String sql = "create table Student(" +
      "no TEXT primary key," +
      "name TEXT not null," +
      "gender TEXT," +
      "age INTEGER," +
      "dept TEXT" +
      ");";

    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'", "Student".toUpperCase()));
        while(rs.next()){
          int count = rs.getInt(1);

          if(count == 0 && stmt.executeUpdate(sql) > 0){
            res.code = 0;
          }          
        }
      } catch (Exception e) {
        e.printStackTrace();
        res.message = e.getMessage();
      } finally {
        ConnectionUtil.closeStmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  /**
   * 对象填充到关系
   */
  private void toR(Student o, PreparedStatement stmt) throws Exception{
    if(o != null && stmt != null){
      String no = o.getNo();
      if(no != null){
        stmt.setString(1, no);       
      }
      String name = o.getName();
      if(name != null){
        stmt.setString(2, name);       
      }
      String gender = o.getGender();
      if(gender != null){
        stmt.setString(3, gender);       
      }
      Integer age = o.getAge();
      if(age != null){
        stmt.setInt(4, age);       
      }
      String dept = o.getDept();
      if(dept != null){
        stmt.setString(5, dept);       
      }
    }
  }

  /**
   * 关系填充到对象
   */
  private Student toO(ResultSet rs) throws Exception{
    Student res = null;

    if(rs != null){
      res = new Student();
      res.setNo(rs.getString("no"));
      res.setName(rs.getString("name"));
      res.setGender(rs.getString("gender"));
      res.setAge(rs.getInt("age"));
      res.setDept(rs.getString("dept"));
    }

    return res;
  }

  public Result create(Student o) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "insert into Student values (?, ?, ?, ?, ?)";
    con = ConnectionUtil.getConnection();
    if (o != null && con != null) {
      try {
        stmt = con.prepareStatement(sql);
        toR(o, stmt);
        if(stmt.executeUpdate() > 0){
          res.code = 0;
        }
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }
    }

    return res;
  }

  public Result delete(String no) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "delete from Student where no = ?;";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {

        stmt = con.prepareStatement(sql);
        stmt.setString(1, no);
        res.code = 0;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  public Result update(Student o) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";
    con = ConnectionUtil.getConnection();
    if (o != null && con != null) {
      try {

        stmt = con.prepareStatement(sql);
        toR(o, stmt);
        stmt.setString(6, o.getNo());
        if(stmt.executeUpdate() > 0){
          res.code = 0;
        }
      } catch (Exception e) {
        e.printStackTrace();
        res.message = e.getMessage();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }
    }

    return res;
  }

  public Result get(String no) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "select * from Student where no = ?";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.prepareStatement(sql);
        stmt.setString(1, no);
        ResultSet rs = stmt.executeQuery();
        Student last = null;
        while (rs.next()) {
          last = new Student();
          last.setNo(rs.getString("no"));
          last.setName(rs.getString("name"));
          last.setGender(rs.getString("gender"));
          last.setAge(rs.getInt("age"));
          last.setDept(rs.getString("dept"));
        }

        res.code = 0;
        res.data = last;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  public Result getAll() {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "select * from Student";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        ArrayList<Student> data = new ArrayList<Student>();
        while (rs.next()) {
          Student last = toO(rs);
          data.add(last);
        }

        res.code = 0;
        res.data = data;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }
}

封装前端输入

广义讲,符合规范的Java类都是JavaBean

JavaBean的作用

  • 封装数据
  • 处理业务逻辑

JSP与JavaBean如何结合使用呢?

  • Scriptlet

  • JSP标准动作

访问JavaBean步骤


访问JavaBean基本语法

  1. 导入JavaBean类

    • <%@ page import="类名"%>
  2. 实例化JavaBean类

1
2
3
<jsp:useBean id="Name" scope="page|request|session|appliction" 类型模式>
//内容实例…
</jsp:useBean>

Id

  • 创建JavaBean对象实例的名称。

Scope

  • 创建JavaBean对象实例的作用域。
范围 描述
Page Bean 只能在使用页面时使用。
Request Bean 在用户对其发出请求时存在
Session Bean一直存在于会话中,直至其终止或被删除为止
Application Bean 在整个应用程序中均可使用

类型模式

  • 对象所属的类或类型

    • class="类名"

    • class="类名" type="类型名"

    • beanName="bean名字" type="类型名"

    • type="类型名"

JavaBean执行过程


jsp:setProperty/jsp:setProperty

为指定JavaBean对象(bean)设置属性值。

<jsp:setProperty name="bean名称" 属性表达式/>

参数含义

  • Name

    • 指明JavaBean实例对象的名字
  • 设置具体的属性

    • Perperty="*":为已经创建的bean的所有属性设置或修改属性值

    • Peroperty="属性名":为指定的的属性设置或修改属性值

    • Property="属性值" param="参数名" :为指定属性设置或修改属性值

    • Property="属性名" value="字符串|<%=表达或%>:设置属性的属性值

获取JavaBean对象实例的属性值<jsp:getProperty name="对象名" property="属性名"/>

Input.jsp
  <form action="Cal.jsp" method="post">
    <table border="1">
      <caption>输入长方体的长、宽、高</caption>
      <tr>
        <td>长度</td>
        <td><input type="text" name="length" /></td>
      </tr>
      <tr>
        <td>宽度</td>
        <td><input type="text" name="width" /></td>
      </tr>
      <tr>
        <td>高度</td>
        <td><input type="text" name="height" /></td>
      </tr>
      <tr>
        <td><input type="submit" value="提交" /></td>
      </tr>
    </table>
  </form>
</body>

</html>

源代码
Input.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
  <!DOCTYPE html>
  <html>

  <head>
    <meta charset="utf-8">
    <title>Insert title here</title>
  </head>

  <body>
    <form action="Cal.jsp" method="post">
      <table border="1">
        <caption>输入长方体的长、宽、高</caption>
        <tr>
          <td>长度</td>
          <td><input type="text" name="length" /></td>
        </tr>
        <tr>
          <td>宽度</td>
          <td><input type="text" name="width" /></td>
        </tr>
        <tr>
          <td>高度</td>
          <td><input type="text" name="height" /></td>
        </tr>
        <tr>
          <td><input type="submit" value="提交" /></td>
        </tr>
      </table>
    </form>
  </body>

  </html>

BeanTest/BeanTest/src/main/webapp/Cal.jsp
  <jsp:useBean id="cuboid1" scope="page" class="beans.CuboidBean" />
  <jsp:setProperty name="cuboid1" property="*" />
  <%
  if (cuboid1.isCuboid()) {
    out.println("CuboidBean的应用一<br>");
    out.println("长=" + cuboid1.getLength());
    out.println("宽=" + cuboid1.getWidth());
    out.println("高=" + cuboid1.getHeight());
    out.println("周长:" + cuboid1.getPerimeter());
    out.println("面积:" + cuboid1.getArea());
    out.println("体积:" + cuboid1.getVolume());
  }
  %>

  <jsp:useBean id="cuboid2" scope="page" class="beans.CuboidBean">
    <jsp:setProperty name="cuboid2" property="width" value="23" />
    <jsp:setProperty name="cuboid2" property="height" />
    <jsp:setProperty name="cuboid2" property="length" />
  </jsp:useBean>
  <%
  if (cuboid2.isCuboid()) {
    out.println("<br><hr>CuboidBean的应用二<br>");
    out.println("长=" + cuboid2.getLength());
    out.println("宽=" + cuboid2.getWidth());
    out.println("高=" + cuboid2.getHeight());
  }
  %>

  <jsp:useBean id="cuboid3" scope="page" class="beans.CuboidBean">
    <jsp:setProperty name="cuboid3" property="width"
      value="<%=cuboid2.getHeight()%>" />
    <jsp:setProperty name="cuboid3" property="height"
      value="<%=cuboid2.getLength()%>" />
    <jsp:setProperty name="cuboid3" property="length"
      value="<%=cuboid2.getWidth()%>" />
  </jsp:useBean>
  <%
  if (cuboid3.isCuboid()) {
    out.println("<br><hr>CuboidBean的应用三->修改CuboidBean的长宽高<br>");
    out.println("长=" + cuboid3.getLength());
    out.println("宽=" + cuboid3.getWidth());
    out.println("高=" + cuboid3.getHeight());
  }
  %>
</body>

</html>

源代码
BeanTest/BeanTest/src/main/webapp/Cal.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>长方体计算</title>
</head>

<body>
  <jsp:useBean id="cuboid1" scope="page" class="beans.CuboidBean" />
  <jsp:setProperty name="cuboid1" property="*" />
  <%
  if (cuboid1.isCuboid()) {
    out.println("CuboidBean的应用一<br>");
    out.println("长=" + cuboid1.getLength());
    out.println("宽=" + cuboid1.getWidth());
    out.println("高=" + cuboid1.getHeight());
    out.println("周长:" + cuboid1.getPerimeter());
    out.println("面积:" + cuboid1.getArea());
    out.println("体积:" + cuboid1.getVolume());
  }
  %>

  <jsp:useBean id="cuboid2" scope="page" class="beans.CuboidBean">
    <jsp:setProperty name="cuboid2" property="width" value="23" />
    <jsp:setProperty name="cuboid2" property="height" />
    <jsp:setProperty name="cuboid2" property="length" />
  </jsp:useBean>
  <%
  if (cuboid2.isCuboid()) {
    out.println("<br><hr>CuboidBean的应用二<br>");
    out.println("长=" + cuboid2.getLength());
    out.println("宽=" + cuboid2.getWidth());
    out.println("高=" + cuboid2.getHeight());
  }
  %>

  <jsp:useBean id="cuboid3" scope="page" class="beans.CuboidBean">
    <jsp:setProperty name="cuboid3" property="width"
      value="<%=cuboid2.getHeight()%>" />
    <jsp:setProperty name="cuboid3" property="height"
      value="<%=cuboid2.getLength()%>" />
    <jsp:setProperty name="cuboid3" property="length"
      value="<%=cuboid2.getWidth()%>" />
  </jsp:useBean>
  <%
  if (cuboid3.isCuboid()) {
    out.println("<br><hr>CuboidBean的应用三->修改CuboidBean的长宽高<br>");
    out.println("长=" + cuboid3.getLength());
    out.println("宽=" + cuboid3.getWidth());
    out.println("高=" + cuboid3.getHeight());
  }
  %>
</body>

</html>

JavaBean属性

属性是JavaBean组件内部状态的抽象表示。

JavaBean属性分为四种类型:

  • Simple(简单)

  • Indexed(索引)

  • Bound(绑定)

  • Constrained(限制)


Simple属性

一个简单属性表示一个带有get/set方法的变量,语法如下:

1
2
3
public void set<PropertyName>(<PropertyType> value);
public <PropertyType> get<PropertyName>();
public boolean is<PropertyName>();

若同时具有get和set方法,则该属性可读可写;若只有一个方法,则该属性为只读或只写。

注意:在定义变量时用小写,如type,但在定义方法时第一个字母要大写,如getType(),setType()

PropertyTest/SimpleTest.java
public class SimpleTest {
  private String ourString="Hello"; //属性名为ourString,类型为字符串

  public String getOurString() {
    return ourString;
  }

  public void setOurString(String ourString) {
    this.ourString = ourString;
  }
}

Indexed属性

一个索引属性表示一个数组,同简单属性类似,可用get/set方法取得数组中的值,语法如下:

1
2
3
4
public void set<PropertyName>(int index, <PropertyType> value);
public <PropertyType> get<PropertyName>(int index);
public void set<PropertyName>(<PropertyType[]> value);
public <PropertyType[]> get<PropertyName>();
PropertyTest/IndexedTest.java
public class IndexedTest {
  private int[] dataSet = { 1, 2, 3, 4, 5, 6 };

  public IndexedTest() {
  }

  public void setDataSet(int[] x) {
    dataSet = x;
  }

  public void setDataSet(int index, int x) {
    dataSet[index] = x;
  }

  public int[] getDataSet() {
    return dataSet;
  }
}

Bound属性

一个Bound属性是指当该种属性的值发生变化时,通过PropertyChange事件通知其他的对象。

PropertyTest/BoundTest.java
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class BoundTest {
  String ourString = "Hello";
  private PropertyChangeSupport ourStringChanges = new PropertyChangeSupport(this);

  public void setString(String newString) {
    String oldString = new String();
    ourStringChanges.firePropertyChange(ourString, oldString, newString);
  }

  public String getString() {
    return ourString;
  }

  public void addOurStringChangeListener(PropertyChangeListener  l) {
    ourStringChanges.addPropertyChangeListener(l);
  }

  public void removeOurStringChangeListener(PropertyChangeListener l) {
    ourStringChanges.removePropertyChangeListener(l);
  }
}

Constrained属性

当这个属性的值要发生改变时,与这个属性建立了某种连接的其他java对象可否决属性值的改变。constrained属性的监听者通过抛出PropertyVectoEcxeption来阻止该属性的改变。

PropertyTest/ConstrainedTest.java
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;

public class ConstrainedTest {
  private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  private VetoableChangeSupport vetos = new VetoableChangeSupport(this);

  private int ourPriceInCents;

  public void setPriceInCents(int newPriceInCents) throws PropertyVetoException {
    int oldPriceInCents = ourPriceInCents;
    vetos.fireVetoableChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents));
    ourPriceInCents = newPriceInCents;
    changes.firePropertyChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents));
  }

  public void addVetosChangeListener(VetoableChangeListener l) {
    vetos.addVetoableChangeListener(l);
  }

  public void removeVetoasChangeListener(VetoableChangeListener l) {
    vetos.removeVetoableChangeListener(l);
  }
}

封装数据存储逻辑

数据库访问流程

JDBC步骤


ORM

对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。ORM框架是连接数据库的桥梁,只要提供了持久化类与表的映射关系,ORM框架在运行时就能参照映射文件的信息,把对象持久化到数据库中。

ORM框架:为了解决面型对象与关系数据库存在的互不匹配的现象的框架。

当前ORM框架主要有五种:

  1. Hibernate 全自动 需要写hql语句

  2. iBATIS 半自动 自己写sql语句,可操作性强,小巧

  3. mybatis

  4. eclipseLink

  5. JFinal

ORM是对象关系映射的简称,主要任务是:

  1. 根据对象的类型生成表结构

  2. 将对象、列表操作,转换成SQL语句(或其他数据库的语句)

  3. 将SQL查询到的结果转换为对象、列表

对象模型 关系模型
类名对应 数据库中的表名
类属性对应  数据库里的字段
类实例对应 数据库表里的一行数据

优点:

  1. 提高开发效率,降低开发成本

  2. 使开发更加对象化

  3. 可移植

  4. 可以很方便地引入数据缓存之类的附加功能

缺点:

  1. 自动化进行关系数据库的映射需要消耗系统性能。其实这里的性能消耗还好啦,一般来说都可以忽略之。

  2. 在处理多表联查、where条件复杂之类的查询时,ORM的语法会变得复杂。


示例

DaoTest/Student.sql
1
2
3
4
5
6
7
create table Student(
    no TEXT primary key,
    name TEXT not null,
    gender TEXT,
    age INTEGER,
    dept TEXT
);
xust.demo.ConnectionUtil.java
/**
 * 创建连接
 * 
 * @return 连接对象
 */
public static Connection getConnection() {
  Connection res = null;

  try {
    Class.forName("org.sqlite.JDBC");
    res = DriverManager.getConnection("jdbc:sqlite:stu.db");
  } catch (Exception e) {
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
    System.exit(0);
  }

  return res;
}
源代码
xust.demo.ConnectionUtil.java
package xust.demo;

import java.sql.*;

/**
 * 连接辅助类
 */
public class ConnectionUtil {
  /**
   * 创建连接
   * 
   * @return 连接对象
   */
  public static Connection getConnection() {
    Connection res = null;

    try {
      Class.forName("org.sqlite.JDBC");
      res = DriverManager.getConnection("jdbc:sqlite:stu.db");
    } catch (Exception e) {
      System.err.println(e.getClass().getName() + ": " + e.getMessage());
      System.exit(0);
    }

    return res;
  }

  /**
   * 关闭连接
   * 
   * @param connection 连接对象
   */
  public static void closeConnection(Connection connection) {
    if (connection != null) {
      try {
        connection.close();
      } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
      }
    }
  }

  /**
   * 关闭语句
   * 
   * @param pstmt 语句对象
   */
  public static void closePstmt(PreparedStatement pstmt) {
    if (pstmt != null) {
      try {
        pstmt.close();
      } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
      }
    }
  }

  /**
   * 关闭语句
   * 
   * @param pstmt 语句对象
   */
  public static void closeStmt(Statement stmt) {
    if (stmt != null) {
      try {
        stmt.close();
      } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
      }
    }
  }
}
xust.demo.stu.domain.Student
public class Student{      
  /**
   * 学号
   */
  private String no;
  public String getNo(){
    return no;
  }

  public void setNo(String newValue){
    no = newValue;
  }
}
源代码
xust.demo.stu.domain.Student
package xust.demo.stu.domain;

/**
 * Class Student
 * 学生表
 * @author XUST
 * @version 1.0, 2023-04-20
 */
public class Student{


    /**
     * 学号
     */
    private String no;
    public String getNo(){
        return no;
    }

    public void setNo(String newValue){
        no = newValue;
    }
    /**
     * 姓名
     */
    private String name;
    public String getName(){
        return name;
    }

    public void setName(String newValue){
        name = newValue;
    }
    /**
     * 性别
     */
    private String gender;
    public String getGender(){
        return gender;
    }

    public void setGender(String newValue){
        gender = newValue;
    }
    /**
     * 年龄
     */
    private Integer age;
    public Integer getAge(){
        return age;
    }

    public void setAge(Integer newValue){
        age = newValue;
    }
    /**
     * 所在系
     */
    private String dept;
    public String getDept(){
        return dept;
    }

    public void setDept(String newValue){
        dept = newValue;
    }

    @Override
    public String toString() {
    return "{" + "\"no\": " + "\"" + no + "\"" + ", " + "\"name\": " + "\"" + name + "\"" + ", " + "\"gender\": " + "\"" + gender + "\"" + ", " + "\"age\": " + age + ", " + "\"dept\": " + "\"" + dept + "\"" +  "}";
    }
}
xust.demo.stu.dao.StudentDao.java
package xust.demo.stu.dao;
import xust.stu.Result;
import xust.demo.stu.domain.Student;

public interface StudentDao {
  /**
   * 初始化表
   * @return
   */
  Result init();

  /**
   * 增
   * @param o
   * @return
   */
  Result create(Student o);

  /**
   * 删
   * @param id
   * @return
   */
  Result delete(String no);

  /**
   * 改
   * @param o
   * @return
   */
  Result update(Student o);

  /**
   * 查
   * @param no
   * @return
   */
  Result get(String no);

  /**
   * 查
   * @return
   */
  Result getAll();
}
源代码
xust.demo.stu.dao.StudentDaoImpl.java
package xust.demo.stu.dao;

import java.sql.*;
import java.util.ArrayList;
import xust.stu.Result;
import xust.stu.ConnectionUtil;
import xust.demo.stu.domain.Student;

public class StudentDaoImpl implements StudentDao{
  public Result init() {
    Result res = new Result(false);

    Connection con = null;
    Statement stmt = null;
    String sql = "create table Student(" +
      "no TEXT primary key," +
      "name TEXT not null," +
      "gender TEXT," +
      "age INTEGER," +
      "dept TEXT" +
      ");";

    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'", "Student".toUpperCase()));
        while(rs.next()){
          int count = rs.getInt(1);

          if(count == 0 && stmt.executeUpdate(sql) > 0){
            res.code = 0;
          }          
        }
      } catch (Exception e) {
        e.printStackTrace();
        res.message = e.getMessage();
      } finally {
        ConnectionUtil.closeStmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  /**
   * 对象填充到关系
   */
  private void toR(Student o, PreparedStatement stmt) throws Exception{
    if(o != null && stmt != null){
      String no = o.getNo();
      if(no != null){
        stmt.setString(1, no);       
      }
      String name = o.getName();
      if(name != null){
        stmt.setString(2, name);       
      }
      String gender = o.getGender();
      if(gender != null){
        stmt.setString(3, gender);       
      }
      Integer age = o.getAge();
      if(age != null){
        stmt.setInt(4, age);       
      }
      String dept = o.getDept();
      if(dept != null){
        stmt.setString(5, dept);       
      }
    }
  }

  /**
   * 关系填充到对象
   */
  private Student toO(ResultSet rs) throws Exception{
    Student res = null;

    if(rs != null){
      res = new Student();
      res.setNo(rs.getString("no"));
      res.setName(rs.getString("name"));
      res.setGender(rs.getString("gender"));
      res.setAge(rs.getInt("age"));
      res.setDept(rs.getString("dept"));
    }

    return res;
  }

  public Result create(Student o) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "insert into Student values (?, ?, ?, ?, ?)";
    con = ConnectionUtil.getConnection();
    if (o != null && con != null) {
      try {
        stmt = con.prepareStatement(sql);
        toR(o, stmt);
        if(stmt.executeUpdate() > 0){
          res.code = 0;
        }
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }
    }

    return res;
  }

  public Result delete(String no) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "delete from Student where no = ?;";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {

        stmt = con.prepareStatement(sql);
        stmt.setString(1, no);
        res.code = 0;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  public Result update(Student o) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";
    con = ConnectionUtil.getConnection();
    if (o != null && con != null) {
      try {

        stmt = con.prepareStatement(sql);
        toR(o, stmt);
        stmt.setString(6, o.getNo());
        if(stmt.executeUpdate() > 0){
          res.code = 0;
        }
      } catch (Exception e) {
        e.printStackTrace();
        res.message = e.getMessage();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }
    }

    return res;
  }

  public Result get(String no) {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "select * from Student where no = ?";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.prepareStatement(sql);
        stmt.setString(1, no);
        ResultSet rs = stmt.executeQuery();
        Student last = null;
        while (rs.next()) {
          last = new Student();
          last.setNo(rs.getString("no"));
          last.setName(rs.getString("name"));
          last.setGender(rs.getString("gender"));
          last.setAge(rs.getInt("age"));
          last.setDept(rs.getString("dept"));
        }

        res.code = 0;
        res.data = last;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }

  public Result getAll() {
    Result res = new Result(false);

    Connection con = null;
    PreparedStatement stmt = null;
    String sql = "select * from Student";
    con = ConnectionUtil.getConnection();
    if(con != null){
      try {
        stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        ArrayList<Student> data = new ArrayList<Student>();
        while (rs.next()) {
          Student last = toO(rs);
          data.add(last);
        }

        res.code = 0;
        res.data = data;
      } catch (Exception e) {
        res.message = e.getMessage();
        e.printStackTrace();
      } finally {
        ConnectionUtil.closePstmt(stmt);
        ConnectionUtil.closeConnection(con);
      }      
    }

    return res;
  }
}
xust.demo.stu.dao.StudentDaoTest.java
package xust.demo.stu.dao;

import java.util.ArrayList;
import xust.stu.Result;
import xust.demo.stu.domain.Student;

public class StudentDaoTest {
  public static void main(String[] args) {
    StudentDao dao = new StudentDaoImpl();
    Result res_init = dao.init();
    System.out.println("init=" + res_init.code);

    Student o1 = new Student();
    o1.setNo("X1");
    o1.setName("X1");
    o1.setGender("X1");
    o1.setAge(1);
    o1.setDept("X1");

    Student o2 = new Student();
    o2.setNo("X2");
    o2.setName("X2");
    o2.setGender("X2");
    o2.setAge(2);
    o2.setDept("X2");
    dao.create(o2);
    Result res_create = dao.create(o1);
    System.out.println("create=" + res_create.code);

    Result res_update = dao.update(o1);
    System.out.println("update=" + res_update.code);

    Result res_delete = dao.delete("X1");
    System.out.println("delete=" + res_delete.code);

    Result res_get = dao.get("X2");

    Result res_getAll = dao.getAll();
    if(res_getAll.code == 0){
      for(Student o: (ArrayList<Student>)res_getAll.data){
        System.out.println(o.toString());
      }
    }
  }
}
DaoTest/StudentDaoTest.bat
1
2
3
4
5
6
7
8
del stu.db
javac -encoding utf-8 xust/stu/Result.java -d .
javac -encoding utf-8 xust/stu/ConnectionUtil.java -d .
javac -encoding utf-8 xust/stu/demo/domain/Student.java -d .
javac -encoding utf-8 xust/stu/demo/dao/StudentDao.java -d .
javac -encoding utf-8 xust/stu/demo/dao/StudentDaoImpl.java -d .
javac -encoding utf-8 xust/stu/demo/dao/StudentDaoTest.java -d .
java -classpath ".;sqlite-jdbc-3.41.0.0.jar" xust.stu.demo.dao.StudentDaoTest > StudentDaoTestResult.txt
运行结果
1
2
3
4
5
6
init=1
create=0
update=0
delete=0
{"no": "X2", "name": "X2", "gender": "X2", "age": 2, "dept": "X2"}
{"no": "X1", "name": "X1", "gender": "X1", "age": 1, "dept": "X1"}