JDBC练习二

本文最后更新于:2021年9月20日 晚上

JDBC练习二

import java.sql.*;
/*
	JDBC完成Delete
*/

public class JDBCTest02 {
    public static void main(String[] args) {
        // 1、注册驱动
        // 2、获取连接
        // 3、获取数据库操作对象
        // 4、执行sql语句
        // 5、获取查询结果集
        // 6、释放资源

        Connection conn = null;
        Statement stmt = null;
        try {
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);

            String url = "jdbc:mysql://127.0.0.1:3306/mydb";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url,user,password);

            stmt = conn.createStatement();

            int count = stmt.executeUpdate("delete from student where studentid = 5");

            System.out.println(count == 1? "删除成功":"删除失败");

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

运行结果:

image-20210920190345003


本文作者: CodeAnime
本文链接: https://codeanime.cc/JDBC%E7%BB%83%E4%B9%A0%E4%BA%8C.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!