Java CachedRowSet相关操作

原创 Laughing  2017-11-06 23:02  阅读 44 次 评论 0 条

CachedRowSet读取以及分页等相关操作

  1. package Sqlserver;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.sql.DriverManager;  
  7. import java.sql.PreparedStatement;  
  8. import java.sql.ResultSet;  
  9. import java.util.Properties;  
  10.   
  11. import javax.sql.rowset.CachedRowSet;  
  12. import javax.sql.rowset.JdbcRowSet;  
  13. import javax.sql.rowset.RowSetFactory;  
  14. import javax.sql.rowset.RowSetProvider;  
  15.   
  16. import com.mysql.jdbc.Connection;  
  17.   
  18. public class JdbcRowSetTest {  
  19.   
  20.     String driver;  
  21.     String url;  
  22.     String userName;  
  23.     String passWord;  
  24.     String databaseName;  
  25.   
  26.     public void Init() {  
  27.         Properties properties = new Properties();  
  28.         try {  
  29.             properties.load(new FileInputStream("sqlserver.ini"));  
  30.             driver = properties.getProperty("driver");  
  31.             url = properties.getProperty("url");  
  32.             userName = properties.getProperty("userName");  
  33.             passWord = properties.getProperty("passWord");  
  34.             databaseName = properties.getProperty("databaseName");  
  35.         } catch (FileNotFoundException e) {  
  36.             // TODO Auto-generated catch block  
  37.             e.printStackTrace();  
  38.         } catch (IOException e) {  
  39.             // TODO Auto-generated catch block  
  40.             e.printStackTrace();  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     public void update() throws Exception {  
  46.         Class.forName(driver);  
  47.         try {  
  48.             java.sql.Connection connection = DriverManager.getConnection(url, userName, passWord);  
  49.             PreparedStatement preparedStatement = connection.prepareStatement("select * from t_brand");  
  50.             ResultSet resultSet = preparedStatement.executeQuery();  
  51.             RowSetFactory rowSetFactory = RowSetProvider.newFactory();  
  52.             CachedRowSet cachedRowSet = rowSetFactory.createCachedRowSet();  
  53.             cachedRowSet.setPageSize(10);  
  54.             cachedRowSet.populate(resultSet,0);  
  55.             System.out.println(cachedRowSet.size());  
  56.             resultSet.close();  
  57.             preparedStatement.close();  
  58.             connection.close();  
  59.             cachedRowSet.afterLast();  
  60.               
  61.         } catch (Exception exception) {  
  62.             System.out.println(exception.getMessage());  
  63.         }  
  64.     }  
  65.   
  66.     public static void main(String[] args) throws Exception {  
  67.         // TODO Auto-generated method stub  
  68.         JdbcRowSetTest jdbcRowSetTest = new JdbcRowSetTest();  
  69.         jdbcRowSetTest.Init();  
  70.         jdbcRowSetTest.update();  
  71.     }  
  72.   
  73. }  
本文地址:https://www.lisen.me/java-cachedrowset-related-operations.html
版权声明:本文为原创文章,版权归 木子网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情