提示:本文包含在Java中通过JDBC Driver对Caché/IRIS数据库进行查询的示例代码。
近期有客户反应使用Java从老版本Caché中读取数据时,如果数据中包含long varchar, Caché数据库中与之对应的属性类型为%Stream.GlobalCharacter,即使实际上该流数据长度非常小,也会成十几倍的降低性能。
大家先来看一段代码,
public static void test99()
{
Statement stmt = null;
ResultSet rs = null;
int fetchSize = 100000;
long before = System.currentTimeMillis();
String sql="Select Title, Notes from My.Employee Where id=1";
try {
CacheDataSource ds = new CacheDataSource();
ds.setURL("jdbc:Cache://123.123.123.1:1972/Samples"); //访问Caché数据库
ds.setUser("_SYSTEM");
ds.setPassword("SYS");
Connection connection = ds.getConnection();
connection.setAutoCommit(false);
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
long executed = System.currentTimeMillis();
System.out.println("execute take miliseconds of:"+(executed-before));
ResultSetMetaData rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
String str = null;
while (rs.next()) {
for (int i = 1,ilen = colnum; i <= ilen; i++) {
str = rs.getString(i);
}
}
stmt.close();
rs.close();
connection.close();
long end = System.currentTimeMillis();
System.out.println("read take miliseconds of:"+(end - executed));
System.out.print("total take miliseconds of:"+(end-before));
}catch (Exception ex) {
System.out.println("TinyJDBC caught exception: "
+ ex.getClass().getName() + ": " + ex.getMessage());
}
}