javaSE--jdbc

更新语句类:

实例化Driver中可省去手动newInstance的原因:其类中本身就是被static代码块包裹,随着该类的加载而自动加载进内存中。只需如下写:


//2.加载驱动 (①实例化Driver ②注册驱动)
        Class.forName(driverName);

然而java的spi机制,上一步也不用写!
 @Test
    public void testPrepareStatement() {

        Connection connection =null;
        Statement statement = null;
        ResultSet resultSet =null;


        try {
            String sql ="update vip set username=? where id=?";
            connection = jdbcUtil.getConnection();
            PreparedStatement  preparedStatement = connection.prepareStatement(sql);
            //1:代表第一个?
            preparedStatement.setString(1,"xzhao");//string双引号,char单引!!!
//            2:代表第二个?占位符
            preparedStatement.setInt(2,2);
            preparedStatement.execute();



        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            jdbcUtil.closeAll(connection,statement,resultSet);

        }
    }

查询语句类:

@Test
    public void testPrepareStatement1() {

        Connection connection =null;
        Statement statement = null;
        ResultSet resultSet =null;


        try {
            String sql ="select * from vip where id > ?";
            connection = jdbcUtil.getConnection();
            PreparedStatement  preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,0);
            resultSet=preparedStatement.executeQuery();

            while ((resultSet.next())){
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("username"));
            }



        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            jdbcUtil.closeAll(connection,statement,resultSet);

        }
    }

事务演示类:

@Test
    public void transaction() {
         //★一个事务必须在一个connection中用哦!
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;


        try {
            String sql ="update vip set username= ? where id =?";
            //获取链接并 关闭自动提交
            connection = jdbcUtil.getConnection();
            connection.setAutoCommit(false);
             preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"jhie");  //第一个问号占位符
            preparedStatement.setInt(2,1);    //修改第二个问号占位符
            int i=preparedStatement.executeUpdate();
            System.out.println(i);

            int res =i/0; //模拟异常,用来导致事务回滚,本次操作作废,数据退回上一步的亚子

            String sql2 ="update vip set username= ? where id =?";
            //获取链接并 关闭自动提交
            connection = jdbcUtil.getConnection();
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"特殊t");
            preparedStatement.setInt(2,2);
            int i2=preparedStatement.executeUpdate();
            System.out.println(i2);

            connection.commit();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            jdbcUtil.closeAll(connection,preparedStatement,resultSet);

        }
    }

抽取重复代码Utils:连接和关闭:

public class jdbcUtil {

    public static Connection getConnection(){
        Connection conn = null;
        try {
            Properties properties = new Properties();
            properties.load(JdbcTest.class.getClassLoader().getResourceAsStream("jdbc.properties"));

            String url = properties.getProperty("mysql.url");
            String user = properties.getProperty("mysql.username");
            String password = properties.getProperty("mysql.password");
            String driverName = properties.getProperty("mysql.driverName");

            //2.加载驱动 (①实例化Driver ②注册驱动)
            Class.forName(driverName);

            //3.获取连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(statement!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(resultSet!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

jdbc.properties配置文件:

mysql.username=root
mysql.password=root
mysql.url=jdbc:mysql://127.0.0.1:3306/meta?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
mysql.driverName=com.mysql.jdbc.Driver
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值