📄 timestamptest.java
字号:
dropTable("#t0022a");
dropTable("#t0022b");
Statement stmt = cx.createStatement();
stmt.executeUpdate("create table #t0022a "
+ " (i integer not null, "
+ " str char(254) not null) ");
stmt.executeUpdate("create table #t0022b "
+ " (i integer not null, "
+ " t datetime not null) ");
PreparedStatement pStmtA = cx.prepareStatement(
"insert into #t0022a values (?, ?)");
PreparedStatement pStmtB = cx.prepareStatement(
"insert into #t0022b values (?, getdate())");
final int rowsToAdd = 1000;
int count = 0;
for (int i = 1; i <= rowsToAdd; i++)
{
pStmtA.setInt(1, i);
String tmp = "";
while (tmp.length() < 240)
{
tmp = tmp + "row " + i + ". ";
}
pStmtA.setString(2, tmp);
count += pStmtA.executeUpdate();
pStmtB.setInt(1, i);
pStmtB.executeUpdate();
}
assertTrue(count == rowsToAdd);
Statement stmtA = cx.createStatement();
Statement stmtB = cx.createStatement();
count = 0;
ResultSet rsA = stmtA.executeQuery("select * from #t0022a");
assertNotNull(rsA);
while (rsA.next())
{
count++;
ResultSet rsB = stmtB.executeQuery(
"select * from #t0022b where i=" + rsA.getInt("i"));
assertNotNull(rsB);
assertTrue("Expected a result set", rsB.next());
assertTrue("Expected no result set", !rsB.next());
}
assertTrue(count == rowsToAdd);
}
public void testPrimaryKeyFloat0023() throws Exception
{
Double d[] =
{
new Double(-1.0),
new Double(1234.543),
new Double(0.0),
new Double(1),
new Double(-2.0),
new Double(0.14),
new Double(0.79),
new Double(1000000.12345),
new Double(-1000000.12345),
new Double(1000000),
new Double(-1000000),
new Double(1.7E+308),
new Double(1.7E-307) // jikes 1.04 has a bug and can't handle 1.7E-308
};
Connection cx = getConnection();
dropTable("#t0023");
Statement stmt = cx.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 = cx.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();
assertTrue(preparedCount == 1);
int adhocCount = stmt.executeUpdate(""
+ "insert into #t0023 "
+ " (pk, type, b, str) "
+ " values("
+ " " + d[i] + ", "
+ " 'adhoc', "
+ " 1, "
+ " '" + d[i] + "') ");
assertTrue(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++;
}
assertTrue(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++;
}
assertTrue(count == d.length);
}
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)
};
Connection cx = getConnection();
dropTable("#t0024");
Statement stmt = cx.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 = cx.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] + "') ");
assertTrue(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++;
}
assertTrue(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++;
}
assertTrue(count == d.length);
}
public void testGetBoolean0025() throws Exception
{
Connection cx = getConnection();
dropTable("#t0025");
Statement stmt = cx.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());
}
public void testErrors0036() throws Exception
{
Connection cx = getConnection();
Statement stmt = cx.createStatement();
final int numberToTest = 5;
for (int i = 0; i < numberToTest; i++)
{
String table = "#t0036_do_not_create_" + i;
try
{
stmt.executeUpdate("drop table " + table);
assertTrue("Did not expect to reach here", false);
}
catch (SQLException e)
{
assertTrue(e.getMessage().startsWith("Cannot drop the table '" + table+"', because it does"));
}
}
}
public void testTimestamps0037() throws Exception
{
Connection cx = getConnection();
Statement stmt = cx.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 set", rs.next());
assertTrue(rs.getDate("a") != null);
assertTrue(rs.getDate("b") == null);
assertTrue(rs.getDate("c") != null);
assertTrue(rs.getDate("d") == null);
assertTrue(rs.getTime("a") != null);
assertTrue(rs.getTime("b") == null);
assertTrue(rs.getTime("c") != null);
assertTrue(rs.getTime("d") == null);
assertTrue(rs.getTimestamp("a") != null);
assertTrue(rs.getTimestamp("b") == null);
assertTrue(rs.getTimestamp("c") != null);
assertTrue(rs.getTimestamp("d") == null);
assertTrue("Expected no result set", !rs.next());
}
public void testConnection0038() throws Exception
{
Connection conn = getConnection();
dropTable("#t0038");
Statement stmt = conn.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')");
assertTrue(count == 1);
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("update #t0038 set descField=descField where keyField=?");
ps.setString(1, "value");
ps.executeUpdate();
ps.close();
conn.commit();
// conn.rollback();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(
"select descField from #t0038 where keyField='value'");
assertTrue(resultSet.next());
statement.close();
conn.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
{
Connection cx = getConnection();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -