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)表示倒数第二行。
最新文章
- Android Studio使用Git版本控制工具
- 给vs2010换皮肤
- 架构设计 - Server设计草稿
- maven添加仓库地址
- Typecho中的gravatar头像无法加载
- hdu3555 Bomb (记忆化搜索 数位DP)
- WLAN历史概述-01
- 更新wix installer里的Guid
- RemoteViews嵌入ListView复杂布局
- HLS(HTTP Live Streaming)协议之m3u8文件生成方式
- 说出x的结果,并解释为什么?
- JProtector java应用加密工具
- 中文乱码总结之web乱码情景
- linux ---docker篇
- jQuery实现简单的五星好评
- idea使用破解版mybatis plugin插件失败,idea打不开的解决方案
- mysql 表映射为java bean 手动生成。
- Html5 postMessage实现跨域消息传递
- Linux性能优化 第八章 实用工具:性能工具助手
- 【ocp-12c】最新Oracle OCP-071考试题库(38题)
热门文章
- 搭建rabbitmq集群
- GCC 预处理,汇编,编译,链接,连接静态库,动态库
- 论文阅读笔记五十九:Res2Net: A New Multi-scale Backbone Architecture(CVPR2019)
- 洛谷 P5021 [NOIP2018]赛道重建
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
- 北漂不易,逆风前行[给想在北京工作的java同学们诚恳的建议!]
- (转)yum只下载不安装软件包
- Django中render_to_response和render的区别(转载)
- Github库名命名规范
- MongoDB出现The default storage engine 'wiredTiger' is not available之问题解决