offline Datasets in Java

Source: Internet
Author: User
Tags stmt
1 PrefaceI heard that in Java there has been no good encapsulation of a similar dataset-like offline dataset, but the Java also provides a bad package of offline data set rowset, in the earlier JDK version (jdk1.4 and before) actually has provided a rowset jar package, But the Sun company only provides 5 interfaces, and does not provide the interface realization, but we want to complete the work which we want to have to write the realization, but at the same time, because of the majority of Netizen's request (feel this really inconvenient), Sun also provided a separate Rowset.jar package for download and use on Sun's official website, but the name of the jar package on his Sun official website was jdbc_rowset_tiger-1_0_1- Mrel-jwsdp.zip, in jdk1.5, Sun has rewritten this jar package, so we can easily feel the joy of Sun's offline data set if we download the rowset jar package provided by sun alone or install jdk1.5. 2 Preparation WorkOne of the first things we need to do when using this downloaded offline DataSet is to change it to a jar package that is available in China because the jar package that he provides is used by default in the English-language area, and if not, just call new Cachedrowsetimpl (). You can create a CachedRowSet object, but this is the new Cachedrowsetimpl () that executes this statement when you use it to throw an exception. It's not going to work at all. Workaround:1 decompression jdbc_rowset_tiger-1_0_1-mrel-ri.zip, produce Rowset.jar file. 2 Unzip the Rowset.jar document and locate the Rowsetresourcebundle.properties file in the resulting Com/sun/rowset directory. 3 will rowsetresourcebundle.properties renamed to Rowsetresourcebundle_cn.properties. 4 re-create Rowset.jar package Description: Here _CN said that China. Different countries, this name is different through the above steps can be normal use of the new Cachedrowsetimpl (). 3 INTRODUCTION RowsetJ Ava 5 strengthens support for Java Database Connectivity (JDBC) Which added a new package Javax.sql.rowset,javax.sql.rowset.serial,javax.sql.rowset.spi.      Five new interfaces are specified from the rowset interface: 1. Cachedrowset:cachedrowset can not establish a long-term connection with the data source, only when reading data from the database or writing data to the database will be connected to the database, it provides a lightweight way to access the database, its data are in memory. 2.  Jdbcrowset: Wraps a ResultSet object so that the ResultSet object can be made as a javabeans™ component. 3.  Filteredrowset: Inherits from CachedRowSet, can obtain a subset of data according to the setting condition. 4.  Joinrowset: Inherits from CachedRowSet, you can merge multiple rowset objects into a SQL join statement. 5. Webrowset: Inherited from CachedRowSet, you can export Webrowset objects into XML format. 4 Implementation Because I personally experience this piece of the most useful and most basic is the cachedrowset part, so I just did the CachedRowSet implementation example. CachedRowSet can generate data by calling populate (Resuletset RS), and once the data is obtained, CachedRowSet can disconnect from the database until the connection is written to the database. Example code is as follows: Package src;   Import java.sql.Connection; Import Java.sql.DriverManager; Import Java.sql.ResultSet; Import java.sql.SQLException; Import Javax.sql.RowSet; Import Com.sun.rowset.CachedRowSetImpl;   public class Testcachedrowset {         public static void Testcachedrowset () {             }               public static Com.sun.rowset.CachedRowSetImpl Query_all () throws sqlexception{& nbsp;             Connection conn = null;               String url= "Jdbc:microsoft: Sqlserver://127.0.0.1:1433;databasename=testrowset ";               String user= "sa";               String password= "sa";           //access to database connections                conn= drivermanager.getconnection (Url,user,password);               java.sql.Statement stmt = Conn.createstatement ();              //Query database, get table data                ResultSet rs = stmt.executequery ("SELECT * FROM Test ");                                 // Generates an object of CachedRowSet type based on the ResultSet object               Cachedrowsetimpl CRS = new Cachedrowsetimpl ();               Crs.populate (RS);              //Close resultset                Rs.close ();              //Shutdown of database connections                Conn.close ();               return CRS;       }        public static void Main (string[] args) Throws SQLException {              String driver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver";               Connection conn = null;               String url= "Jdbc:microsoft: Sqlserver://127.0.0.1:1433;databasename=testrowset; Selectmethod=cursor ";//The last part of the note               String user= "SA";               String password= "sa";                       try {                             Class.forName (Driver). newinstance ();                              Cachedrowsetimpl CRS = new Cachedrowsetimpl ();                              Crs=query_all ();                           crs.settablename ("test");                           Crs.next ();                          //change Operation                               crs.updatestring (1, "434");                          /Check Operation                          String id = crs.getstring ("id");                           System.out.println (ID);                          //delete operation                           Crs.deleterow ();                           Crs.updaterow ();                           //regain access to the database                           conn= drivermanager.getconnection (Url,user,password);                          //update cachedrowset content to database                           crs.acceptchanges (conn);                          //close CachedRowSet                           crs.close ();                          //Close database connection                           Conn.close ();                       catch (Instantiationexception e) {                             E.printstacktrace ();                       catch (Illegalaccessexception e) {                             E.printstacktrace ();                       catch (ClassNotFoundException e) {                             E.printstacktrace ();                      }       }} 5 points to note:A problem was found with JDBC: "Java.sql.sqlexception:[microsoft][sqlserver Driver for Jdbc]can ' t start a cloned connection while In manual transaction mode. " After checking the official website of Microsoft, we found that the default setting for the database connection opened when we used JDBC was "direct connection mode in manual mode", in which case the SQL Server database could not be manipulated in bulk, even Microsoft admits it is a design problem. Other databases do not find this problem for the time being, we must modify the connection string as follows: "String url=" jdbc:microsoft:sqlserver://localhost;databasename; Selectmethod=cursor ";" To ensure that our pattern is cursor mode rather than direct mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.