Java JDBC JoinRowSet create

Description

Java JDBC JoinRowSet create

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.JoinRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

import com.sun.rowset.JoinRowSetImpl;

public class Main {

    public static Connection conn = null;
    public static CachedRowSet bookAuthors = null;
    public static CachedRowSet authorWork = null;
    public static JoinRowSet jrs = null;

    public static void main(Connection c) {
        try {//from ww w .j a v a  2  s.  c  o  m
            conn = c;
            queryBookAuthor();
            queryAuthorWork();
            joinRowQuery();
        } catch (Exception ex) {
            System.out.println(ex);
        } finally {
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (bookAuthors != null) {
                try {
                    bookAuthors.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (authorWork != null) {
                try {
                    authorWork.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (jrs != null) {
                try {
                    jrs.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

    public static void queryBookAuthor() {
        RowSetFactory factory;
        try {
            factory = RowSetProvider.newFactory();
            bookAuthors = factory.createCachedRowSet();
            // set CachedRowSet connection settings
            // crs.setUsername(createConn.getUsername());
            // crs.setPassword(createConn.getPassword());
            // crs.setUrl(createConn.getJdbcUrl());

            bookAuthors.setCommand("SELECT ID, LAST, FIRST FROM BOOK_AUTHOR");
            bookAuthors.execute(conn);
            while (bookAuthors.next()) {
                System.out.println(bookAuthors.getString(1) + ": " + bookAuthors.getString(2)
                        + ", " + bookAuthors.getString(3));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public static void queryAuthorWork() {
        RowSetFactory factory;
        try {
            factory = RowSetProvider.newFactory();
            authorWork = factory.createCachedRowSet();
            // set the CachedRowSet connection settings
            // crs.setUsername(createConn.getUsername());
            // crs.setPassword(createConn.getPassword());
            // crs.setUrl(createConn.getJdbcUrl());
            authorWork.setCommand("SELECT ID, AUTHOR_ID, CHAPTER_NUMBER, CHAPTER_TITLE FROM AUTHOR_WORK");
            authorWork.execute(conn);
            while (authorWork.next()) {
                System.out.println(authorWork.getString(1) + ": " + authorWork.getInt(3)
                        + " - " + authorWork.getString(4));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void joinRowQuery() {
        try {
            jrs = new JoinRowSetImpl();
            jrs.addRowSet(bookAuthors, 1);
            jrs.addRowSet(authorWork, 2);
            while(jrs.next()){
                System.out.println(jrs.getInt("CHAPTER_NUMBER") + ": " + 
                                   jrs.getString("CHAPTER_TITLE") + " - " +
                                   jrs.getString("FIRST") + " " + 
                                   jrs.getString("LAST"));
            }            
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
            
    }
}



PreviousNext

Related