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;

                }

        }

}
上一篇:main方法可以继承么?


下一篇:Springboot热部署原理