首页 > 数据库技术 > 详细

JDBC练习_update_DDL语句

时间:2020-12-20 15:06:05      阅读:35      评论:0      收藏:0      [点我收藏+]

删除一条记录:

public static void main(String[] args) {

        Connection conn = null;

        Statement stmt = null;

        try {

            //1. 注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            //2.获取连接对象

            conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");

           //conn = JDBCUtils.getConnection("jdbc:mysql:///db3", "root", "root");

            //3.定义sql

            String sql  = "delete from account where id = 3";

            //4.获取执行sql对象

            stmt = conn.createStatement();

            //5.执行sql

            int count = stmt.executeUpdate(sql);

            //6.处理结果

            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 {

            //7.释放资源

 

            if(stmt != null){

                try {

                    stmt.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

 

            if(conn != null){

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

        }

}

创建一张表:

public static void main(String[] args) {

        Connection conn = null;

        Statement stmt = null;

        try {

            //1. 注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            //2.获取连接对象

            conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");

            //3.定义sql

            String sql  = "create table student (id int , name varchar(20))";

            //4.获取执行sql对象

            stmt = conn.createStatement();

            //5.执行sql

            int count = stmt.executeUpdate(sql);

            //6.处理结果

            System.out.println(count);

 

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            //7.释放资源

 

            if(stmt != null){

                try {

                    stmt.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

 

            if(conn != null){

                try {

                    conn.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

        }

    }

 

 

总结 :

注意这里需要使用statementexecuteUpdate(sql),方法,并关闭连接connectionstatement

JDBC练习_update_DDL语句

原文:https://www.cnblogs.com/1and2/p/14152969.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!