Example usage for java.sql ResultSet CONCUR_READ_ONLY

List of usage examples for java.sql ResultSet CONCUR_READ_ONLY

Introduction

In this page you can find the example usage for java.sql ResultSet CONCUR_READ_ONLY.

Prototype

int CONCUR_READ_ONLY

To view the source code for java.sql ResultSet CONCUR_READ_ONLY.

Click Source Link

Document

The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.

Usage

From source file:DemoScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;//  ww w.jav  a  2s. c  o m
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id, name from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
        // scroll from the bottom
        rs.afterLast();
        while (rs.previous()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // release database resources
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:GetNumberOfRowsScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;/* w  w  w . ja va 2  s. co m*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            System.out.println("id=" + id);
        }
        // move to the end of the result set
        rs.last();
        // get the row number of the last row which is also the row count
        int rowCount = rs.getRow();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:ReverseSelect.java

public static void main(String argv[]) {
    Connection con = null;//from ww  w  .j av  a2  s .c  om

    try {
        String url = "jdbc:msql://carthage.imaginary.com/ora";
        String driver = "com.imaginary.sql.msql.MsqlDriver";
        Properties p = new Properties();
        Statement stmt;
        ResultSet rs;

        p.put("user", "borg");
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url, "borg", "");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery("SELECT * from test ORDER BY test_id");
        // as a new ResultSet, rs is currently positioned
        // before the first row
        System.out.println("Got results:");
        // position rs after the last row
        rs.afterLast();
        while (rs.previous()) {
            int a = rs.getInt("test_id");
            String str = rs.getString("test_val");

            System.out.print("\ttest_id= " + a);
            System.out.println("/str= '" + str + "'");
        }
        System.out.println("Done.");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:TypeConcurrency.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;// w  w  w  . j  av  a  2 s. com
    Statement stmt;
    try {

        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

        ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");

        int type = srs.getType();

        System.out.println("srs is type " + type);

        int concur = srs.getConcurrency();

        System.out.println("srs has concurrency " + concur);
        while (srs.next()) {
            String name = srs.getString("COF_NAME");
            int id = srs.getInt("SUP_ID");
            float price = srs.getFloat("PRICE");
            int sales = srs.getInt("SALES");
            int total = srs.getInt("TOTAL");
            System.out.print(name + "   " + id + "   " + price);
            System.out.println("   " + sales + "   " + total);
        }

        srs.close();
        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("-----SQLException-----");
        System.err.println("SQLState:  " + ex.getSQLState());
        System.err.println("Message:  " + ex.getMessage());
        System.err.println("Vendor:  " + ex.getErrorCode());
    }
}

From source file:Main.java

public static void showProperty(ResultSet rset) throws SQLException {
    switch (rset.getType()) {
    case ResultSet.TYPE_FORWARD_ONLY:
        System.out.println("Result set type: TYPE_FORWARD_ONLY");
        break;/*  w  w  w  . j av  a2  s.c  om*/
    case ResultSet.TYPE_SCROLL_INSENSITIVE:
        System.out.println("Result set type: TYPE_SCROLL_INSENSITIVE");
        break;
    case ResultSet.TYPE_SCROLL_SENSITIVE:
        System.out.println("Result set type: TYPE_SCROLL_SENSITIVE");
        break;
    default:
        System.out.println("Invalid type");
        break;
    }
    switch (rset.getConcurrency()) {
    case ResultSet.CONCUR_UPDATABLE:
        System.out.println("Result set concurrency: ResultSet.CONCUR_UPDATABLE");
        break;
    case ResultSet.CONCUR_READ_ONLY:
        System.out.println("Result set concurrency: ResultSet.CONCUR_READ_ONLY");
        break;
    default:
        System.out.println("Invalid type");
        break;
    }
    System.out.println("fetch size: " + rset.getFetchSize());
}

From source file:com.javacreed.examples.spring.StreamingStatementCreator.java

@Override
public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
    final PreparedStatement statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
    statement.setFetchSize(Integer.MIN_VALUE);
    return statement;
}

From source file:com.dsclab.loader.export.DBClient.java

public DBClient(final String url) throws ClassNotFoundException, SQLException {
    Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
    try {//  w ww.j av a  2 s.  co  m
        con = DriverManager.getConnection(url);
        con.setAutoCommit(false);
        stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(1000);
        stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
        md = con.getMetaData();
    } catch (RuntimeException ex) {
        //LOG.error("Could not connect",ex);
    }
}

