?? resultsettest.java
字號:
import java.sql.*;
import java.util.*;
import java.io.*;
import static java.sql.ResultSet.*;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class ResultSetTest
{
private String driver;
private String url;
private String user;
private String pass;
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
public void initParam(String paramFile)throws Exception
{
//使用Properties類來加載屬性文件
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
}
public void query(String sql)throws Exception
{
try
{
//加載驅動
Class.forName(driver);
//獲取數據庫連接
conn = DriverManager.getConnection(url , user , pass);
//使用Connection來創建一個PreparedStatement對象
//傳入控制結果集可滾動,可更新的參數。
pstmt = conn.prepareStatement(sql , ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rs.last();
int rowCount = rs.getRow();
for (int i = rowCount; i > 0 ; i-- )
{
rs.absolute(i);
System.out.println(rs.getString(1) + "\t"
+ rs.getString(2) + "\t" + rs.getString(3));
//修改單元格多對應的值
rs.updateString(2 , "學生名" + i);
//提交修改
rs.updateRow();
}
}
//使用finally塊來關閉數據庫資源
finally
{
if (rs != null)
{
rs.close();
}
if (pstmt != null)
{
pstmt.close();
}
if (conn != null)
{
conn.close();
}
}
}
public static void main(String[] args) throws Exception
{
ResultSetTest rt = new ResultSetTest();
rt.initParam("mysql.ini");
rt.query("select * from student_table");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -