📄 testresultset.java
字号:
check(rs.getRow() == 2);
check(rs.getInt(2) == 0 && !rs.wasNull());
check(!rs.getBoolean(2));
check(rs.getByte(2) == 0);
check(rs.getShort(2) == 0);
check(rs.getLong(2) == 0);
check(rs.getFloat(2) == 0.0);
check(rs.getDouble(2) == 0.0);
check(rs.getString(2).equals("0") && !rs.wasNull());
check(rs.getInt(1) == 2 && !rs.wasNull());
rs.next();
check(rs.getRow() == 3);
check(rs.getInt("ID") == 3 && !rs.wasNull());
check(rs.getInt("VALUE") == 1 && !rs.wasNull());
rs.next();
check(rs.getRow() == 4);
check(rs.getInt("ID") == 4 && !rs.wasNull());
check(rs.getInt("VALUE") == Integer.MAX_VALUE && !rs.wasNull());
rs.next();
check(rs.getRow() == 5);
check(rs.getInt("id") == 5 && !rs.wasNull());
check(rs.getInt("value") == Integer.MIN_VALUE && !rs.wasNull());
check(rs.getString(1).equals("5") && !rs.wasNull());
rs.next();
check(rs.getRow() == 6);
check(rs.getInt("id") == 6 && !rs.wasNull());
check(rs.getInt("value") == 0 && rs.wasNull());
check(rs.getInt(2) == 0 && rs.wasNull());
check(rs.getInt(1) == 6 && !rs.wasNull());
check(rs.getString(1).equals("6") && !rs.wasNull());
check(rs.getString(2) == null && rs.wasNull());
o = rs.getObject(2);
check(o == null);
check(rs.wasNull());
checkFalse(rs.next());
check(rs.getRow(), 0);
// there is one more row, but because of setMaxRows we don't get it
stat.execute("DROP TABLE TEST");
stat.setMaxRows(0);
}
void testVarchar() throws Exception {
trace("Test VARCHAR");
ResultSet rs;
Object o;
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE VARCHAR(255))");
stat.execute("INSERT INTO TEST VALUES(1,'')");
stat.execute("INSERT INTO TEST VALUES(2,' ')");
stat.execute("INSERT INTO TEST VALUES(3,' ')");
stat.execute("INSERT INTO TEST VALUES(4,NULL)");
stat.execute("INSERT INTO TEST VALUES(5,'Hi')");
stat.execute("INSERT INTO TEST VALUES(6,' Hi ')");
stat.execute("INSERT INTO TEST VALUES(7,'Joe''s')");
stat.execute("INSERT INTO TEST VALUES(8,'{escape}')");
stat.execute("INSERT INTO TEST VALUES(9,'\\n')");
stat.execute("INSERT INTO TEST VALUES(10,'\\''')");
stat.execute("INSERT INTO TEST VALUES(11,'\\%')");
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
testResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.VARCHAR }, new int[] {
10, 255 }, new int[] { 0, 0 });
String value;
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <>)");
check(value != null && value.equals("") && !rs.wasNull());
check(rs.getInt(1) == 1 && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: < >)");
check(rs.getString(2).equals(" ") && !rs.wasNull());
check(rs.getInt(1) == 2 && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: < >)");
check(rs.getString(2).equals(" ") && !rs.wasNull());
check(rs.getInt(1) == 3 && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <null>)");
check(rs.getString(2) == null && rs.wasNull());
check(rs.getInt(1) == 4 && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <Hi>)");
check(rs.getInt(1) == 5 && !rs.wasNull());
check(rs.getString(2).equals("Hi") && !rs.wasNull());
o = rs.getObject("value");
trace(o.getClass().getName());
check(o instanceof String);
check(o.toString().equals("Hi"));
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: < Hi >)");
check(rs.getInt(1) == 6 && !rs.wasNull());
check(rs.getString(2).equals(" Hi ") && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <Joe's>)");
check(rs.getInt(1) == 7 && !rs.wasNull());
check(rs.getString(2).equals("Joe's") && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <{escape}>)");
check(rs.getInt(1) == 8 && !rs.wasNull());
check(rs.getString(2).equals("{escape}") && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <\\n>)");
check(rs.getInt(1) == 9 && !rs.wasNull());
check(rs.getString(2).equals("\\n") && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <\\'>)");
check(rs.getInt(1) == 10 && !rs.wasNull());
check(rs.getString(2).equals("\\'") && !rs.wasNull());
rs.next();
value = rs.getString(2);
trace("Value: <" + value + "> (should be: <\\%>)");
check(rs.getInt(1) == 11 && !rs.wasNull());
check(rs.getString(2).equals("\\%") && !rs.wasNull());
check(!rs.next());
stat.execute("DROP TABLE TEST");
}
void testDecimal() throws Exception {
trace("Test DECIMAL");
ResultSet rs;
Object o;
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE DECIMAL(10,2))");
stat.execute("INSERT INTO TEST VALUES(1,-1)");
stat.execute("INSERT INTO TEST VALUES(2,.0)");
stat.execute("INSERT INTO TEST VALUES(3,1.)");
stat.execute("INSERT INTO TEST VALUES(4,12345678.89)");
stat.execute("INSERT INTO TEST VALUES(6,99999998.99)");
stat.execute("INSERT INTO TEST VALUES(7,-99999998.99)");
stat.execute("INSERT INTO TEST VALUES(8,NULL)");
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
testResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.DECIMAL }, new int[] {
10, 10 }, new int[] { 0, 2 });
BigDecimal bd;
rs.next();
check(rs.getInt(1) == 1);
check(!rs.wasNull());
check(rs.getInt(2) == -1);
check(!rs.wasNull());
bd = rs.getBigDecimal(2);
check(bd.compareTo(new BigDecimal("-1.00")) == 0);
check(!rs.wasNull());
o = rs.getObject(2);
trace(o.getClass().getName());
check(o instanceof BigDecimal);
check(((BigDecimal) o).compareTo(new BigDecimal("-1.00")) == 0);
rs.next();
check(rs.getInt(1) == 2);
check(!rs.wasNull());
check(rs.getInt(2) == 0);
check(!rs.wasNull());
bd = rs.getBigDecimal(2);
check(bd.compareTo(new BigDecimal("0.00")) == 0);
check(!rs.wasNull());
rs.next();
checkColumnBigDecimal(rs, 2, 1, "1.00");
rs.next();
checkColumnBigDecimal(rs, 2, 12345679, "12345678.89");
rs.next();
checkColumnBigDecimal(rs, 2, 99999999, "99999998.99");
rs.next();
checkColumnBigDecimal(rs, 2, -99999999, "-99999998.99");
rs.next();
checkColumnBigDecimal(rs, 2, 0, null);
check(!rs.next());
stat.execute("DROP TABLE TEST");
}
void testDoubleFloat() throws Exception {
trace("Test DOUBLE - FLOAT");
ResultSet rs;
Object o;
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, D DOUBLE, R REAL)");
stat.execute("INSERT INTO TEST VALUES(1, -1, -1)");
stat.execute("INSERT INTO TEST VALUES(2,.0, .0)");
stat.execute("INSERT INTO TEST VALUES(3, 1., 1.)");
stat.execute("INSERT INTO TEST VALUES(4, 12345678.89, 12345678.89)");
stat.execute("INSERT INTO TEST VALUES(6, 99999999.99, 99999999.99)");
stat.execute("INSERT INTO TEST VALUES(7, -99999999.99, -99999999.99)");
stat.execute("INSERT INTO TEST VALUES(8, NULL, NULL)");
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
testResultSetMeta(rs, 3, new String[] { "ID", "D", "R" },
new int[] { Types.INTEGER, Types.DOUBLE, Types.REAL }, new int[] { 10, 17, 7 }, new int[] { 0, 0, 0 });
BigDecimal bd;
rs.next();
check(rs.getInt(1) == 1);
check(!rs.wasNull());
check(rs.getInt(2) == -1);
check(rs.getInt(3) == -1);
check(!rs.wasNull());
bd = rs.getBigDecimal(2);
check(bd.compareTo(new BigDecimal("-1.00")) == 0);
check(!rs.wasNull());
o = rs.getObject(2);
trace(o.getClass().getName());
check(o instanceof Double);
check(((Double) o).compareTo(new Double("-1.00")) == 0);
o = rs.getObject(3);
trace(o.getClass().getName());
check(o instanceof Float);
check(((Float) o).compareTo(new Float("-1.00")) == 0);
rs.next();
check(rs.getInt(1) == 2);
check(!rs.wasNull());
check(rs.getInt(2) == 0);
check(!rs.wasNull());
check(rs.getInt(3) == 0);
check(!rs.wasNull());
bd = rs.getBigDecimal(2);
check(bd.compareTo(new BigDecimal("0.00")) == 0);
check(!rs.wasNull());
bd = rs.getBigDecimal(3);
check(bd.compareTo(new BigDecimal("0.00")) == 0);
check(!rs.wasNull());
rs.next();
check(rs.getDouble(2), 1.0);
check(rs.getFloat(3), 1.0f);
rs.next();
check(rs.getDouble(2), 12345678.89);
check(rs.getFloat(3), 12345678.89f);
rs.next();
check(rs.getDouble(2), 99999999.99);
check(rs.getFloat(3), 99999999.99f);
rs.next();
check(rs.getDouble(2), -99999999.99);
check(rs.getFloat(3), -99999999.99f);
rs.next();
checkColumnBigDecimal(rs, 2, 0, null);
checkColumnBigDecimal(rs, 3, 0, null);
check(!rs.next());
stat.execute("DROP TABLE TEST");
}
void testDatetime() throws Exception {
trace("Test DATETIME");
ResultSet rs;
Object o;
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE DATETIME)");
stat.execute("INSERT INTO TEST VALUES(1,DATE '2011-11-11')");
stat.execute("INSERT INTO TEST VALUES(2,TIMESTAMP '2002-02-02 02:02:02')");
stat.execute("INSERT INTO TEST VALUES(3,TIMESTAMP '1800-1-1 0:0:0')");
stat.execute("INSERT INTO TEST VALUES(4,TIMESTAMP '9999-12-31 23:59:59')");
stat.execute("INSERT INTO TEST VALUES(5,NULL)");
rs = stat.executeQuery("SELECT 0 ID, TIMESTAMP '9999-12-31 23:59:59' VALUE FROM TEST ORDER BY ID");
testResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.TIMESTAMP },
new int[] { 10, 23 }, new int[] { 0, 10 });
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
testResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.TIMESTAMP },
new int[] { 10, 23 }, new int[] { 0, 10 });
rs.next();
java.sql.Date date;
java.sql.Time time;
java.sql.Timestamp ts;
date = rs.getDate(2);
check(!rs.wasNull());
time = rs.getTime(2);
check(!rs.wasNull());
ts = rs.getTimestamp(2);
check(!rs.wasNull());
trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
trace("Date ms: " + date.getTime() + " Time ms:" + time.getTime() + " Timestamp ms:" + ts.getTime());
trace("1970 ms: " + java.sql.Timestamp.valueOf("1970-01-01 00:00:00.0").getTime());
check(date.getTime(), java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime());
check(time.getTime(), java.sql.Timestamp.valueOf("1970-01-01 00:00:00.0").getTime());
check(ts.getTime(), java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime());
check(date.equals(java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0")));
check(time.equals(java.sql.Timestamp.valueOf("1970-01-01 00:00:00.0")));
check(ts.equals(java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0")));
checkFalse(rs.wasNull());
o = rs.getObject(2);
trace(o.getClass().getName());
check(o instanceof java.sql.Timestamp);
check(((java.sql.Timestamp) o).equals(java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0")));
checkFalse(rs.wasNull());
rs.next();
date = rs.getDate("VALUE");
check(!rs.wasNull());
time = rs.getTime("VALUE");
check(!rs.wasNull());
ts = rs.getTimestamp("VALUE");
check(!rs.wasNull());
trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
check(date.toString(), "2002-02-02");
check(time.toString(), "02:02:02");
check(ts.toString(), "2002-02-02 02:02:02.0");
rs.next();
check(rs.getDate("value").toString(), "1800-01-01");
check(rs.getTime("value").toString(), "00:00:00");
check(rs.getTimestamp("value").toString(), "1800-01-01 00:00:00.0");
rs.next();
check(rs.getDate("Value").toString(), "9999-12-31");
check(rs.getTime("Value").toString(), "23:59:59");
check(rs.getTimestamp("Value").toString(), "9999-12-31 23:59:59.0");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -