其他分享
首页 > 其他分享> > JBCD_修改数据

JBCD_修改数据

作者:互联网

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcDemo03 {
    public static void main(String[] args) {
        Connection conn = null; //定义为全局变量
        Statement stat = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接对象
             conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "123456");
            //3.定义sql
            String sql = "update stu set name = 'lucky' where id = 2";
            //3.获取执行sql对象
            stat = conn.createStatement();
            //4.执行sql
            int count = stat.executeUpdate(sql);
            System.out.println(count);
            if(count>0){
                System.out.println("修改成功!");
            }else{
                System.out.println("修改失败!");
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

标签:stat,JBCD,printStackTrace,修改,sql,catch,null,数据,conn
来源: https://www.cnblogs.com/lcc-lv/p/16488780.html