List of usage examples for java.sql ResultSet CONCUR_UPDATABLE
int CONCUR_UPDATABLE
To view the source code for java.sql ResultSet CONCUR_UPDATABLE.
Click Source Link
ResultSet
object that may be updated. From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCLogsDAO.java
private List<LogEntry> internalGetLogs(boolean paginate, String resourcePath, int action, String userName, Date from, Date to, boolean descending, Connection conn) throws RegistryException { try {/*from w ww. jav a2 s . c o m*/ String dbName = conn.getMetaData().getDatabaseProductName(); if (dbName.contains("Microsoft") || dbName.equals("Oracle")) { enableApiPagination = "false"; } } catch (SQLException e) { throw new RegistryException("Failed to get Database product name ", e); } if (conn == null) { log.fatal( "Failed to get Logs. Communications link failure. The connection to the database could not be acquired."); throw new RegistryException( "Failed to get Logs. Communications link failure. The connection to the database could not be acquired."); } PreparedStatement s = null; ResultSet results = null; boolean paginated = false; int start = 0; int count = 0; String sortOrder = ""; String sortBy = ""; MessageContext messageContext = null; // enableApiPagination is the value of system property - enable.registry.api.paginating if (enableApiPagination == null || enableApiPagination.equals("true")) { messageContext = MessageContext.getCurrentMessageContext(); if (messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext)) { PaginationContext paginationContext = PaginationUtils.initPaginationContext(messageContext); start = paginationContext.getStart(); count = paginationContext.getCount(); if (start == 0) { start = 1; } sortBy = paginationContext.getSortBy(); sortOrder = paginationContext.getSortOrder(); paginated = paginate; } } String sql = "SELECT REG_PATH, REG_USER_ID, REG_LOGGED_TIME, REG_ACTION, REG_ACTION_DATA FROM " + "REG_LOG"; boolean queryStarted = false; sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action); if (descending) { sql = sql + " ORDER BY REG_LOGGED_TIME DESC"; } try { if (enableApiPagination == null || enableApiPagination.equals("true")) { // TYPE_SCROLL_INSENSITIVE and CONCUR_UPDATABLE should be set to move the cursor through the resultSet s = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); } else { s = conn.prepareStatement(sql); } int paramNumber = 1; if (resourcePath != null) { s.setString(paramNumber, resourcePath); paramNumber++; } if (userName != null) { s.setString(paramNumber, userName); paramNumber++; } if (from != null) { s.setTimestamp(paramNumber, new Timestamp(from.getTime())); paramNumber++; } if (to != null) { s.setTimestamp(paramNumber, new Timestamp(to.getTime())); paramNumber++; } if (action != -1) { s.setInt(paramNumber, action); paramNumber++; } s.setInt(paramNumber, CurrentSession.getTenantId()); results = s.executeQuery(); List<LogEntry> resultList = new ArrayList<LogEntry>(); if (paginated) { //Check start index is a valid one if (results.relative(start)) { //This is to get cursor to correct position to execute results.next(). results.previous(); int i = 0; while (results.next() && i < count) { i++; resultList.add(getLogEntry(results)); } } else { log.debug("start index doesn't exist in the result set"); } //move the cursor to the last index if (results.last()) { log.debug("cursor move to the last index of result set"); } else { log.debug("cursor doesn't move to the last index of result set"); } //set row count to the message context. PaginationUtils.setRowCount(messageContext, Integer.toString(results.getRow())); } else { while (results.next()) { resultList.add(getLogEntry(results)); } LogEntry[] logEntries = resultList.toArray(new LogEntry[resultList.size()]); resultList = Arrays.asList(logEntries); } return resultList; } catch (SQLException e) { String msg = "Failed to get logs. " + e.getMessage(); log.error(msg, e); throw new RegistryException(msg, e); } finally { try { try { if (results != null) { results.close(); } } finally { if (s != null) { s.close(); } } } catch (SQLException ex) { String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR; log.error(msg, ex); } } }
From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java
public void testDataTypes() throws SQLException { conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); Statement stat = conn.createStatement(); PreparedStatement prep;/* w w w . j a v a 2s .co m*/ ResultSet rs; trace("Create tables"); stat.execute("CREATE TABLE T_INT(ID INT PRIMARY KEY,VALUE INT)"); stat.execute("CREATE TABLE T_VARCHAR(ID INT PRIMARY KEY,VALUE VARCHAR(255))"); stat.execute("CREATE TABLE T_DECIMAL_0(ID INT PRIMARY KEY,VALUE DECIMAL(30,0))"); stat.execute("CREATE TABLE T_DECIMAL_10(ID INT PRIMARY KEY,VALUE DECIMAL(20,10))"); stat.execute("CREATE TABLE T_DATETIME(ID INT PRIMARY KEY,VALUE DATETIME)"); prep = conn.prepareStatement("INSERT INTO T_INT VALUES(?,?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); prep.setInt(1, 1); prep.setInt(2, 0); prep.executeUpdate(); prep.setInt(1, 2); prep.setInt(2, -1); prep.executeUpdate(); prep.setInt(1, 3); prep.setInt(2, 3); prep.executeUpdate(); prep.setInt(1, 4); prep.setNull(2, Types.INTEGER); prep.executeUpdate(); prep.setInt(1, 5); prep.setBigDecimal(2, new BigDecimal("0")); prep.executeUpdate(); prep.setInt(1, 6); prep.setString(2, "-1"); prep.executeUpdate(); prep.setInt(1, 7); prep.setObject(2, new Integer(3)); prep.executeUpdate(); prep.setObject(1, "8"); // should throw an exception prep.setObject(2, null); // some databases don't allow calling setObject with null (no data type) prep.executeUpdate(); prep.setInt(1, 9); prep.setObject(2, -4, Types.VARCHAR); prep.executeUpdate(); prep.setInt(1, 10); prep.setObject(2, "5", Types.INTEGER); prep.executeUpdate(); prep.setInt(1, 11); prep.setObject(2, null, Types.INTEGER); prep.executeUpdate(); prep.setInt(1, 12); prep.setBoolean(2, true); prep.executeUpdate(); prep.setInt(1, 13); prep.setBoolean(2, false); prep.executeUpdate(); prep.setInt(1, 14); prep.setByte(2, (byte) -20); prep.executeUpdate(); prep.setInt(1, 15); prep.setByte(2, (byte) 100); prep.executeUpdate(); prep.setInt(1, 16); prep.setShort(2, (short) 30000); prep.executeUpdate(); prep.setInt(1, 17); prep.setShort(2, (short) (-30000)); prep.executeUpdate(); prep.setInt(1, 18); prep.setLong(2, Integer.MAX_VALUE); prep.executeUpdate(); prep.setInt(1, 19); prep.setLong(2, Integer.MIN_VALUE); prep.executeUpdate(); assertTrue(stat.execute("SELECT * FROM T_INT ORDER BY ID")); rs = stat.getResultSet(); assertResultSetOrdered(rs, new String[][] { { "1", "0" }, { "2", "-1" }, { "3", "3" }, { "4", null }, { "5", "0" }, { "6", "-1" }, { "7", "3" }, { "8", null }, { "9", "-4" }, { "10", "5" }, { "11", null }, { "12", "1" }, { "13", "0" }, { "14", "-20" }, { "15", "100" }, { "16", "30000" }, { "17", "-30000" }, { "18", "" + Integer.MAX_VALUE }, { "19", "" + Integer.MIN_VALUE }, }); prep = conn.prepareStatement("INSERT INTO T_DECIMAL_0 VALUES(?,?)"); prep.setInt(1, 1); prep.setLong(2, Long.MAX_VALUE); prep.executeUpdate(); prep.setInt(1, 2); prep.setLong(2, Long.MIN_VALUE); prep.executeUpdate(); prep.setInt(1, 3); prep.setFloat(2, 10); prep.executeUpdate(); prep.setInt(1, 4); prep.setFloat(2, -20); prep.executeUpdate(); prep.setInt(1, 5); prep.setFloat(2, 30); prep.executeUpdate(); prep.setInt(1, 6); prep.setFloat(2, -40); prep.executeUpdate(); rs = stat.executeQuery("SELECT VALUE FROM T_DECIMAL_0 ORDER BY ID"); checkBigDecimal(rs, new String[] { "" + Long.MAX_VALUE, "" + Long.MIN_VALUE, "10", "-20", "30", "-40" }); }
From source file:org.giswater.dao.MainDao.java
public static ResultSet getResultset(Connection connection, String sql, boolean showError) { return getResultset(connection, sql, showError, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); }
From source file:com.alibaba.wasp.jdbc.JdbcConnection.java
private void checkTypeConcurrency(int resultSetType, int resultSetConcurrency) { switch (resultSetType) { case ResultSet.TYPE_FORWARD_ONLY: case ResultSet.TYPE_SCROLL_INSENSITIVE: case ResultSet.TYPE_SCROLL_SENSITIVE: break;/*from ww w. jav a 2s. c om*/ default: throw JdbcException.getInvalidValueException("resultSetType", resultSetType); } switch (resultSetConcurrency) { case ResultSet.CONCUR_READ_ONLY: case ResultSet.CONCUR_UPDATABLE: break; default: throw JdbcException.getInvalidValueException("resultSetConcurrency", resultSetConcurrency); } }
From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCTagsVersionDAO.java
/** * Method to get tags added to the given resource, along with the count. * * @param resourceImpl the resource./*from w w w .java 2 s . c o m*/ * * @return an array of tags (with counts). * @throws RegistryException if an error occurred while getting tags. */ public Tag[] getTagsWithCount(ResourceImpl resourceImpl) throws RegistryException { JDBCDatabaseTransaction.ManagedRegistryConnection conn = JDBCDatabaseTransaction.getConnection(); try { String dbName = conn.getMetaData().getDatabaseProductName(); if (dbName.contains("Microsoft") || dbName.equals("Oracle")) { enableApiPagination = "false"; } } catch (SQLException e) { throw new RegistryException("Failed to get Database product name ", e); } List<Tag> tagList = new ArrayList<Tag>(); ResultSet result = null; PreparedStatement ps = null; boolean paginated = false; int start = 0; int count = 0; String sortOrder = ""; String sortBy = ""; MessageContext messageContext = null; // enableApiPagination is the value of system property - enable.registry.api.paginating if (enableApiPagination == null || enableApiPagination.equals("true")) { messageContext = MessageContext.getCurrentMessageContext(); if (messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext)) { PaginationContext paginationContext = PaginationUtils.initPaginationContext(messageContext); start = paginationContext.getStart(); if (start == 0) { start = 1; } count = paginationContext.getCount(); sortBy = paginationContext.getSortBy(); sortOrder = paginationContext.getSortOrder(); paginated = true; } } try { String sql = "SELECT T.REG_TAG_NAME, COUNT(T.REG_ID) FROM REG_TAG T, REG_RESOURCE_TAG RT " + "WHERE RT.REG_VERSION=? AND T.REG_ID=RT.REG_TAG_ID AND " + "T.REG_TENANT_ID=? AND RT.REG_TENANT_ID=? " + "GROUP BY T.REG_TAG_NAME"; if (paginated) { if (!"".equals(sortBy) && !"".equals(sortOrder)) { sql = sql + " ORDER BY " + sortBy + " " + sortOrder; } } if (enableApiPagination == null || enableApiPagination.equals("true")) { // TYPE_SCROLL_INSENSITIVE and CONCUR_UPDATABLE should be set to move the cursor through the resultSet ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); } else { ps = conn.prepareStatement(sql); } ps.setLong(1, resourceImpl.getVersionNumber()); ps.setInt(2, CurrentSession.getTenantId()); ps.setInt(3, CurrentSession.getTenantId()); result = ps.executeQuery(); if (paginated) { //Check start index is a valid one if (result.relative(start)) { //This is to get cursor to correct position to execute results.next(). result.previous(); int i = 0; while (result.next() && i < count) { i++; tagList.add(getTag(result)); } } else { log.debug("start index doesn't exist in the result set"); } //move the cursor to the last index if (result.last()) { log.debug("cursor move to the last index of result set"); } else { log.debug("cursor doesn't move to the last index of result set"); } //set row count to the message context. PaginationUtils.setRowCount(messageContext, Integer.toString(result.getRow())); } else { while (result.next()) { tagList.add(getTag(result)); } } } catch (SQLException e) { String msg = "Failed to get tags and tag counts of the resource " + resourceImpl.getPath() + ". " + e.getMessage(); log.error(msg, e); throw new RegistryException(msg, e); } finally { try { try { if (result != null) { result.close(); } } finally { if (ps != null) { ps.close(); } } } catch (SQLException ex) { String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR; log.error(msg, ex); } } return tagList.toArray(new Tag[tagList.size()]); }
From source file:com.lewa.crazychapter11.MainActivity.java
private void sqlCon() { try {/*from w w w . j a va 2s .c o m*/ Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); } try { // String url = "jdbc:mysql://192.168.142.128:3306/mysql?user=zzfeihua&password=12345&useUnicode=true&characterEncoding=UTF-8";// ?? String url = "jdbc:mysql://10.0.4.170:3306/lewa?user=root&password=lewa&useUnicode=true&characterEncoding=UTF-8";// ?? Connection conn = (Connection) DriverManager.getConnection(url); // ? Statement stmt = (Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select * from user";// user? ResultSet rs = stmt.executeQuery(sql);// StringBuilder str = new StringBuilder(); while (rs.next()) { str.append(rs.getString(1) + "\n"); } mSetText(str.toString()); rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.apache.openjpa.jdbc.sql.PostgresDictionary.java
private void updatePostgresBlob(Row row, Column col, JDBCStore store, Object ob, Select sel) throws SQLException { JDBCFetchConfiguration fetch = store.getFetchConfiguration(); SQLBuffer sql = sel.toSelect(true, fetch); ResultSet res = null;//from w w w. ja va 2s. c o m DelegatingConnection conn = (DelegatingConnection) store.getConnection(); PreparedStatement stmnt = null; try { stmnt = sql.prepareStatement(conn, fetch, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); setTimeouts(stmnt, fetch, true); res = stmnt.executeQuery(); if (!res.next()) { throw new InternalException(_loc.get("stream-exception")); } int oid = res.getInt(1); if (oid != -1) { conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); if (ob != null) { LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); long size = copy((InputStream) ob, os); lo.truncate((int) size); lo.close(); } else { lom.delete(oid); row.setInt(col, -1); } } else { if (ob != null) { conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); oid = lom.create(); LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); copy((InputStream) ob, os); lo.close(); row.setInt(col, oid); } } } catch (IOException ioe) { throw new StoreException(ioe); } finally { if (res != null) try { res.close(); } catch (SQLException e) { } if (stmnt != null) try { stmnt.close(); } catch (SQLException e) { } if (conn != null) try { conn.close(); } catch (SQLException e) { } } }
From source file:org.apache.openjpa.jdbc.sql.PostgresDictionary.java
public void deleteStream(JDBCStore store, Select sel) throws SQLException { JDBCFetchConfiguration fetch = store.getFetchConfiguration(); SQLBuffer sql = sel.toSelect(true, fetch); ResultSet res = null;/*from www .j a v a2s . c o m*/ DelegatingConnection conn = (DelegatingConnection) store.getConnection(); PreparedStatement stmnt = null; try { stmnt = sql.prepareStatement(conn, fetch, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); setTimeouts(stmnt, fetch, true); res = stmnt.executeQuery(); if (!res.next()) { throw new InternalException(_loc.get("stream-exception")); } int oid = res.getInt(1); if (oid != -1) { conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); lom.delete(oid); } } finally { if (res != null) try { res.close(); } catch (SQLException e) { } if (stmnt != null) try { stmnt.close(); } catch (SQLException e) { } if (conn != null) try { conn.close(); } catch (SQLException e) { } } }
From source file:org.kawanfw.sql.jdbc.ConnectionHttp.java
/** * Creates a <code>Statement</code> object that will generate * <code>ResultSet</code> objects with the given type and concurrency. This * method is the same as the <code>createStatement</code> method above, but * it allows the default result set type and concurrency to be overridden. * The holdability of the created result sets can be determined by calling * {@link #getHoldability}./*from w w w .ja v a2 s.c o m*/ * * @param resultSetType * a result set type; one of * <code>ResultSet.TYPE_FORWARD_ONLY</code>, * <code>ResultSet.TYPE_SCROLL_INSENSITIVE</code>, or * <code>ResultSet.TYPE_SCROLL_SENSITIVE</code> * @param resultSetConcurrency * a concurrency type; one of * <code>ResultSet.CONCUR_READ_ONLY</code> or * <code>ResultSet.CONCUR_UPDATABLE</code> * @return a new <code>Statement</code> object that will generate * <code>ResultSet</code> objects with the given type and * concurrency * @exception SQLException * if a database access error occurs, this method is called * on a closed connection or the given parameters are not * <code>ResultSet</code> constants indicating type and * concurrency * @exception SQLFeatureNotSupportedException * if the JDBC driver does not support this method or this * method is not supported for the specified result set type * and result set concurrency. * @since 1.2 */ @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { testIfClosed(); // We support only ResultSet.CONCUR_READ_ONLY if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) { throw new SQLFeatureNotSupportedException("Concurrency ResultSet.CONCUR_UPDATABLE is not supported."); } return new StatementHttp(this, resultSetType, resultSetConcurrency, getHoldability()); }
From source file:de.innovationgate.webgate.api.jdbc.custom.JDBCSource.java
private ResultSet getTableResultSet(String folder, String specify, boolean updatable) throws SQLException { if (!_tables.containsKey(folder.toLowerCase())) { return null; }/* ww w .j av a 2 s.c o m*/ StringBuffer query = new StringBuffer(); query.append("SELECT * FROM " + folder); if (specify != null) { query.append(" WHERE ").append(specify); } Statement stmt = getConnection().createStatement(_resultSetType, (updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY)); stmt.execute(query.toString()); ResultSet resultSet = stmt.getResultSet(); return resultSet; }