JdbcRowSet & SQL Server(Sqljdbc4.jar)


Create a JdbcRowSet object in various ways:
  • 1. By using the reference implementation constructor that takes a ResultSet object
  • 2. By using the reference implementation constructor that takes a Connection object
  • 3. By using the reference implementation default constructor
  • 4. By using an instance of RowSetFactory, which is created from the class RowSetProvider

Testing Environment :
JDBC driver: Sqljdbc4.jar
Java SE Version:1.7.0_51

Result:
1.Passing ResultSet Objects// It works.
2.Passing Connection Objects// It does not work
3.Using the Default Constructor// It does not work
4.Using the RowSetFactory Interface// It does not work

import com.sun.rowset.JdbcRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

    public static void main(String[] args) throws Exception {
        JdbcRowSet rowSet = null;
        String url = "jdbc:sqlserver://127.0.0.1:1433;databasename=test_Staging; create = true ";
        String selectStr = "select top 10 * from SYS.objects ";
        String username = "test";
        String password = "test234";
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//1. By using the reference implementation constructor that takes a ResultSet object
        //.Passing ResultSet Objects // It works.
        Connection con = DriverManager.getConnection(url, username, password);
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery(selectStr);
        rowSet = new JdbcRowSetImpl(rs);
      
        //CachedRowSet Table_3;
        //Table_3=new CachedRowSetImpl(); // It works.
        //Table_3.populate(rs);
         rs.close();
         //System.out.println(Table_3.getString("xxx"));

//2. By using the reference implementation constructor that takes a Connection object
        //.Passing Connection Objects  // It does not work.
//        rowSet = new JdbcRowSetImpl(con);
//        rowSet.setCommand(selectStr);
//        rowSet.execute();
3. By using the reference implementation default constructor
        //.Using the Default Constructor // It does not work.
        //            rowSet = new JdbcRowSetImpl();
        //            rowSet.setCommand(selectStr);
        //            rowSet.setUrl(url);
        //            rowSet.setUsername(username);
        //            rowSet.setPassword(password);
        //            rowSet.execute();
4. By using an instance of RowSetFactory, which is created from the class RowSetProvider
        //.Using the RowSetFactory Interface  // It does not work.
        //            RowSetFactory myRowSetFactory = null;
        //            myRowSetFactory = RowSetProvider.newFactory();
        //            rowSet = myRowSetFactory.createJdbcRowSet();
        //            rowSet.setUrl(url);
        //            rowSet.setUsername(username);
        //            rowSet.setPassword(password);
        //            rowSet.setCommand(selectStr);
        //            rowSet.execute();

        while (rowSet.next()) {
            System.out.println(rowSet.getString(1) + "\t" + rowSet.getString(2) + "\t" + rowSet.getString(3) + "\t" +   rowSet.getString(4) + "\t" + rowSet.getString("type") + "\t" + rowSet.getString("name"));
        }

    }
Ref: http://docs.oracle.com/javase/tutorial/jdbc/basics/jdbcrowset.html

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE