java使用mysql-connector-java操作数据库:更新、删除、插入

    //更新、删除、插入
    public static void update() throws ClassNotFoundException, SQLException {
        String sql = "update sql.t_backend_user_role set create_time='2017-05-22 23:29:27' where id=277";//更新数据
//        String sql = "delete from sql.t_backend_user_role where id=281 or id=279";//删除数据
//        String sql = "insert into sql.t_backend_user_role(id,role_id,backend_user_id,create_time) values(279,8,9,'2020-08-08 00:00:00')";//插入数据
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.10.102/?useUnicode=true&characterEncoding=UTF-8", "", "");
        Statement stmt = conn.createStatement();
        int count = stmt.executeUpdate(sql);
        if (count == 0) {
            System.out.println("操作数据失败:" + sql);
        } else {
            System.out.println("操作" + count + "条数据成功:" + sql);
        }
        stmt.close();
        conn.close();
    }

 

你可能感兴趣的:(程序开发)