From source file:com.dsclab.loader.loader.DBClient.java

public DBClient(final String url, final String driverClass) throws ClassNotFoundException, SQLException {
    if (driverClass != null) {
        Class.forName(driverClass);
    }/*from   ww w  .ja v  a 2  s  .  c  o m*/
    try {
        con = DriverManager.getConnection(url);
        con.setAutoCommit(false);
        stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(1000);
        stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
        md = con.getMetaData();
    } catch (RuntimeException ex) {
        //LOG.error("Could not connect",ex);
    }
}

From source file:edu.psu.citeseerx.disambiguation.CsxDisambiguation.java

public static void createBlocks(ListableBeanFactory factory) throws Exception {
    String dirpath = "data/csauthors/blocks";

    DataSource dataSource = (DataSource) factory.getBean("csxDataSource");

    PreparedStatement st = dataSource.getConnection().prepareStatement("SELECT * FROM authors",
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    st.setFetchSize(Integer.MIN_VALUE);
    ResultSet rs = st.executeQuery();

    initDirectories(dirpath);//from w  ww  . java  2 s  .  co m

    CsxAuthorFilter filter = (CsxAuthorFilter) factory.getBean("csxAuthorFilter");
    //new CsxAuthorFilter("data/csauthors/name_stopwords.txt");
    BufferedWriter skip = new BufferedWriter(new FileWriter("skip.txt"));

    int count = 0;
    Map<String, List<String>> blocks = new HashMap<String, List<String>>();
    while (rs.next()) {
        count++;
        if ((count % 10000) == 0)
            System.out.println("#Auth:" + count);
        String rsname = rs.getString("name");
        if (!filter.isStopword(rsname) && !filter.isInstitute(rsname) && !filter.isPosition(rsname)) {

            CsxAuthor auth = new CsxAuthor(rs);
            String lname = auth.getLastName();
            String fname = auth.getFirstName();

            if ((lname != null) && (fname != null)) {
                if ((lname.charAt(0) >= 'A') && (lname.charAt(0) <= 'Z') && (fname.charAt(0) >= 'A')
                        && (fname.charAt(0) <= 'Z') && !((fname.length() == 1) && (lname.length() == 1))
                        && !(lname.matches(".*/.*"))) {

                    String l_init = lname.substring(0, 1).toUpperCase();
                    String f_init = fname.substring(0, 1).toUpperCase();
                    String key = l_init + f_init + "/" + lname.toLowerCase() + "_" + f_init.toLowerCase()
                            + ".txt";

                    List<String> list;
                    if (!blocks.containsKey(key)) {
                        list = new ArrayList<String>();
                        blocks.put(key, list);
                    } else {
                        list = blocks.get(key);
                    }
                    list.add(auth.getId());
                } else {
                    skip.write("SKIP: [" + rsname + "]\n");
                }
            }
        }
    }
    skip.close();

    for (String key : blocks.keySet()) {
        List<String> aids = blocks.get(key);
        // only care about cluster with more than one document
        if (aids.size() > 1) {
            BufferedWriter out = new BufferedWriter(new FileWriter(dirpath + "/" + key));
            for (String aid : aids) {
                out.write(aid + "\n");
            }
            out.close();
        }
    }
}

From source file:RetrieveMusicFiles.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   www.j a v  a  2 s  .  co  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String android_id = request.getParameter("androidId");

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {

        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

        String sql = "select a.album_title, a.album_artist, a.album_composer, a.release_year, "
                + "s.song_title, s.genre, s.artist_name from album a, song s, user_music_data u "
                + "where u.android_id='" + android_id + "' and u.song_id=s.song_id and s.album_id=a.album_id";
        rs = stmt.executeQuery(sql);
        JSONObject json = new JSONObject();
        JSONArray jArray = new JSONArray();
        while (rs.next()) {
            JSONObject element = new JSONObject();
            element.put("title", rs.getString("s.song_title"));
            element.put("album", rs.getString("a.album_title"));
            element.put("artist", rs.getString("s.artist_name"));
            element.put("genre", rs.getString("s.genre"));
            element.put("album_artist", rs.getString("a.album_artist"));
            element.put("album_composer", rs.getString("a.album_composer"));
            element.put("album_year", rs.getString("a.release_year"));

            jArray.put(element);
        }
        json.put("data", jArray);
        out.print(json);
        //out.print(resultJSON);
    } catch (Exception se) {
        //out.println("Exception preparing or processing query: " + se);
        se.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {

        }
    }
}