Bootstrap

JDBC执行删除与更新

JDBC执行删除与更新

1. JDBC执行删除

/*
	JDBC完成delete update
*/
public class JDBCTest02 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            //1.注册驱动
            DriverManager.registerDriver(new Driver());

            //2.获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123456");

            //3.获取数据库操作对象
            statement =  connection.createStatement();

            //4.执行sql
            // JDBC中的sql语句不需要提供分号结尾。
            String sql = "delete from dept where deptno = 40";
            int count = statement.executeUpdate(sql);
            System.out.println(count == 1 ? "删除成功":"删除失败!");

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

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

2. JDBC执行更新

/*
	JDBC完成delete update
*/
public class JDBCTest02 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            //1.注册驱动
            DriverManager.registerDriver(new Driver());

            //2.获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123456");

            //3.获取数据库操作对象
            statement =  connection.createStatement();

            //4.执行sql
            // JDBC中的sql语句不需要提供分号结尾。
            String sql = "update dept set dname = '销售部',loc = '天津' where deptno  = 20";
            int count = statement.executeUpdate(sql);
            System.out.println(count == 1 ? "删除成功":"删除失败!");

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

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

 

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;