resultsettest.java
来自「jtds的源码 是你学习java的好东西」· Java 代码 · 共 1,736 行 · 第 1/5 页
JAVA
1,736 行
stmt2.close();
rs.close();
}
/**
* Test for bug [1028881] statement.execute() causes wrong ResultSet type.
*/
public void testResultSetScroll3() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #resultSetScroll3 (data INT)");
stmt.execute("CREATE PROCEDURE #procResultSetScroll3 AS SELECT data FROM #resultSetScroll3");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #resultSetScroll3 (data) VALUES (?)");
pstmt.setInt(1, 1);
assertEquals(1, pstmt.executeUpdate());
pstmt.close();
// Test plain Statement
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
assertTrue("Was expecting a ResultSet", stmt2.execute("SELECT data FROM #resultSetScroll3"));
ResultSet rs = stmt2.getResultSet();
assertEquals("ResultSet not scrollable", ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
rs.close();
stmt2.close();
// Test PreparedStatement
pstmt = con.prepareStatement("SELECT data FROM #resultSetScroll3", ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
assertTrue("Was expecting a ResultSet", pstmt.execute());
rs = pstmt.getResultSet();
assertEquals("ResultSet not scrollable", ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
rs.close();
pstmt.close();
// Test CallableStatement
CallableStatement cstmt = con.prepareCall("{call #procResultSetScroll3}",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
assertTrue("Was expecting a ResultSet", cstmt.execute());
rs = cstmt.getResultSet();
assertEquals("ResultSet not scrollable", ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
rs.close();
cstmt.close();
}
/**
* Test for bug [1008208] 0.9-rc1 updateNull doesn't work.
*/
public void testResultSetUpdate1() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #resultSetUpdate1 (id INT PRIMARY KEY, dsi SMALLINT NULL, di INT NULL)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #resultSetUpdate1 (id, dsi, di) VALUES (?, ?, ?)");
pstmt.setInt(1, 1);
pstmt.setShort(2, (short) 1);
pstmt.setInt(3, 1);
assertEquals(1, pstmt.executeUpdate());
pstmt.close();
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.executeQuery("SELECT id, dsi, di FROM #resultSetUpdate1");
ResultSet rs = stmt.getResultSet();
assertNotNull(rs);
assertTrue(rs.next());
rs.updateNull("dsi");
rs.updateNull("di");
rs.updateRow();
rs.moveToInsertRow();
rs.updateInt(1, 2);
rs.updateNull("dsi");
rs.updateNull("di");
rs.insertRow();
stmt.close();
rs.close();
stmt = con.createStatement();
stmt.executeQuery("SELECT id, dsi, di FROM #resultSetUpdate1 ORDER BY id");
rs = stmt.getResultSet();
assertNotNull(rs);
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
rs.getShort(2);
assertTrue(rs.wasNull());
rs.getInt(3);
assertTrue(rs.wasNull());
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
rs.getShort(2);
assertTrue(rs.wasNull());
rs.getInt(3);
assertTrue(rs.wasNull());
assertFalse(rs.next());
stmt.close();
rs.close();
}
/**
* Test for bug [1009233] ResultSet getColumnName, getColumnLabel return wrong values
*/
public void testResultSetColumnName1() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #resultSetCN1 (data INT)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #resultSetCN1 (data) VALUES (?)");
pstmt.setInt(1, 1);
assertEquals(1, pstmt.executeUpdate());
pstmt.close();
Statement stmt2 = con.createStatement();
stmt2.executeQuery("SELECT data as test FROM #resultSetCN1");
ResultSet rs = stmt2.getResultSet();
assertNotNull(rs);
assertTrue(rs.next());
assertEquals(1, rs.getInt("test"));
assertFalse(rs.next());
stmt2.close();
rs.close();
}
/**
* Test for fixed bugs in ResultSetMetaData:
* <ol>
* <li>isNullable() always returns columnNoNulls.
* <li>isSigned returns true in error for TINYINT columns.
* <li>Type names for numeric / decimal have (prec,scale) appended in error.
* <li>Type names for auto increment columns do not have "identity" appended.
* </ol>
* NB: This test assumes getColumnName has been fixed to work as per the suggestion
* in bug report [1009233].
*
* @throws Exception
*/
public void testResultSetMetaData() throws Exception {
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.execute("CREATE TABLE #TRSMD (id INT IDENTITY NOT NULL, byte TINYINT NOT NULL, num DECIMAL(28,10) NULL)");
ResultSetMetaData rsmd = stmt.executeQuery("SELECT id as idx, byte, num FROM #TRSMD").getMetaData();
assertNotNull(rsmd);
// Check id
assertEquals("idx", rsmd.getColumnName(1)); // no longer returns base name
assertEquals("idx", rsmd.getColumnLabel(1));
assertTrue(rsmd.isAutoIncrement(1));
assertTrue(rsmd.isSigned(1));
assertEquals(ResultSetMetaData.columnNoNulls, rsmd.isNullable(1));
assertEquals("int identity", rsmd.getColumnTypeName(1));
assertEquals(Types.INTEGER, rsmd.getColumnType(1));
// Check byte
assertFalse(rsmd.isAutoIncrement(2));
assertFalse(rsmd.isSigned(2));
assertEquals(ResultSetMetaData.columnNoNulls, rsmd.isNullable(2));
assertEquals("tinyint", rsmd.getColumnTypeName(2));
assertEquals(Types.TINYINT, rsmd.getColumnType(2));
// Check num
assertFalse(rsmd.isAutoIncrement(3));
assertTrue(rsmd.isSigned(3));
assertEquals(ResultSetMetaData.columnNullable, rsmd.isNullable(3));
assertEquals("decimal", rsmd.getColumnTypeName(3));
assertEquals(Types.DECIMAL, rsmd.getColumnType(3));
stmt.close();
}
/**
* Test for bug [1022445] Cursor downgrade warning not raised.
*/
public void testCursorWarning() throws Exception
{
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #TESTCW (id INT PRIMARY KEY, DATA VARCHAR(255))");
stmt.execute("CREATE PROC #SPTESTCW @P0 INT OUTPUT AS SELECT * FROM #TESTCW");
stmt.close();
CallableStatement cstmt = con.prepareCall("{call #SPTESTCW(?)}",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
cstmt.registerOutParameter(1, Types.INTEGER);
ResultSet rs = cstmt.executeQuery();
// This should generate a ResultSet type/concurrency downgraded error.
assertNotNull(rs.getWarnings());
cstmt.close();
}
/**
* Test that the cursor fallback logic correctly discriminates between
* "real" sql errors and cursor open failures.
* <p/>
* This illustrates the logic added to fix:
* <ol>
* <li>[1323363] Deadlock Exception not reported (SQL Server)</li>
* <li>[1283472] Unable to cancel statement with cursor resultset</li>
* </ol>
*/
public void testCursorFallback() throws Exception {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//
// This test should fail on the cursor open but fall back to normal
// execution returning two result sets
//
stmt.execute("CREATE PROC #testcursor as SELECT 'data' select 'data2'");
stmt.execute("exec #testcursor");
assertNotNull(stmt.getWarnings());
ResultSet rs = stmt.getResultSet();
assertNotNull(rs); // First result set OK
assertTrue(stmt.getMoreResults());
rs = stmt.getResultSet();
assertNotNull(rs); // Second result set OK
//
// This test should fail on the cursor open (because of the for browse)
// but fall back to normal execution returning a single result set
//
rs = stmt.executeQuery("SELECT description FROM master..sysmessages FOR BROWSE");
assertNotNull(rs);
assertNotNull(rs.getWarnings());
rs.close();
//
// Enable logging to see that this test should just fail without
// attempting to fall back on normal execution.
//
// DriverManager.setLogStream(System.out);
try {
stmt.executeQuery("select bad from syntax");
fail("Expected SQLException");
} catch (SQLException e) {
assertEquals("S0002", e.getSQLState());
}
// DriverManager.setLogStream(null);
stmt.close();
}
/**
* Test for bug [1246270] Closing a statement after canceling it throws an
* exception.
*/
public void testCancelResultSet() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #TEST (id int primary key, data varchar(255))");
for (int i = 1; i < 1000; i++) {
stmt.executeUpdate("INSERT INTO #TEST VALUES (" + i +
", 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')");
}
ResultSet rs = stmt.executeQuery("SELECT * FROM #TEST");
assertNotNull(rs);
assertTrue(rs.next());
stmt.cancel();
stmt.close();
}
/**
* Test whether retrieval by name returns the first occurence (that's what
* the spec requires).
*/
public void testGetByName() throws Exception
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1 myCol, 2 myCol, 3 myCol");
assertTrue(rs.next());
assertEquals(1, rs.getInt("myCol"));
assertFalse(rs.next());
stmt.close();
}
/**
* Test if COL_INFO packets are processed correctly for
* <code>ResultSet</code>s with over 255 columns.
*/
public void testMoreThan255Columns() throws Exception
{
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
// create the table
int cols = 260;
StringBuffer create = new StringBuffer("create table #manycolumns (");
for (int i=0; i<cols; ++i) {
create.append("col" + i + " char(10), ") ;
}
create.append(")");
stmt.executeUpdate(create.toString());
String query = "select * from #manycolumns";
ResultSet rs = stmt.executeQuery(query);
rs.close();
stmt.close();
}
/**
* Test that <code>insertRow()</code> works with no values set.
*/
public void testEmptyInsertRow() throws Exception
{
int rows = 10;
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(
"create table #emptyInsertRow (id int identity, val int default 10)");
ResultSet rs = stmt.executeQuery("select * from #emptyInsertRow");
for (int i=0; i<rows; i++) {
rs.moveToInsertRow();
rs.insertRow();
}
rs.close();
rs = stmt.executeQuery("select count(*) from #emptyInsertRow");
assertTrue(rs.next());
assertEquals(rows, rs.getInt(1));
rs.close();
rs = stmt.executeQuery("select * from #emptyInsertRow order by id");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(10, rs.getInt(2));
rs.close();
stmt.close();
}
/**
* Test that inserted rows are visible in a scroll sensitive
* <code>ResultSet</code> and that they show up at the end.
*/
public void testInsertRowVisible() throws Exception
{
int rows = 10;
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?