首页 > 其他分享 >JDBC练习_delete、DDL语句和JDBC各个类详解_ResultSet_基本使用

JDBC练习_delete、DDL语句和JDBC各个类详解_ResultSet_基本使用

时间:2022-10-20 11:59:16浏览次数:42  
标签:JDBC sta DDL ResultSet printStackTrace sql catch null conn

JDBC练习_delete、DDL语句:

delete语句:删除一条记录

1.练习:

account表  删除一条记录

public static void main(String[] args) {
        // 设sta为空
        Statement sta = null;
        // 设conn为空
        Connection conn = null;
        try {
            // 1.注册驱动成功
            Class.forName("com.mysql.jdbc.Driver");
            // 2.数据库连接
            conn = DriverManager.getConnection("jdbc:mysql:///mybatis", "root", "root");
            // 3.指定sql语句
            String sql = "delete from account where  id = 3";
            // 4.获取sql对象语句
            sta = conn.createStatement();
            // 5.执行sql
            int count = sta.executeUpdate(sql);
            // 6.处理结果
            System.out.println(count);
            // 判断count大于0
            if (count > 0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException es) {
            es.printStackTrace();
        }finally {
            // 7.释放资源
            // 避免空指针异常
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        // 7.释放资源
        // 避免空指针异常
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException es) {
                es.printStackTrace();
            }
        }
    }

输出结果:

 

 

 DDL语句:创建一个表

public static void main(String[] args) {
        // 设sta为空
        Statement sta = null;
        // 设conn为空
        Connection conn = null;
        try {
            // 1.注册驱动成功
            Class.forName("com.mysql.jdbc.Driver");
            // 2.数据库连接
            conn = DriverManager.getConnection("jdbc:mysql:///mybatis", "root", "root");
            // 3.指定sql语句
            String sql = "create table parent (id int, name varchar(20))";
            // 4.获取sql对象语句
            sta = conn.createStatement();
            // 5.执行sql
            int count = sta.executeUpdate(sql);
            // 6.处理结果
            System.out.println(count);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException es) {
            es.printStackTrace();
        }finally {
            // 7.释放资源
            // 避免空指针异常
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        // 7.释放资源
        // 避免空指针异常
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException es) {
                es.printStackTrace();
            }
        }
    }

输出结果:

 

 

 

 

 

JDBC各个类详解_ResultSet_基本使用:

1. Resultset:结果集对象,封装查询结果

next():游标向下移动一行

getxxx(参数):获取数据

xxx:代表数据类型如:int getInt() , string getstring()

参数:

1. int :代表列的编号,从1开始如: getstring(1)

2. string :代表列名称。如: getDouble( "balance")

代码实现:

public static void main(String[] args) {
        // 设sta为空
        Statement sta = null;
        // 设conn为空
        Connection conn = null;
        //
        ResultSet res = null;
        try {
            // 1.注册驱动成功
            Class.forName("com.mysql.jdbc.Driver");
            // 2.数据库连接
            conn = DriverManager.getConnection("jdbc:mysql:///mybatis", "root", "root");
            // 3.指定sql语句
            String sql = "select * from account";
            // 4.获取sql对象语句
            sta = conn.createStatement();
            // 5.执行sql
            res = sta.executeQuery(sql);
            // 6.处理结果
            // 6.1让游标向下移动一行
            res.next();
            // 6.2获取数据
            int id = res.getInt(1);
            String name = res.getString("name");
            double bal = res.getDouble(3);

            System.out.println(id + "---" + name + "---" + bal);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException es) {
            es.printStackTrace();
        }finally {
            // 7.释放资源
            // 避免空指针异常
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        // 7.释放资源
        // 避免空指针异常
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException es) {
                es.printStackTrace();
            }
        }
        // 7.释放资源
        // 避免空指针异常
        if (res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

输出结果:

 

标签:JDBC,sta,DDL,ResultSet,printStackTrace,sql,catch,null,conn
From: https://www.cnblogs.com/hungui/p/16809315.html

相关文章