Java Code Examples for java.sql.PreparedStatement.getMoreResults()

The following are Jave code examples for showing how to use getMoreResults() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: mybatisx   File: DefaultSqlRunner.java   Source Code and License Vote up 7 votes
/**
 * 得到结果集总数
 * @param ps
 * @return
 * @throws SQLException
 */
private long getTotal(PreparedStatement ps) throws SQLException {
 long total=-1;
 ResultSet rs=null;
 ResultSetMetaData rsmd=null;
 String columnName=null;
 
 while(ps.getMoreResults()){
  rs= ps.getResultSet();
  	  rsmd = rs.getMetaData();
        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
      	  columnName=rsmd.getColumnLabel(i + 1);
      	  if(columnName.equalsIgnoreCase(COLUMN_TOTAL_NAME)){
      		  while (rs.next()) {
      			  total=rs.getLong(columnName);
    	  }
      		  break;
      	  }
        }
    }
 return total;
}
 
Example 2
Project: lams   File: ResultSetReturnImpl.java   Source Code and License Vote up 6 votes
@Override
public ResultSet execute(PreparedStatement statement) {
	// sql logged by StatementPreparerImpl
	try {
		final ResultSet rs;
		try {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().startStatementExecution();
			if ( !statement.execute() ) {
				while ( !statement.getMoreResults() && statement.getUpdateCount() != -1 ) {
					// do nothing until we hit the resultset
				}
			}
			rs = statement.getResultSet();
		}
		finally {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().endStatementExecution();
		}
		postExtract( rs, statement );
		return rs;
	}
	catch (SQLException e) {
		throw sqlExceptionHelper.convert( e, "could not execute statement" );
	}
}
 
Example 3
Project: toshi-headless-client   File: SqliteStore.java   Source Code and License Vote up 5 votes
public void executeResourceScript(String resourceFilename) {
    try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourceFilename);
         BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {

        // TODO: must be a nicer way of doing this!
        ArrayList<String> sqls = new ArrayList<String>();
        String insql = "";
        String line = null;
        while ((line = reader.readLine()) != null) {
            insql += line + "\n";
            if (line.endsWith(";")) {
                sqls.add(insql);
                insql = "";
            }
        }
        for (String sql: sqls) {
            PreparedStatement st = conn.prepareStatement(sql);
            boolean results = st.execute();
            while (results) {
                results = st.getMoreResults();
            }
            st.close();
        }

    } catch (IOException x) {
        x.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 4
Project: parabuild-ci   File: EntityPersister.java   Source Code and License Vote up 4 votes
/**
 * Persist an object, using a natively generated identifier
 */
public Serializable insert(Object[] fields, boolean[] notNull, String sql, Object object, SessionImplementor session) 
throws HibernateException {
	
	if ( log.isTraceEnabled() ) {
		log.trace("Inserting entity: " + getClassName() + " (native id)");
		if ( isVersioned() ) log.trace( "Version: " + Versioning.getVersion(fields, this) );
	}
	
	boolean useGetGeneratedKeys = session.getFactory().isGetGeneratedKeysEnabled();
	
	try {
		
		//TODO: refactor all this stuff up to AbstractEntityPersister:
		String insertSelectSQL = useGetGeneratedKeys ? 
			null : 
			getDialect().appendIdentitySelectToInsert(sql);
		if (insertSelectSQL != null) {
			
			//use one statement to insert the row and get the generated id
			PreparedStatement insertSelect = session.getBatcher().prepareStatement(insertSelectSQL);
			try {
				dehydrate(null, fields, notNull, insertSelect, session);
				if ( !insertSelect.execute() ) {
					while ( !insertSelect.getMoreResults() );
				}
				return getGeneratedIdentity( object, session, insertSelect.getResultSet() );
			}
			finally {
				session.getBatcher().closeStatement(insertSelect);
			}
			
		} 
		else {
			
			//do the insert
			PreparedStatement statement = session.getBatcher().prepareStatement(sql, useGetGeneratedKeys);
			try {
				dehydrate(null, fields, notNull, statement, session);
				statement.executeUpdate();
				if (useGetGeneratedKeys) {
					return getGeneratedIdentity( object, session, GetGeneratedKeysHelper.getGeneratedKey(statement) );
				}
			}
			finally {
				session.getBatcher().closeStatement(statement);
			}
			
			//fetch the generated id in a separate query
			PreparedStatement idselect = session.getBatcher().prepareStatement( sqlIdentitySelect() );
			try {
				return getGeneratedIdentity( object, session, idselect.executeQuery() );
			}
			finally {
				session.getBatcher().closeStatement(idselect);
			}
			
		}
		
	}
	catch (SQLException sqle) {
		throw convert( sqle, "could not insert: " +  MessageHelper.infoString(this) );

	}
	
}