0
点赞
收藏
分享

微信扫一扫

JBCD_修改数据

小飞侠熙熙 2023-09-21 阅读 33

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();
                }
            }
        }
    }
}



举报

相关推荐

0 条评论