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