自学内容网 自学内容网

【web】JDBC

项目连接数据库

右侧导航栏找到databsae

如果没有驱动,先下载驱动

填写数据库用户名密码

勾选对应的表即可

JDBC代码流程

1,配置信息

2,加载驱动   从MySQL Connector/J 5.1版本开始,推荐使用com.mysql.cj.jdbc.Driver这个新的驱动类。

3,链接数据库

4,向数据库发送SQL的对象statement:CURD

5,编写sql

6,执行查询SQL,返回一个ResultSet  结果集

7,关闭链接,释放资源  一定要做,先开的后关

代码流程如下:

public class TestIdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=ture&characterEncoding=utf-8解决中文乱码问题
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=ture&characterEncoding=utf-8";
        String username="root";
        String password="123456";
        //1,加载驱动   从MySQL Connector/J 5.1版本开始,推荐使用com.mysql.cj.jdbc.Driver这个新的驱动类。
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2,链接数据库
        Connection cnno= DriverManager.getConnection(url,username,password);
        //3,向数据库发送SQL的对象statement:CURD
        Statement sta=cnno.createStatement();
        //4,编写sql
        String sql="select * from user";
        //5,执行查询SQL,返回一个ResultSet  结果集
        ResultSet rs=sta.executeQuery(sql);

        //4,编写sql
        // String sql="update user set name='张三' where id=1";
        //5,执行查询SQL,返回一个int 修改的数据条数
       // int rs=sta.executeUpdate(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("age="+rs.getObject("age"));
            System.out.println("address="+rs.getObject("address"));
        }

        //6,关闭链接,释放资源  一定要做,先开的后关
        rs.close();
        sta.close();
        cnno.close();
    }

预编译sql 

public class TestIdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        //useUnicode=ture&characterEncoding=utf-8解决中文乱码问题
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=ture&characterEncoding=utf-8";
        String username="root";
        String password="123456";
        //1,加载驱动   从MySQL Connector/J 5.1版本开始,推荐使用com.mysql.cj.jdbc.Driver这个新的驱动类。
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2,链接数据库
        Connection cnno= DriverManager.getConnection(url,username,password);
         //3,编写sql
        String sql="insert into user(id,name,password,address,age,eamil) values (?,?,?,?,?,?)";
        //4,预编译
        PreparedStatement preparedStatement= cnno.prepareStatement(sql);

        preparedStatement.setInt(1,3);//给第一个占位符?的赋值为1
        preparedStatement.setString(2,"我们");//给第二一个占位符?的赋值为1
        preparedStatement.setString(3,"12345");
        preparedStatement.setString(4,"成都");
        preparedStatement.setString(5,"18");
        preparedStatement.setString(6,"11.qq.com");
        //5,执行sql
        int i=preparedStatement.executeUpdate();
        if(i > 0){
            System.out.println("成功");
        }

        //6,关闭链接,释放资源  一定要做,先开的后关
        preparedStatement.close();
        cnno.close();
    }
}

JDBC事务 

 ACID原则:保证数据的安全性,要么都成功,要么都不成功

开启事务 start transaction;

事务提交 commit()

事务回滚 rollback()

关闭事务

单元测试依赖junit

    <!--单元测试  方法上加@Test即可运行测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

事务案例

public class TestIdbc3 {
    public static void main(String[] args){
        //配置信息
        //useUnicode=ture&characterEncoding=utf-8解决中文乱码问题
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=ture&characterEncoding=utf-8";
        String username="root";
        String password="123456";
        Connection cnno = null;
        try {
            //1,加载驱动   从MySQL Connector/J 5.1版本开始,推荐使用com.mysql.cj.jdbc.Driver这个新的驱动类。
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2,链接数据库
            cnno= DriverManager.getConnection(url,username,password);
            //通知数据库开启是事务
            cnno.setAutoCommit(false);
            //3,编写sql
            String sql="update account set money = money-100 where name='A'";
           // 执行sql
           cnno.prepareStatement(sql).executeUpdate();

           //制造错误
            int i=1/0;
            
            String sql2="update account set money = money+100 where name='B'";
            // 执行sql
            cnno.prepareStatement(sql2).executeUpdate();

            cnno.commit();


        } catch (Exception e) {
            try {
                //如果出现异常,通知数据库回滚
                cnno.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            //6,关闭链接,释放资源  一定要做,先开的后关
            try {
                cnno.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}


原文地址:https://blog.csdn.net/m0_63789588/article/details/142938167

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!