📄 timestamptest.java
字号:
new Double(1.7E-307) // jikes 1.04 has a bug and can't handle 1.7E-308
};
Statement stmt = con.createStatement();
stmt.executeUpdate(""
+ "create table #t0023 "
+ " (pk float not null, "
+ " type char(30) not null, "
+ " b bit, "
+ " str char(30) not null, "
+ " t int identity(1,1), "
+ " primary key (pk, type)) ");
PreparedStatement pstmt = con.prepareStatement(
"insert into #t0023 (pk, type, b, str) values(?, 'prepared', 0, ?)");
for (int i = 0; i < d.length; i++) {
pstmt.setDouble(1, d[i].doubleValue());
pstmt.setString(2, (d[i]).toString());
int preparedCount = pstmt.executeUpdate();
assertEquals(preparedCount, 1);
int adhocCount = stmt.executeUpdate(""
+ "insert into #t0023 "
+ " (pk, type, b, str) "
+ " values("
+ " " + d[i] + ", "
+ " 'adhoc', "
+ " 1, "
+ " '" + d[i] + "') ");
assertEquals(adhocCount, 1);
}
int count = 0;
ResultSet rs = stmt.executeQuery("select * from #t0023 where type='prepared' order by t");
assertNotNull(rs);
while (rs.next()) {
assertEquals(d[count].toString(), "" + rs.getDouble("pk"));
count++;
}
assertEquals(count, d.length);
count = 0;
rs = stmt.executeQuery("select * from #t0023 where type='adhoc' order by t");
while (rs.next()) {
assertEquals(d[count].toString(), "" + rs.getDouble("pk"));
count++;
}
assertEquals(count, d.length);
stmt.close();
pstmt.close();
}
public void testPrimaryKeyReal0024() throws Exception {
Float d[] = {
new Float(-1.0),
new Float(1234.543),
new Float(0.0),
new Float(1),
new Float(-2.0),
new Float(0.14),
new Float(0.79),
new Float(1000000.12345),
new Float(-1000000.12345),
new Float(1000000),
new Float(-1000000),
new Float(3.4E+38),
new Float(3.4E-38)
};
Statement stmt = con.createStatement();
stmt.executeUpdate(""
+ "create table #t0024 "
+ " (pk real not null, "
+ " type char(30) not null, "
+ " b bit, "
+ " str char(30) not null, "
+ " t int identity(1,1), "
+ " primary key (pk, type)) ");
PreparedStatement pstmt = con.prepareStatement(
"insert into #t0024 (pk, type, b, str) values(?, 'prepared', 0, ?)");
for (int i=0; i < d.length; i++) {
pstmt.setFloat(1, d[i].floatValue());
pstmt.setString(2, (d[i]).toString());
int preparedCount = pstmt.executeUpdate();
assertTrue(preparedCount == 1);
int adhocCount = stmt.executeUpdate(""
+ "insert into #t0024 "
+ " (pk, type, b, str) "
+ " values("
+ " " + d[i] + ", "
+ " 'adhoc', "
+ " 1, "
+ " '" + d[i] + "') ");
assertEquals(adhocCount, 1);
}
int count = 0;
ResultSet rs = stmt.executeQuery("select * from #t0024 where type='prepared' order by t");
assertNotNull(rs);
while (rs.next()) {
String s1 = d[count].toString().trim();
String s2 = ("" + rs.getFloat("pk")).trim();
assertTrue(s1.equalsIgnoreCase(s2));
count++;
}
assertEquals(count, d.length);
count = 0;
rs = stmt.executeQuery("select * from #t0024 where type='adhoc' order by t");
while (rs.next()) {
String s1 = d[count].toString().trim();
String s2 = ("" + rs.getFloat("pk")).trim();
assertTrue(s1.equalsIgnoreCase(s2));
count++;
}
assertEquals(count, d.length);
stmt.close();
pstmt.close();
}
public void testGetBoolean0025() throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("create table #t0025 " +
" (i integer, " +
" b bit, " +
" s char(5), " +
" f float) ");
// @todo Check which CHAR/VARCHAR values should be true and which should be false.
assertTrue(stmt.executeUpdate("insert into #t0025 values(0, 0, 'false', 0.0)") == 1);
assertTrue(stmt.executeUpdate("insert into #t0025 values(0, 0, '0', 0.0)") == 1);
assertTrue(stmt.executeUpdate("insert into #t0025 values(1, 1, 'true', 7.0)") == 1);
assertTrue(stmt.executeUpdate("insert into #t0025 values(2, 1, '1', -5.0)") == 1);
ResultSet rs = stmt.executeQuery("select * from #t0025 order by i");
assertNotNull(rs);
assertTrue("Expected a result set", rs.next());
assertTrue(!rs.getBoolean("i"));
assertTrue(!rs.getBoolean("b"));
assertTrue(!rs.getBoolean("s"));
assertTrue(!rs.getBoolean("f"));
assertTrue("Expected a result set", rs.next());
assertTrue(!rs.getBoolean("i"));
assertTrue(!rs.getBoolean("b"));
assertTrue(!rs.getBoolean("s"));
assertTrue(!rs.getBoolean("f"));
assertTrue("Expected a result set", rs.next());
assertTrue(rs.getBoolean("i"));
assertTrue(rs.getBoolean("b"));
assertTrue(rs.getBoolean("s"));
assertTrue(rs.getBoolean("f"));
assertTrue("Expected a result set", rs.next());
assertTrue(rs.getBoolean("i"));
assertTrue(rs.getBoolean("b"));
assertTrue(rs.getBoolean("s"));
assertTrue(rs.getBoolean("f"));
assertTrue("Expected no result set", !rs.next());
stmt.close();
}
/**
* <b>SAfe</b> Tests whether cursor-based statements still work ok when
* nested. Similar to <code>testNestedStatements0022</code>, which tests
* the same with plain (non-cursor-based) statements (and unfortunately
* fails).
*
* @throws Exception if an Exception occurs (very relevant, huh?)
*/
public void testNestedStatements0026() throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("create table #t0026a "
+ " (i integer not null, "
+ " str char(254) not null) ");
stmt.executeUpdate("create table #t0026b "
+ " (i integer not null, "
+ " t datetime not null) ");
stmt.close();
PreparedStatement pstmtA = con.prepareStatement(
"insert into #t0026a values (?, ?)");
PreparedStatement pstmtB = con.prepareStatement(
"insert into #t0026b values (?, getdate())");
final int rowsToAdd = 100;
int count = 0;
for (int i = 1; i <= rowsToAdd; i++) {
pstmtA.setInt(1, i);
StringBuffer tmp = new StringBuffer(255);
while (tmp.length() < 240) {
tmp.append("row ").append(i).append(". ");
}
pstmtA.setString(2, tmp.toString());
count += pstmtA.executeUpdate();
pstmtB.setInt(1, i);
pstmtB.executeUpdate();
}
assertEquals(count, rowsToAdd);
pstmtA.close();
pstmtB.close();
Statement stmtA = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Statement stmtB = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
count = 0;
ResultSet rsA = stmtA.executeQuery("select * from #t0026a");
assertNotNull(rsA);
while (rsA.next()) {
count++;
ResultSet rsB = stmtB.executeQuery(
"select * from #t0026b where i=" + rsA.getInt("i"));
assertNotNull(rsB);
assertTrue("Expected a result set", rsB.next());
assertTrue("Expected no result set", !rsB.next());
rsB.close();
}
assertEquals(count, rowsToAdd);
stmtA.close();
stmtB.close();
}
public void testErrors0036() throws Exception {
Statement stmt = con.createStatement();
final int numberToTest = 5;
for (int i = 0; i < numberToTest; i++) {
String table = "#t0036_no_create_" + i;
try {
stmt.executeUpdate("drop table " + table);
fail("Did not expect to reach here");
} catch (SQLException e) {
assertEquals("42S02", e.getSQLState());
}
}
stmt.close();
}
public void testTimestamps0037() throws Exception {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"select " +
" convert(smalldatetime, '1999-01-02') a, " +
" convert(smalldatetime, null) b, " +
" convert(datetime, '1999-01-02') c, " +
" convert(datetime, null) d ");
assertNotNull(rs);
assertTrue("Expected a result", rs.next());
assertNotNull(rs.getDate("a"));
assertNull(rs.getDate("b"));
assertNotNull(rs.getDate("c"));
assertNull(rs.getDate("d"));
assertNotNull(rs.getTime("a"));
assertNull(rs.getTime("b"));
assertNotNull(rs.getTime("c"));
assertNull(rs.getTime("d"));
assertNotNull(rs.getTimestamp("a"));
assertNull(rs.getTimestamp("b"));
assertNotNull(rs.getTimestamp("c"));
assertNull(rs.getTimestamp("d"));
assertTrue("Expected no more results", !rs.next());
stmt.close();
}
public void testConnection0038() throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("create table #t0038 ("
+ " keyField char(255) not null, "
+ " descField varchar(255) not null) ");
int count = stmt.executeUpdate("insert into #t0038 values ('value', 'test')");
assertEquals(count, 1);
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("update #t0038 set descField=descField where keyField=?");
ps.setString(1, "value");
ps.executeUpdate();
ps.close();
con.commit();
// conn.rollback();
ResultSet resultSet = stmt.executeQuery(
"select descField from #t0038 where keyField='value'");
assertTrue(resultSet.next());
stmt.close();
}
public void testConnection0039() throws Exception {
for (int i = 0; i < 10; i++) {
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select 5");
assertNotNull(resultSet);
resultSet.close();
statement.close();
conn.close();
}
}
public void testPreparedStatement0040() throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("create table #t0040 ("
+ " c255 char(255) not null, "
+ " v255 varchar(255) not null) ");
PreparedStatement pstmt = con.prepareStatement("insert into #t0040 values (?, ?)");
String along = getLongString('a');
String blong = getLongString('b');
pstmt.setString(1, along);
pstmt.setString(2, along);
int count = pstmt.executeUpdate();
assertEquals(count, 1);
pstmt.close();
count = stmt.executeUpdate(""
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -