📄 benchtest.java
字号:
System.out.println();
System.out.println( "Test insert rows with insertRow(): " + rowCount + " rows");
try{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
rs.moveToInsertRow();
rs.updateBytes ( "bi", byteArray );
rs.updateString( "c" , "Test" );
rs.updateDate ( "d" , new Date( System.currentTimeMillis() ) );
rs.updateFloat ( "de", (float)1234.56789 );
rs.updateFloat ( "f" , (float)9876.54321 );
rs.updateBytes ( "im", largeByteArray );
rs.updateInt ( "i" , i );
rs.updateDouble( "m" , 23.45 );
rs.updateDouble( "n" , 567.45 );
rs.updateFloat ( "r" , (float)78.89 );
rs.updateTime ( "sd", new Time( System.currentTimeMillis() ) );
rs.updateShort ( "si", (short)i );
rs.updateFloat ( "sm", (float)34.56 );
rs.updateString( "sy", "sysname (30) NULL" );
rs.updateString( "t" , "ntext NULL, sample to save in the field" );
rs.updateByte ( "ti", (byte)i );
rs.updateBytes ( "vb", byteArray );
rs.updateString( "vc", "nvarchar (255) NULL" );
rs.insertRow();
}
time += System.currentTimeMillis();
rs = st.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
int count = rs.getInt(1);
if (count != rowCount){
st.execute("DELETE FROM " + tableName);
System.out.println( " Failed: Only " + count + " rows were inserted.");
}else System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
e.printStackTrace();
try{
// reset for the next test
Statement st = con.createStatement();
st.execute("DELETE FROM " + tableName);
st.close();
}catch(Exception ee){/* ignore it */}
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 6. Test
* Request one page of rows from a large ResultSet.
*/
static void test_RowRequestPages(Connection con){
int pages = 100;
int rows = rowCount / pages;
System.out.println();
System.out.println( "Test request row pages : " + pages + " pages, " +rows + " rows per page");
try{
Statement st1 = con.createStatement();
ResultSet rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
int count = rs.getInt(1);
if (count != rowCount){
// There are not the correct count of rows.
if (count == 0){
createTestDataWithClassicInsert( con );
rs = st1.executeQuery( "SELECT count(*) FROM " + tableName);
rs.next();
count = rs.getInt(1);
}
if (count != rowCount){
System.out.println( " Failed: Only " + (rowCount-count) + " rows were found.");
return;
}
}
st1.close();
long time = -System.currentTimeMillis();
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize( rows );
for (int i=0; i<pages; i++){
rs = st.executeQuery("SELECT * FROM " + tableName);
rs.absolute( i*rows+1 );
for (int r=1; r<rows; r++){
// only (rows-1) rows because absolute has already the first row
if (!rs.next()){
System.out.println( " Failed: No rows were found at page " + i + " page and row " + r);
return;
}
int col_i = rs.getInt("i");
if (col_i != (i*rows+r)){
System.out.println( " Failed: Wrong row " + col_i + ", it should be row " + (i*rows+r));
return;
}
}
}
time += System.currentTimeMillis();
System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 7. Test
* Update rows with the method updateRow().
*/
static void test_UpdateRows(Connection con){
System.out.println();
System.out.println( "Test update rows with updateRow(): " + rowCount + " rows");
try{
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
int colCount = rs.getMetaData().getColumnCount();
long time = -System.currentTimeMillis();
int count = 0;
while(rs.next()){
for (int i=2; i<=colCount; i++){
rs.updateObject( i, rs.getObject(i) );
}
rs.updateRow();
count++;
}
time += System.currentTimeMillis();
if (count != rowCount)
System.out.println( " Failed: Only " + count + " rows were updated.");
else System.out.println( " Test time: " + time + " ms");
st.close();
}catch(Exception e){
System.out.println(" Failed:" + e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 8. Test
* Update rows with a PreparedStatement.
*/
static void test_UpdateRowsPrepare(Connection con){
System.out.println();
System.out.println( "Test update rows with a PreparedStatement: " + rowCount + " rows");
try{
PreparedStatement pr = con.prepareStatement( "UPDATE " + tableName + " SET bi=?,c=?,d=?,de=?,f=?,im=?,i=?,m=?,n=?,r=?,sd=?,si=?,sm=?,sy=?,t=?,ti=?,vb=?,vc=? WHERE i=?" );
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
pr.setBytes ( 1, byteArray );
pr.setString( 2 , "Test" );
pr.setDate ( 3 , new Date( System.currentTimeMillis() ) );
pr.setFloat ( 4, (float)1234.56789 );
pr.setFloat ( 5 , (float)9876.54321 );
pr.setBytes ( 6, largeByteArray );
pr.setInt ( 7 , i );
pr.setDouble( 8 , 23.45 );
pr.setDouble( 9 , 567.45 );
pr.setFloat ( 10 , (float)78.89 );
pr.setTime ( 11, new Time( System.currentTimeMillis() ) );
pr.setShort ( 12, (short)23456 );
pr.setFloat ( 13, (float)34.56 );
pr.setString( 14, "sysname (30) NULL" );
pr.setString( 15 , "text NULL" );
pr.setByte ( 16, (byte)28 );
pr.setBytes ( 17, byteArray );
pr.setString( 18, "varchar (255) NULL" );
pr.setInt ( 19 , i );
int updateCount = pr.executeUpdate();
if (updateCount != 1){
System.out.println( " Failed: Update count should be 1 but it is " + updateCount + ".");
return;
}
}
time += System.currentTimeMillis();
System.out.println( " Test time: " + time + " ms");
pr.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
}
}
/**
* 9. Test
* Update rows with a PreparedStatement and a stored procedure.
*/
static void test_UpdateRowsPrepareSP(Connection con){
System.out.println();
System.out.println( "Test update rows with a PreparedStatement and a stored procedure: " + rowCount + " rows");
try{
Statement st = con.createStatement();
try{st.execute("drop procedure sp_"+tableName);}catch(Exception e){/* ignore it */}
st.execute("create procedure sp_"+tableName+" (@bi binary,@c nchar(255),@d datetime,@de decimal,@f float,@im image,@i int,@m money,@n numeric(18, 0),@r real,@sd smalldatetime,@si smallint,@sm smallmoney,@sy sysname,@t ntext,@ti tinyint,@vb varbinary(255),@vc nvarchar(255)) as UPDATE " + tableName + " SET bi=@bi,c=@c,d=@d,de=@de,f=@f,im=@im,i=@i,m=@m,n=@n,r=@r,sd=@sd,si=@si,sm=@sm,sy=@sy,t=@t,ti=@ti,vb=@vb,vc=@vc WHERE i=@i");
PreparedStatement pr = con.prepareStatement( "exec sp_" + tableName + " ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" );
long time = -System.currentTimeMillis();
for (int i=0; i<rowCount; i++){
pr.setBytes ( 1, byteArray );
pr.setString( 2 , "Test" );
pr.setDate ( 3 , new Date( System.currentTimeMillis() ) );
pr.setFloat ( 4, (float)1234.56789 );
pr.setFloat ( 5 , (float)9876.54321 );
pr.setBytes ( 6, largeByteArray );
pr.setInt ( 7 , i );
pr.setDouble( 8 , 23.45 );
pr.setDouble( 9 , 567.45 );
pr.setFloat ( 10 , (float)78.89 );
pr.setTime ( 11, new Time( System.currentTimeMillis() ) );
pr.setShort ( 12, (short)23456 );
pr.setFloat ( 13, (float)34.56 );
pr.setString( 14, "sysname (30) NULL" );
pr.setString( 15 , "text NULL" );
pr.setByte ( 16, (byte)28 );
pr.setBytes ( 17, byteArray );
pr.setString( 18, "varchar (255) NULL" );
int updateCount = pr.executeUpdate();
if (updateCount != 1){
System.out.println( " Failed: Update count should be 1 but it is " + updateCount + ".");
return;
}
}
time += System.currentTimeMillis();
System.out.println( " Test time: " + time + " ms");
st.execute("drop procedure sp_"+tableName);
st.close();
pr.close();
}catch(Exception e){
System.out.println(" Failed:"+e);
}finally{
System.out.println();
System.out.println("===================================================================");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -