JDBC提供了很多连接数据库的方法,同时也提供了ResultSet方法对数据库结果集的可滚动查询,可更新查询。
举例数据库:Mysql
声明:这两个类我都已经测试通过,如果运行出错,请仔细检查,源码方法一定没问题。问题可能会出在表数据的属性,和JDBC版本是否支持。
下面是源码:
可滚动查询源码:(对数据进行了输出控制)
import java.sql.*;
public class TestScrollResultSet{
public static void main(String orgs[]){
Connection conn=null;
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");
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);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
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();
}
}
public class TestScrollResultSet{
public static void main(String orgs[]){
Connection conn=null;
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");
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);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
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.*;
public class TestUpdatableResultSet{
public static void main(String orgs[]){
Connection conn=null;
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");
//更新前结果集中的数据
while(rs.next()){
showOneRow(rs);
}
//更新和删除记录
rs.beforeFirst();
while(rs.next()){
String name=rs.getString("name").trim();//.trim()过滤掉空格
if(name.equals("施杨")){
String sal=rs.getString("url");
rs.updateString("url",sal+"pppp");
rs.updateRow();
}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");
while(rs.next()){
showOneRow(rs);
}
rs.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
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();
}
}
public class TestUpdatableResultSet{
public static void main(String orgs[]){
Connection conn=null;
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");
//更新前结果集中的数据
while(rs.next()){
showOneRow(rs);
}
//更新和删除记录
rs.beforeFirst();
while(rs.next()){
String name=rs.getString("name").trim();//.trim()过滤掉空格
if(name.equals("施杨")){
String sal=rs.getString("url");
rs.updateString("url",sal+"pppp");
rs.updateRow();
}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");
while(rs.next()){
showOneRow(rs);
}
rs.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
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();
}
}
实现这两个类要更改类中有关数据库数据,然后就可以完美运行了。
本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2008/08/15/1268305.html,如需转载请自行联系原作者