五、数据库编程

1、JDBC的设计

1.1 JDBC的典型用法

在三层应用模型中,客户端不直接调用数据库,而是调用服务器上的中间件层,由中间件层完成数据库查询操作,这种三层模型有以下优点:它将可视化表示(位于客户端)从业务逻辑(位于中间层)和原始数据(位于数据库)中分离处理。
在这里插入图片描述

2、JDBC配置

jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/aaa?serverTimezone=CTT&characterEncoding=UTF-8
jdbc.username = root
jdbc.password = root
public class JDBCDemo01 {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        //1、读取配置
        Properties properties = new Properties();
        try (InputStream in = Files.newInputStream(Paths.get("database.properties"))) {
            properties.load(in);
        }
        //2、注册驱动类
        String driver = properties.getProperty("jdbc.driver");
        Class.forName(driver);
        //3、连接到数据库
        String url = properties.getProperty("jdbc.url");
        String username = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");
        Connection connection = DriverManager.getConnection(url, username, password);
        //4、执行sql
        Statement statement = connection.createStatement();
        statement.executeUpdate("insert into t_student (name, no) values ('张三', 13)");
        ResultSet resultSet = statement.executeQuery("select * from t_student");
        if (resultSet.next()){
            System.out.println(resultSet.getString(2));
        }
    }
}

3、执行SQL语句

        //返回修改的行数
        int a = statement.executeUpdate("insert into t_student (name, no) values ('王五', '20201515')");
        int b = statement.executeUpdate("update t_student set name = '张三update' where no = '20201313'");
        System.out.println(a + " --------- " + b);
        ResultSet resultSet = statement.executeQuery("select * from t_student");
        if (resultSet.next()){
            //输出查询结果第二列
            System.out.println(resultSet.getString(2));
            //输出查询结果字段名为“no”列
            System.out.println(resultSet.getString("no"));
        }

        String sql1 = "select * from t_student where name = ?";
        //设置预备语句
        PreparedStatement statement2 = connection.prepareStatement(sql1);
        //执行预备语句之前设置 ? 参数(1代表第一个?, 后面的是参数值)
        statement2.setString(1, "张三update");
        ResultSet resultSet1 = statement2.executeQuery();
        if (resultSet1.next()){
            //输出查询结果第二列
            System.out.println(resultSet1.getString(2));
            //输出查询结果字段名为“no”列
            System.out.println(resultSet1.getString("no"));

4、事务

我们可以将一组语句构建成一个事务。当所有语句都顺利执行之后,事务可以被提交。否则,如果其中某个语句遇到错误,那么事务将被回滚。
将多个语句组合成事务的主要原因是为了确保数据库完整性。

public class JDBCDemo02 {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        //1、读取配置
        Properties properties = new Properties();
        try (InputStream in = Files.newInputStream(Paths.get("D:\\codes\\study\\src\\main\\java\\com\\java02\\day05\\database.properties"))) {
            properties.load(in);
        }
        //2、注册驱动类
        String driver = properties.getProperty("jdbc.driver");
        Class.forName(driver);
        //3、连接到数据库
        String url = properties.getProperty("jdbc.url");
        String username = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");
        Connection connection = DriverManager.getConnection(url, username, password);

        //关闭自动提交
        connection.setAutoCommit(false);

        //4、执行sql
        Statement statement = connection.createStatement();
        statement.executeUpdate("insert into t_student (name, no) values ('赵六', '20201616')");
        //还可以设置保存点,用来精确回滚操作
        //创建一个保存点,意味着回滚的时候只需回滚到这个位置,不需要回滚到事务最初的位置
        Savepoint savepoint = connection.setSavepoint();
        try {
            statement.executeUpdate("update t_student set name = '张三update111' where no = '20201313'");
            statement.executeUpdate("update t_student set name = '王五333' where no = '20201515'");
            //执行提交
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //遇到错误执行回滚
            connection.rollback();
        }
        //如果设置了保存点,当不需要保存点时,必须要释放它。
        connection.releaseSavepoint(savepoint);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。针对这个问题,首先需要明确你需要设计的小型数据库应用程序的具体功能和需求,以便能够更加具体地进行设计和实现。 一般来说,设计一个小型的数据库应用程序,需要考虑以下几个方面的内容: 1. 数据库设计:根据需求,设计数据库的表结构和字段,明确各个表之间的关系。 2. 数据库连接:使用数据库连接工具或编程语言提供的数据库连接库,连接数据库并进行数据的增、删、改、查等操作。 3. 界面设计:设计应用程序的用户界面,使用户能够直观地使用应用程序。 4. 功能实现:根据需求,实现应用程序的各项功能,例如数据的查询、统计、导出等。 下面给出一个简单的例子,假设你需要设计一个学生成绩管理系统,具体的需求如下: 1. 记录学生的基本信息,包括姓名、学号、年龄、性别等。 2. 记录学生的各门课程成绩,包括课程名称、成绩等。 3. 能够对学生进行查询、添加、修改和删除等操作。 4. 能够统计学生的课程成绩情况,并能够按照不同的条件进行排序和筛选。 基于以上需求,可以设计一个简单的数据库表结构如下: 学生表(Student): 字段:学号、姓名、年龄、性别 成绩表(Score): 字段:学号、课程名称、成绩 根据上述表结构,可以使用编程语言进行数据库连接和操作,例如使用 Python 的 MySQLdb 库进行操作。同时,可以设计一个简单的用户界面,例如使用 PyQt 进行开发。 在功能实现方面,可以根据需求实现查询、添加、修改和删除等功能,同时也可以实现课程成绩的统计和排序等功能。 当然,以上仅是一个简单的例子,实际的小型数据库应用程序需要根据具体需求进行设计和实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值