JDBC操作

导入jdbc依赖包

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.19version>
    dependency>
public class Jdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //配置信息
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123";

        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //连接数据库
        Connection connection = DriverManager.getConnection(url,username,password);

        //创建数据库操作对象statement
        Statement statement = connection.createStatement();

        //要执行的sql语句
        String sql = "select * from users ";

        //执行sql语句,返回结果集
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()){
            System.out.println(resultSet.getObject("id"));
            System.out.println(resultSet.getObject("age"));
            System.out.println(resultSet.getObject("name"));
            System.out.println(resultSet.getObject("address"));
        }

        //关闭连接
        resultSet.close();
        statement.close();
        connection.close();

    }
}

预编译,防止sql注入

public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123";

        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //编写sql语句
        String sql = "insert into users(id, name, age, address) values(?,?,?,?)";
        //预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setInt(1,4);
        preparedStatement.setString(2,"唐三");
        preparedStatement.setInt(3,20);
        preparedStatement.setString(4,"武汉");

        //执行sql
        int i = preparedStatement.executeUpdate();

        if (i>0){
            System.out.println("插入成功");
        }

        //关闭连接,释放资源
        preparedStatement.close();
        connection.close();
    }
}

jdbc关于事务

事务具有原子性,要么都成功执行,要么都不成功执行

单元测试的包

    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
public class TestJdbcS {
    //单元测试,需要导入junit包
    @Test
    public void hello(){
        System.out.println("hello");
    }

    
    @Test
    public void test(){

        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123";

        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);

            //开启事务
            connection.setAutoCommit(false);

            String sql1 = "update account set money=money-100 where `'user'`='a'";
            connection.prepareStatement(sql1).executeUpdate();

            //制造错误
            int i =1/0;

            String sql2 = "update account set money=money+100 where `'user'`='b'";
            connection.prepareStatement(sql2).executeUpdate();

            //提交事务
            connection.commit();
            System.out.println("success");

        } catch (Exception e) {
            e.printStackTrace();

            //出现异常就回滚事务
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

}

你可能感兴趣的:(Java,web,mysql,数据库,sql)