JDBC的CURD操作之修改操作

1.1.1 修改操作代码实现

@Test

/**

 * 修改操作的代码实现

 */

public void demo2(){

        Connection conn = null;

        Statement stmt  = null;

        try{

                // 注册驱动:

                Class.forName("com.mysql.jdbc.Driver");

                // 获得连接

                conn = DriverManager.getConnection("jdbc:mysql:///web_test3", "root", "abc");

                // 执行操作:

                // 创建执行SQL语句的对象:

                stmt = conn.createStatement();

                // 编写SQL语句:

                String sql = "update user set password='abc',nickname='旺财' where id = 5";

                // 执行SQL语句:

                int num = stmt.executeUpdate(sql);

                if(num > 0){

                        System.out.println("修改用户成功!!!");

                }

        }catch(Exception e){

                e.printStackTrace();

        }finally{

                // 资源释放:

                if(stmt != null){

                        try {

                                stmt.close();

                        } catch (SQLException e) {

                                e.printStackTrace();

                        }

                         

                        stmt = null;

                }

                if(conn != null){

                        try {

                                conn.close();

                        } catch (SQLException e) {

                                e.printStackTrace();

                        }

                        conn = null;

                }

        }

}
    原文作者:qq5d3e5bae55f08
    原文地址: https://blog.51cto.com/14473726/2449728
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