• 02、JDBC查询


    ① 向数据库发送SQL查询语句

       首先使用Statement声明一个SQL语句对象,然后让已创建的连接对象con调用方法createStatement()创建SQL语句对象。

    Statement sql = con.createStatement();

    Connection con = null;

    String DBname = "jdb"; //数据库名字

    String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8";

    String username = "root";//数据库账号

    String password = "root";//数据库密码

    Statement sql = null;

    try {

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

    con = DriverManager.getConnection(url, username, password);//连接代码

    sql = con.createStatement();

    } catch (Exception e) {

    // TODO: handle exception

    System.out.println(e);

    }

    ② 处理查询结果

       有了SQL语句对象后,可以调用相应的方法实现对数据库中表的查询和修改,并将结果放在一个ResultSet类声明的对象中。换句话说,SQL查询语句对数据库的查询操作将返回一个ResultSet对象,ResultSet对象由按"列"(字段)组织的数据行构成。

    ResultSet rs = sql.executeQuery("SELECT * FROM students");//查询student表中的数据

    Connection con = null;

    String DBname = "jdb"; //数据库名字

    String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8";

    String username = "root";//数据库账号

    String password = "root";//数据库密码

    Statement sql = null;

    ResultSet rs = null;

    try {

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

    con = DriverManager.getConnection(url, username, password);//连接代码

    sql = con.createStatement();

    rs = sql.executeQuery("SELECT * FROM students");//查询student表中的数据

    } catch (Exception e) {

    // TODO: handle exception

    System.out.println(e);

    }

       ResultSet对象的方法

    NO.

    方法名称

    类型

    描述

    01

    public boolean next()throws SQLException

    普通

    将光标从当前位置向前移一行

    02

    public byte getByte(int columnIndex)throws SQLException

    普通

    以byte的形式获取当前行中指定列的值

    03

    public Date getDate(int columnIndex)throws SQLException

    普通

    以java.sql.Date对象的形式获取当前行中指定列的值

    04

    public double getDouble(int columnIndex)throws SQLException

    普通

    以double的形式获取此 当前行中指定列的值

    05

    public float getFloat(int columnIndex)throws SQLException

    普通

    以float的形式获取当前行中指定列的值

    06

    public int getInt(int columnIndex)throws SQLException

    普通

    以int的形式获取当前行中指定列的值

    07

    public long getLong(int columnIndex)throws SQLException

    普通

    以long的形式获取当前行中指定列的值

    08

    public String getString(int columnIndex)throws SQLException

    普通

    以String的形式获取当前行中指定列的值

    09

    public byte getByte(String columnName)throws SQLException

    普通

    以byte的形式获取当前行中指定列的值

    10

    public Date getDate(String columnName)throws SQLException

    普通

    以java.sql.Date对象的形式获取当前行中指定列的值

    11

    public double getDouble(String columnName)throws SQLException

    普通

    以double的形式获取此 当前行中指定列的值

    12

    public float getFloat(String columnName)throws SQLException

    普通

    以float的形式获取当前行中指定列的值

    13

    public int getInt(String columnName)throws SQLException

    普通

    以int的形式获取当前行中指定列的值

    14

    public long getLong(String columnName)throws SQLException

    普通

    以long的形式获取当前行中指定列的值

    15

    public String getString(String columnName)throws SQLException

    普通

    以String的形式获取当前行中指定列的值

    说明:

       无论字段是何种属性;都可以使用getString方法返回字段值的串表示

    ③ 关闭连接

       con.close();

    注:使用ResultSet对象中的数据时,必须始终保持数据库的连接,直到应用程序将ResultSet对象中的数据查看完毕。如果在rs之后立即关闭连接,那么程序将无法获取rs中的数据。

    范例:控制statement对象游标

    方法:public Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException

    //获取Statement对象

    Statement stmt = con.createStatement(int type,int concurrency);

    //返回结果集

    ResultSet rs = stmt.executeQuery(SQL语句);

    resultSetType - 结果集类型,

    resultSetType:ResultSet.TYPE_FORWARD_ONLY、ResultSet.TYPE_SCROLL_INSENSITIVE 或 ResultSet.TYPE_SCROLL_SENSITIVE

    resultSetConcurrency:ResultSet.CONCUR_READ_ONLY 或 ResultSet.CONCUR_UPDATABLE

       ResulSet常用方法

    NO.

    方法名称

    类型

    描述

    01

    public void beforeFirst()throws SQLException

    普通

    将光标移动到开头,位于第一行之前

    02

    public void afterLast()throws SQLException

    普通

    将光标移动到末尾,位于最后一行之后

    03

    public boolean first()throws SQLException

    普通

    将光标移动到第一行

    04

    public boolean last()throws SQLException

    普通

    将光标移动到最后一行

    05

    public boolean isBeforeFirst()throws SQLException

    普通

    获取光标是否位于第一行之前

    06

    public boolean isAfterLast()throws SQLException

    普通

    获取光标是否位于最后一行之后

    07

    public boolean isFirst()throws SQLException

    普通

    获取光标是否位于第一行

    08

    public boolean isLast()throws SQLException

    普通

    获取光标是否位于最后一行。调用 isLast 方法可能开销很大

    09

    public int getRow()throws SQLException

    普通

    获取当前行编号

    10

    public boolean absolute(int row)throws SQLException

    普通

    将光标移动到此 ResultSet 对象的给定行编号

    注:如果row取负值,就是倒数的行数,absolute(-1)表示移到最后一行,absolute(-2)表示倒数第二行。

  • 相关阅读:
    CentOS7设置tomcat开机自启动
    MySQL复制表结构和表数据
    SpEL表达式总结
    Flink快速入门
    Flink架构、原理与部署测试
    第十一篇:Spark SQL 源码分析之 External DataSource外部数据源
    第十篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 query
    第九篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 cache table
    第八篇:Spark SQL Catalyst源码分析之UDF
    第七篇:Spark SQL 源码分析之Physical Plan 到 RDD的具体实现
  • 原文地址:https://www.cnblogs.com/CSAH/p/11638654.html
Copyright © 2020-2023  润新知