JavaWeb中增删改的优化方式

MYSQL增删改查重复代码简化

1.使用 @Before @After 将重复代码聚合

 private  Connection connection;
    private  String sql;
    @Before
    public void into() throws Exception {
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban","root","123456");
    }

    @Test
    public void testAdd() throws Exception {

        sql = "insert into bailiban.hero VALUES (null,'哈哈','男',16)";
    }
 @Test
    public void testUpdate() throws SQLException {

            sql = "update bailiban.hero set age = 13 where name = '露娜'";
        }
 @Test
    public void testDelete() throws SQLException {

         sql ="delete from bailiban.hero where id = 3";

    }
   @After
    public  void end() throws Exception {

       Statement statement = connection.createStatement();
       int i = statement.executeUpdate(sql);
       System.out.println(i);

       statement.close();
       connection.close();

   }

}

2.创建工具类

 public static void main(String[] args) throws Exception {
      
    }


    public static void upDate(String sql) throws Exception {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban", "root", "123456");
        Statement stat = conn.createStatement();
        int i = stat.executeUpdate(sql);
        System.out.println(i);

        stat.close();
        conn.close();


    }
    public static void queryOne(String sql) throws Exception {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban", "root", "123456");
        Statement stat = conn.createStatement();
        ResultSet query = stat.executeQuery(sql);
        if (query.next()){
            String id = query.getString("id");
            String name = query.getString("name");
            String sex = query.getString("sex");
            String age = query.getString("age");
            System.out.println(id+"--"+name+"--"+sex+"--"+age);
        }
        System.out.println(query.next());
        stat.close();
        conn.close();
    }

    public static void queryMore(String sql) throws Exception {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bailiban", "root", "123456");
        Statement stat = conn.createStatement();
        ResultSet query = stat.executeQuery(sql);
        while (query.next()){
            String id = query.getString("id");
            String name = query.getString("name");
            String sex = query.getString("sex");
            String age = query.getString("age");
            System.out.println(id+"--"+name+"--"+sex+"--"+age);
        }
        System.out.println(query.next());
        stat.close();
        conn.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值