Java类对数据库结果集的处理(源码)

JDBC提供了很多连接数据库的方法,同时也提供了ResultSet方法对数据库结果集的可滚动查询,可更新查询。

 

举例数据库:Mysql

 

声明:这两个类我都已经测试通过,如果运行出错,请仔细检查,源码方法一定没问题。问题可能会出在表数据的属性,和JDBC版本是否支持。

 

下面是源码:

 

可滚动查询源码:(对数据进行了输出控制)

 

import  java.sql. * ;

ExpandedBlockStart.gifContractedBlock.gif
public   class  TestScrollResultSet {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String orgs[]){
        Connection conn
=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try{
       Class.forName(
"com.mysql.jdbc.Driver");
       String url
="jdbc:mysql://localhost:3306/people";
       conn
=DriverManager.getConnection(url,"username","password");
       Statement stmt
=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
       ResultSet rs
=stmt.executeQuery("select * from guestmessage");
ExpandedSubBlockStart.gifContractedSubBlock.gif       
while(rs.next()){
            showOneRow(rs);
       }
         
       System.out.println(
"---------------------------------");
       rs.last();
       showOneRow(rs);
       rs.first();
       showOneRow(rs);
       rs.beforeFirst();
       rs.next();
       showOneRow(rs);
       rs.absolute(
2);
       showOneRow(rs);
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(Exception e){
   e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
finally{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
try{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(conn!=null){
        conn.close();
      }

ExpandedSubBlockStart.gifContractedSubBlock.gif   }
catch(Exception e){
   e.printStackTrace();
   }

  }

}

ExpandedSubBlockStart.gifContractedSubBlock.gif
public static void showOneRow(ResultSet rs)throws SQLException{
            System.out.print(
""+rs.getRow()+""); 
            System.out.print(
"\t"+rs.getInt(1));
            System.out.print(
"\t"+rs.getString(2));
            System.out.print(
"\t"+rs.getString(3));
            System.out.println();
}

}

 

可更新查询源码:(可以通过此类直接更改数据库数据,但并不推荐,只作为功能介绍)

 

import  java.sql. * ;

ExpandedBlockStart.gifContractedBlock.gif
public   class  TestUpdatableResultSet {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String orgs[]){
        Connection conn
=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try{
       Class.forName(
"com.mysql.jdbc.Driver");
       String url
="jdbc:mysql://localhost:3306/people";
       conn
=DriverManager.getConnection(url,"username","password");
       Statement stmt
=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
       ResultSet rs
=stmt.executeQuery("select id,name,url from guestmessage");
       
//ResultSet rs=stmt.executeQuery("select t.* from guestmessage");
        
//更新前结果集中的数据
ExpandedSubBlockStart.gifContractedSubBlock.gif
       while(rs.next()){
            showOneRow(rs);
       }
         
       
//更新和删除记录
       rs.beforeFirst();
ExpandedSubBlockStart.gifContractedSubBlock.gif       
while(rs.next()){
         String name
=rs.getString("name").trim();//.trim()过滤掉空格
ExpandedSubBlockStart.gifContractedSubBlock.gif
         if(name.equals("施杨")){
            String sal
=rs.getString("url");
            rs.updateString(
"url",sal+"pppp");
            rs.updateRow();
ExpandedSubBlockStart.gifContractedSubBlock.gif         }
else if(name.equals("dd")){
            rs.deleteRow();
         }

       }

       
//插入新记录
       rs.moveToInsertRow();
       rs.updateInt(
"id",19);
       rs.updateString(
"name","hahahahhaha");
       rs.updateString(
"url","ssssssss");
       rs.insertRow();
       rs.close();
       
//结果集更新后数据库中数据
       System.out.println("---------------------------------");
       rs
=stmt.executeQuery("select * from guestmessage");
ExpandedSubBlockStart.gifContractedSubBlock.gif       
while(rs.next()){
          showOneRow(rs);
       }

       rs.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(Exception e){
   e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
finally{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
try{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(conn!=null){
        conn.close();
      }

ExpandedSubBlockStart.gifContractedSubBlock.gif   }
catch(Exception e){
   e.printStackTrace();
   }

  }

}

ExpandedSubBlockStart.gifContractedSubBlock.gif
public static void showOneRow(ResultSet rs)throws SQLException{
            System.out.print(
"\t"+rs.getInt(1));
            System.out.print(
"\t"+rs.getString(2));
            System.out.print(
"\t"+rs.getString(3));
            System.out.println();
}

}

 

 

实现这两个类要更改类中有关数据库数据,然后就可以完美运行了。

 

施杨出品!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值