| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 964 人关注过本帖
标题:请教关于java对SQL Server 2005插入速度的问题,这程序还要修改吗?
只看楼主 加入收藏
林中剑影
Rank: 1
来 自:广州
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-11-3
收藏
 问题点数:0 回复次数:0 
请教关于java对SQL Server 2005插入速度的问题,这程序还要修改吗?
要测试出SQL Server 2005插入速度、更新速度,测出平均每秒的插入和更新速度,我这边测试出的数据是插入才1600多次每秒,更新只有200多次每秒。
请问一下要怎么才有提高速度?我这程序还要再优化吗?
程序代码:
package sqltest;
import java.sql.*;

public class SqlSpeed {
    private final String DBDRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String URL = "jdbc:sqlserver://192.168.168.104:1433;DatabaseName=ATest";
    private static Connection con = null;
    private static PreparedStatement stmt = null;
    private static ResultSet rs = null;
    
    
    public void connect(){
        try{    
        
            Class.forName(DBDRIVE);
            con = DriverManager.getConnection(URL,"sa","ba123");
            System.out.println("Connect success...");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Connect failed...");
        }
    }
    
    public void operate(String sql){
        //int exec = 0;
        try{
            stmt = con.prepareStatement(sql);
            //exec = stmt.executeUpdate(sql);
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
        
    public void resultSql(String sql){
        try{
            stmt = con.prepareStatement(sql);
            //rs = stmt.executeQuery(sql);
            
            ResultSetMetaData md = rs.getMetaData();
            System.out.println(md.getColumnName(1)+'\t'+md.getColumnName(2)+'\t'+"  "+md.getColumnName(3));
            while(rs.next()){
                System.out.println(rs.getString(1)+'\t'+" "+rs.getString(2)+rs.getString(3));
            }
        }catch(Exception e){
            e.getMessage();
        }    
    }
    
    public static void close(){
         if (rs != null)
             try {
                 rs.close();
             } catch(Exception e) {
                 e.printStackTrace();
             }
         if (stmt != null)
             try {
                 stmt.close();
             } catch(Exception e) {
                 e.printStackTrace();
             }
         if (con != null)
             try {
                 con.close();
             } catch(Exception e) {
                 e.printStackTrace();
             }
    }
    
    
    public static void main(String args[]) throws SQLException{
        String SQL = "SELECT * FROM tableb";
        String deleteSQL = "DELETE FROM tableb where name = 'eee'";
        String insertSQL = "INSERT INTO tableb(num,name,age,score) VALUES(?,'eee',20,82)";
        
    
        SqlSpeed ss = new SqlSpeed();
        
        long begin1 = System.currentTimeMillis();    
        ss.connect();             //连接
        long time1 = System.currentTimeMillis()-begin1;
        
    
        int loop = 10000;
        
        long begin2 =System.currentTimeMillis();
        for(int i=0; i<loop; i++){
              ss.operate(insertSQL);
            stmt.setInt(1, i);
            stmt.executeUpdate();
            //stmt.addBatch();   //批量处理
            //String updateSQL = "UPDATE tableb SET age =+"+i+" where num =1";
            //插入
            // ss.operate(updateSQL);    //更新
            //if(i/8000 == 0){
            //    int[] st = stmt.executeBatch();
            //}
        }
        
        long time2 = System.currentTimeMillis() - begin2;
        System.out.println("平均每秒插入:"+(loop*1000)/time2);
    
        
    
        long begin3 = System.currentTimeMillis();
        ss.operate(deleteSQL);    //删除
        stmt.executeUpdate();
        long time3 = System.currentTimeMillis() - begin3;
        
     /*
        long begin4 = System.currentTimeMillis();
        for(int i=1; ; i++){
            String updateSQL = "UPDATE message SET age =+"+i+" where num =1";
               ss.operate(updateSQL);    //更新
            if(System.currentTimeMillis() - begin4 == 1000){
                System.out.println("更新速率="+i+"次/秒");
                break;
            }
        }
    //    */
        
        long begin5 = System.currentTimeMillis();
        //ss.resultSql(SQL);          //处理结果
        long time5 = System.currentTimeMillis()-begin5;
        
        
        
        System.out.println("连接time1="+time1 +"ms. 插入time2="+time2+"ms. 删除time3="+
                            time3+"ms. 处理结果time5="+time5+"ms.");
        
        close();  //关闭所有连接
      }
    
}
搜索更多相关主题的帖子: SQL java Server 速度 private 
2008-08-19 13:52
快速回复:请教关于java对SQL Server 2005插入速度的问题,这程序还要修改吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073442 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved