本文整理匯總了Java中java.sql.Statement.getResultSet方法的典型用法代碼示例。如果您正苦於以下問題:Java Statement.getResultSet方法的具體用法?Java Statement.getResultSet怎麽用?Java Statement.getResultSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.Statement
的用法示例。
在下文中一共展示了Statement.getResultSet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testFixForBug1592
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Checks fix for BUG#1592 -- cross-database updatable result sets are not
* checked for updatability correctly.
*
* @throws Exception
* if the test fails.
*/
public void testFixForBug1592() throws Exception {
if (versionMeetsMinimum(4, 1)) {
Statement updatableStmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
try {
updatableStmt.execute("SELECT * FROM mysql.user");
this.rs = updatableStmt.getResultSet();
} catch (SQLException sqlEx) {
String message = sqlEx.getMessage();
if ((message != null) && (message.indexOf("denied") != -1)) {
System.err.println("WARN: Can't complete testFixForBug1592(), access to 'mysql' database not allowed");
} else {
throw sqlEx;
}
}
}
}
示例2: testBug25009
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Tests fix for BUG#25009 - Results from updates not handled correctly in
* multi-statement queries.
*
* @throws Exception
* if the test fails.
*/
public void testBug25009() throws Exception {
if (!versionMeetsMinimum(4, 1)) {
return;
}
Properties props = new Properties();
props.setProperty("allowMultiQueries", "true");
Connection multiConn = getConnectionWithProps(props);
createTable("testBug25009", "(field1 INT)");
try {
Statement multiStmt = multiConn.createStatement();
multiStmt.execute("SELECT 1;SET @a=1; SET @b=2; SET @c=3; INSERT INTO testBug25009 VALUES (1)");
assertEquals(-1, multiStmt.getUpdateCount());
this.rs = multiStmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(multiStmt.getMoreResults(), false);
for (int i = 0; i < 3; i++) {
assertEquals(0, multiStmt.getUpdateCount());
assertEquals(multiStmt.getMoreResults(), false);
}
assertEquals(1, multiStmt.getUpdateCount());
this.rs = multiStmt.executeQuery("SELECT field1 FROM testBug25009");
assertTrue(this.rs.next());
assertEquals(1, this.rs.getInt(1));
} finally {
if (multiConn != null) {
multiConn.close();
}
}
}
示例3: test
import java.sql.Statement; //導入方法依賴的package包/類
protected boolean test(Statement aStatement) {
try {
//execute the SQL
try {
aStatement.execute(getSql());
} catch (SQLException s) {
throw new Exception("Expected a ResultSet containing "
+ getCountWeWant()
+ " rows, but got the error: "
+ s.getMessage());
}
//check that update count != -1
if (aStatement.getUpdateCount() != -1) {
throw new Exception(
"Expected a ResultSet, but got an update count of "
+ aStatement.getUpdateCount());
}
//iterate over the ResultSet
ResultSet results = aStatement.getResultSet();
int count = 0;
while (results.next()) {
count++;
}
//check that we got as many rows as expected
if (count != getCountWeWant()) {
//we don't have the expected number of rows
throw new Exception("Expected the ResultSet to contain "
+ getCountWeWant()
+ " rows, but it contained " + count
+ " rows.");
}
} catch (Exception x) {
message = x.toString();
return false;
}
return true;
}
示例4: checkPingQuery
import java.sql.Statement; //導入方法依賴的package包/類
private void checkPingQuery(Connection c) throws SQLException {
// Yes, I know we're sending 2, and looking for 1 that's part of the test, since we don't _really_ send the query to the server!
String aPingQuery = "/* ping */ SELECT 2";
Statement pingStmt = c.createStatement();
PreparedStatement pingPStmt = null;
this.rs = pingStmt.executeQuery(aPingQuery);
assertTrue(this.rs.next());
assertEquals(this.rs.getInt(1), 1);
assertTrue(pingStmt.execute(aPingQuery));
this.rs = pingStmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(this.rs.getInt(1), 1);
pingPStmt = c.prepareStatement(aPingQuery);
assertTrue(pingPStmt.execute());
this.rs = pingPStmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(this.rs.getInt(1), 1);
this.rs = pingPStmt.executeQuery();
assertTrue(this.rs.next());
assertEquals(this.rs.getInt(1), 1);
}
示例5: getAllMentorsWithoutMentees
import java.sql.Statement; //導入方法依賴的package包/類
@Override
public ArrayList<Mentor> getAllMentorsWithoutMentees() throws SQLException {
Mentor u = null;
ArrayList<Mentor> mentorList = new ArrayList<Mentor>();
Statement stm = c.createStatement();
stm.executeQuery(SQLStatements.getAllMentorsWithoutMentees);
ResultSet r = stm.getResultSet();
while (r.next()) {
City city = getCityById(r.getInt(DataContract.UsersTable.COL_CITYID));
Area area = getAreaById(r.getInt(DataContract.UsersTable.COL_AREAID));
u = new Mentor(r.getInt(DataContract.UsersTable.COL_ID), r.getString(DataContract.UsersTable.COL_FIRSTNAME),
r.getString(DataContract.UsersTable.COL_LASTNAME), r.getString(DataContract.UsersTable.COL_EMAIL),
r.getString(DataContract.UsersTable.COL_PHONENUMBER),
r.getString(DataContract.UsersTable.COL_PASSWORD), r.getInt(DataContract.UsersTable.COL_GENDER),
r.getString(DataContract.UsersTable.COL_ADDRESS), r.getString(DataContract.UsersTable.COL_NOTES),
r.getString(DataContract.UsersTable.COL_PROFILEPICTURE),
r.getBoolean(DataContract.UsersTable.COL_ACTIVE), userType.MENTOR,
r.getInt(DataContract.UsersTable.COL_AREAID), area.getName(),
r.getInt(DataContract.UsersTable.COL_CITYID), city.getName(),
r.getDate(DataContract.UsersTable.COL_JOINDATE),
r.getString(DataContract.MentorsTable.COL_EXPERIENCE),
r.getString(DataContract.MentorsTable.COL_ROLE), r.getInt(DataContract.MentorsTable.COL_COMPANY),
r.getString(DataContract.MentorsTable.COL_VOLUNTEERING),
r.getString(DataContract.MentorsTable.COL_WORKHISTORY));
mentorList.add(u);
}
r.close();
stm.close();
return mentorList;
}
示例6: testFive
import java.sql.Statement; //導入方法依賴的package包/類
public void testFive() throws SQLException {
Connection conn = newConnection();
Statement st = conn.createStatement();
st.execute(
"create function func_table (in namep varchar(128)) returns table(cola varchar(128), colb varchar(128)) "
+ "return table(select schema_name, schema_owner from information_schema.schemata where schema_owner=namep);");
CallableStatement cs = conn.prepareCall("call func_table('_SYSTEM')");
boolean isResult = cs.execute();
assertTrue(isResult);
ResultSet rs = cs.getResultSet();
rs.next();
assertEquals(rs.getString(1), "INFORMATION_SCHEMA");
assertEquals(rs.getString(2), "_SYSTEM");
rs.close();
//
isResult = st.execute("call func_table('_SYSTEM')");
assertTrue(isResult);
rs = st.getResultSet();
rs.next();
assertEquals(rs.getString(1), "INFORMATION_SCHEMA");
assertEquals(rs.getString(2), "_SYSTEM");
rs.close();
}
示例7: isStatementClosedForTestBug69746
import java.sql.Statement; //導入方法依賴的package包/類
private boolean isStatementClosedForTestBug69746(Statement statement) {
try {
statement.getResultSet();
} catch (SQLException ex) {
return ex.getMessage().equalsIgnoreCase(Messages.getString("Statement.49"));
}
return false;
}
示例8: execQuery
import java.sql.Statement; //導入方法依賴的package包/類
public void execQuery(String query, ResultHandler handler) throws SQLException {
Statement stmt = connection.createStatement();
stmt.execute(query);
ResultSet result = stmt.getResultSet();
handler.handle(result);
result.close();
stmt.close();
}
示例9: testBug58728
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Tests fix for Bug#58728, NPE in com.mysql.jdbc.jdbc2.optional.StatementWrappe.getResultSet()
* ((com.mysql.jdbc.ResultSetInternalMethods) rs).setWrapperStatement(this);
* when rs is null
*/
public void testBug58728() throws Exception {
createTable("testbug58728", "(Id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, txt VARCHAR(50))", "InnoDB");
this.stmt.executeUpdate("INSERT INTO testbug58728 VALUES (NULL, 'Text 1'), (NULL, 'Text 2')");
MysqlConnectionPoolDataSource pds = new MysqlConnectionPoolDataSource();
pds.setUrl(dbUrl);
Statement stmt1 = pds.getPooledConnection().getConnection().createStatement();
stmt1.executeUpdate("UPDATE testbug58728 SET txt = 'New text' WHERE Id > 0");
ResultSet rs1 = stmt1.getResultSet();
stmt1.close();
if (rs1 != null) {
rs1.close();
}
}
示例10: performQuery
import java.sql.Statement; //導入方法依賴的package包/類
@NonNull
protected ResultSet performQuery(MariaDBContainer containerRule, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(containerRule.getJdbcUrl());
hikariConfig.setUsername(containerRule.getUsername());
hikariConfig.setPassword(containerRule.getPassword());
HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();
resultSet.next();
return resultSet;
}
示例11: testBug71396StatementCheck
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Executes one query using a Statement and tests if the results count is the expected.
*/
private void testBug71396StatementCheck(Statement testStmt, String query, int expRowCount) throws SQLException {
ResultSet testRS;
testRS = testStmt.executeQuery(query);
assertTrue(testRS.last());
assertEquals(String.format("Wrong number of rows for query '%s'", query), expRowCount, testRS.getRow());
testRS.close();
testStmt.execute(query);
testRS = testStmt.getResultSet();
assertTrue(testRS.last());
assertEquals(String.format("Wrong number of rows for query '%s'", query), expRowCount, testRS.getRow());
testRS.close();
}
示例12: executeSqlStatement
import java.sql.Statement; //導入方法依賴的package包/類
/**
* @param dbstmt Database sql-Statement
* @param statement sql-Statement
* @throws Exception SQL-Exception while executing sqlstatement
*/
private void executeSqlStatement (Statement dbstmt, String statement)
throws Exception {
try {
boolean isResultSet = dbstmt.execute(statement); //only allows SQL INSERT, UPDATE or DELETE
if(isResultSet) {
ResultSet rs=dbstmt.getResultSet();
StringBuffer res=new StringBuffer();
int colCount=rs.getMetaData().getColumnCount();
String sep="";
while(rs.next()) {
for(int i=1;i<=colCount;i++) {
String value=rs.getString(i);
if(value!=null) {
res.append(sep);
res.append(value);
sep=" ";
}
}
if(res.length()>0) {
log.lifecycle(taskName + ": " + res.toString());
}
res.setLength(0);
sep="";
}
}else {
int modifiedLines=dbstmt.getUpdateCount();
if (modifiedLines==1) {
log.lifecycle(taskName + ": " + modifiedLines + " Line has been modified.");
} else if (modifiedLines>1) {
log.lifecycle(taskName +": " + modifiedLines + " Lines have been modified.");
} else if (modifiedLines<1){
log.lifecycle(taskName + ": No Line has been modified.");
}
}
} catch (SQLException ex) {
throw new SQLException("Error while executing the sqlstatement. " + ex.getMessage());
} finally {
dbstmt.close();
}
}
示例13: testBug9704
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Tests multiple statement support with fix for BUG#9704.
*
* @throws Exception
*/
public void testBug9704() throws Exception {
if (versionMeetsMinimum(4, 1)) {
Connection multiStmtConn = null;
Statement multiStmt = null;
try {
Properties props = new Properties();
props.setProperty("allowMultiQueries", "true");
multiStmtConn = getConnectionWithProps(props);
multiStmt = multiStmtConn.createStatement();
multiStmt.executeUpdate("DROP TABLE IF EXISTS testMultiStatements");
multiStmt.executeUpdate("CREATE TABLE testMultiStatements (field1 VARCHAR(255), field2 INT, field3 DOUBLE)");
multiStmt.executeUpdate("INSERT INTO testMultiStatements VALUES ('abcd', 1, 2)");
multiStmt.execute("SELECT field1 FROM testMultiStatements WHERE field1='abcd'; UPDATE testMultiStatements SET field3=3;"
+ "SELECT field3 FROM testMultiStatements WHERE field3=3");
this.rs = multiStmt.getResultSet();
assertTrue(this.rs.next());
assertTrue("abcd".equals(this.rs.getString(1)));
this.rs.close();
// Next should be an update count...
assertTrue(!multiStmt.getMoreResults());
assertTrue("Update count was " + multiStmt.getUpdateCount() + ", expected 1", multiStmt.getUpdateCount() == 1);
assertTrue(multiStmt.getMoreResults());
this.rs = multiStmt.getResultSet();
assertTrue(this.rs.next());
assertTrue(this.rs.getDouble(1) == 3);
// End of multi results
assertTrue(!multiStmt.getMoreResults());
assertTrue(multiStmt.getUpdateCount() == -1);
} finally {
if (multiStmt != null) {
multiStmt.executeUpdate("DROP TABLE IF EXISTS testMultiStatements");
multiStmt.close();
}
if (multiStmtConn != null) {
multiStmtConn.close();
}
}
}
}
示例14: test
import java.sql.Statement; //導入方法依賴的package包/類
protected boolean test(Statement aStatement) {
try {
//execute the SQL
try {
aStatement.execute(getSql());
} catch (SQLException s) {
throw new Exception("Expected a ResultSet containing "
+ getCountWeWant()
+ " rows, but got the error: "
+ s.getMessage());
}
//check that update count != -1
if (aStatement.getUpdateCount() != -1) {
throw new Exception(
"Expected a ResultSet, but got an update count of "
+ aStatement.getUpdateCount());
}
//iterate over the ResultSet
ResultSet results = aStatement.getResultSet();
int count = 0;
while (results.next()) {
count++;
}
//check that we got as many rows as expected
if (count != getCountWeWant()) {
//we don't have the expected number of rows
throw new Exception("Expected the ResultSet to contain "
+ getCountWeWant()
+ " rows, but it contained " + count
+ " rows.");
}
} catch (Exception x) {
message = x.getMessage();
return false;
}
return true;
}
示例15: testBug33678
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Tests fix for Bug#33678 - Multiple result sets not supported in
* "streaming" mode. This fix covers both normal statements, and stored
* procedures, with the exception of stored procedures with registered
* OUTPUT parameters, which can't be used at all with "streaming" result
* sets.
*
* @throws Exception
*/
public void testBug33678() throws Exception {
if (!versionMeetsMinimum(4, 1)) {
return;
}
createTable("testBug33678", "(field1 INT)");
Connection multiConn = getConnectionWithProps("allowMultiQueries=true");
Statement multiStmt = multiConn.createStatement();
try {
multiStmt.setFetchSize(Integer.MIN_VALUE);
multiStmt.execute("SELECT 1 UNION SELECT 2; INSERT INTO testBug33678 VALUES (1); UPDATE testBug33678 set field1=2; "
+ "INSERT INTO testBug33678 VALUES(3); UPDATE testBug33678 set field1=2 WHERE field1=3; UPDATE testBug33678 set field1=2; SELECT 1");
this.rs = multiStmt.getResultSet();
this.rs.next();
assertEquals("1", this.rs.getString(1));
assertFalse(multiStmt.getMoreResults());
assertEquals(1, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(1, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(1, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(1, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(2, multiStmt.getUpdateCount());
assertTrue(multiStmt.getMoreResults());
this.rs = multiStmt.getResultSet();
this.rs.next();
assertEquals("1", this.rs.getString(1));
this.rs.close();
multiStmt.execute("INSERT INTO testBug33678 VALUES (1); INSERT INTO testBug33678 VALUES (1), (2); INSERT INTO testBug33678 VALUES (1), (2), (3)");
assertEquals(1, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(2, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults());
assertEquals(3, multiStmt.getUpdateCount());
assertFalse(multiStmt.getMoreResults() && multiStmt.getUpdateCount() == -1);
this.rs.close();
if (versionMeetsMinimum(5, 0)) {
createProcedure("spBug33678", "() BEGIN SELECT 1; SELECT 2; SELECT 3; END");
CallableStatement cStmt = multiConn.prepareCall("{CALL spBug33678()}");
cStmt.setFetchSize(Integer.MIN_VALUE);
cStmt.execute();
for (int i = 0; i < 2; i++) {
if (i != 0) {
assertTrue(cStmt.getMoreResults());
}
this.rs = cStmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(i + 1, this.rs.getInt(1));
}
}
} finally {
multiStmt.close();
multiConn.close();
}
}