14、JDBC

JDBC

什么是JDBC:Java连接数据库

image-20210613221451901

需要jar包的支持:

  • java.sql
  • javax.sql
  • mysql-conneter-java... 连接驱动(必须要导入)

实验环境搭建

导入数据库依赖

	<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

IDEA连接数据库

image-20210614125818859

JDBC固定步骤:

1.加载驱动

2.连接数据库

3.向数据库发送sql的对象statement,PrepareStatement:CRUD

4.编写SQL

5.执行SQL

6.关闭连接

public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=true&characterEncoding=utf-8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbcjava?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "root";
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //向数据库发送sql的对象statement:CRUD
        Statement statement = connection.createStatement();
        //编写sql
        String sql = "select * from users";
        //执行sql
        ResultSet rs = statement.executeQuery(sql);
        while ( rs.next() ) {
            System.out.println("id=" + rs.getObject("id"));
            System.out.println("name=" + rs.getObject("name"));
            System.out.println("password=" + rs.getObject("password"));
            System.out.println("email=" + rs.getObject("email"));
            System.out.println("birthday=" + rs.getObject("birthday"));
        }
        //关闭连接,释放资源(先开后关)
        rs.close();
        statement.close();
        connection.close();
    }
}

image-20210614131353726

预编译SQL

public class TestJdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=true&characterEncoding=utf-8 解决中文乱码
        String url = "jdbc:mysql://localhost:3306/jdbcjava?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "root";
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //编写sql
        String sql = "insert into users(id,name,password,email,birthday) values(?,?,?,?,?)";
        //预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,4);
        preparedStatement.setString(2,"多多");
        preparedStatement.setString(3,"123456");
        preparedStatement.setString(4,"duoduo@qq.com");
        preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
        //执行sql
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("插入成功");
        }
        //关闭连接,释放资源(先开后关)
        preparedStatement.close();
        connection.close();
    }
}

image-20210614133607606

image-20210614133623488

事务

要么都成功,要么都失败

ACID原则:保证数据的安全

开启事务
事务提交	commit()
事务回滚	rollback()
关闭事务
    
转账:
A:1000
B:1000
    
A(900)  -- 100 --> B(1100)

Junit单元测试

依赖

		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

简单使用

@Test注解只有在方法上有效,只要加了这个注解的方法,就可以直接运行

@Test
    public void test() {
        System.out.println("hello");
    }

image-20210614135247864

失败的时候是红色的:

image-20210614135502608

搭建一个环境

@Test
    public void test2() {
        String url = "jdbc:mysql://localhost:3306/jdbcjava?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "root";
        Connection connection = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接数据库
            connection = DriverManager.getConnection(url, username, password);
            //开启事务  false:打开
            connection.setAutoCommit(false);
            //编写sql
            String sql = " update account set money = money - 100 where name = 'A'";
            connection.prepareStatement(sql).executeUpdate();
            //编写错误信息
            //int i = 1/0;
            String sql2 = " update account set money = money + 100 where name = 'B'";
            connection.prepareStatement(sql2).executeUpdate();
            //提交事务
            connection.commit();
        } catch (Exception e) {
            //回滚事务
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}
posted @ 2021-06-23 23:02  多瑞C  阅读(48)  评论(0)    收藏  举报