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

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

时间:2022-10-16 20:34:05浏览次数:49  
标签:548JDBC 549JDBC ResultSet stmt printStackTrace sql catch null conn

JDBC练习_update  and  DDL语句

   //删除数据库内容语句
    //导入jar包
    public static void main(String[] args) {
        Connection conn=null;
        Statement stmt =null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库的连接对象
             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/guo01?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");

            //定义sql语句
            String sql = "delete from account where id=3";

            //获取执行sql的对象Statement
            stmt = conn.createStatement();

            //获取执行sql
            int count = stmt.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 (stmt !=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

DDL语句练习

 //执行DDL语句
    //导入jar包
    public static void main(String[] args) {
        Connection conn=null;
        Statement stmt =null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库的连接对象
             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/guo01?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");

            //定义sql语句
            String sql = "create table guo02(id int,name varchar(20))";

            //获取执行sql的对象Statement
            stmt = conn.createStatement();

            //获取执行sql
            int count = stmt.executeUpdate(sql);

            //处理结果
            System.out.println(count);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            if (stmt !=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

JDBC各个类详解_ResultSet_基本使用

ResultsetT结果集对象,封装查询结果

next方法(游标向下移动一行)

getXxx(参数),Xxx代表数据类型。如:int get int(),String getString()

Int :代表列的编号 。从1开始如:getString(1)

String: 代表列的名称。getDouble(balance)

 

 

 

 public static void main(String[] args) {
        Connection conn=null;
        Statement stmt =null;
        ResultSet rs   =null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库的连接对象
             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/guo01?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");

            //定义sql语句
            String sql = "create table guo02(id int,name varchar(20))";

            //获取执行sql的对象Statement
            stmt = conn.createStatement();

            //获取执行sql
            rs = stmt.executeQuery(sql);
            //6.1让游标向下移动一行
            rs.next();
            //获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);
} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { //释放资源 if (stmt !=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn !=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

 

Preparedstatement :执行sql的对象

 

标签:548JDBC,549JDBC,ResultSet,stmt,printStackTrace,sql,catch,null,conn
From: https://www.cnblogs.com/agzq/p/16797021.html

相关文章