Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区 » Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:Hibernate的优点 [Re:lummyliao]
findjob



发贴: 0
积分: 0
于 2004-10-22 19:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这是我的测试
mysql4.0.18nt
mysql-connector-java-3.1.4-beta-bin.jar
window server 2003
代码:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Driver;

public class BatchUpdateTest {

private final static String CLEAR_DB =
"truncate sales_rep";
private final static String INSERT_INTO_DB =
"insert into sales_rep values(?,?,?)";

public final static void main(String[] args) {
if (args.length != 2) {
System.out.println("usage:please enter two args");
return;
}
String type = args[0];
int num = 0;
try {
num = Integer.parseInt(args[1], 10);
} catch (NumberFormatException nume) {
throw new IllegalArgumentException("the second arg must be a number");
}
if (!("update".equals(args[0]) || "updateBatch".equals(args[0]) || "updateStmt".equals(args[0]))) {
throw new IllegalArgumentException("the first arg must be 'update' or 'updateBatch' or 'updateStmt'");
}
//DriverManager.setLogStream(System.out);
Connection conn = null;
try {
new Driver();
Properties prop = new Properties();
prop.setProperty("user", "root");
prop.setProperty("useUnicode", "true");
prop.setProperty("characterEncoding", "GB2312");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", prop);
if (args[0].equals("update"))
update(conn, num);
else if (args[0].equals("updateBatch")) {
updateBatch(conn, num);
} else {
updateStmt(conn, num);
}

} catch(SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (conn != null) conn.close();
} catch (SQLException sqle) {
System.err.println(sqle.toString());
}
}
}

private static void update(Connection conn, int num) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(CLEAR_DB);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement(INSERT_INTO_DB);
long startTime = System.currentTimeMillis();
for (int i = 0;i<num;i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "test");
pstmt.setString(3, "testData");
pstmt.executeUpdate();
}
pstmt.close();
System.out.println(System.currentTimeMillis() - startTime);
}

private static void updateBatch(Connection conn, int num) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(CLEAR_DB);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement(INSERT_INTO_DB);
long startTime = System.currentTimeMillis();
for (int i = 0;i<num;i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "test");
pstmt.setString(3, "testData");
pstmt.addBatch();
}
pstmt.executeBatch();
pstmt.close();
System.out.println(System.currentTimeMillis() - startTime);
}

private static void updateStmt(Connection conn, int num) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(CLEAR_DB);
pstmt.executeUpdate();
pstmt.close();
Statement stmt = conn.createStatement();
long startTime = System.currentTimeMillis();
StringBuffer buffer = new StringBuffer(num * 20);
buffer.append("insert into sales_rep values");
for (int i=0;i<num;i++) {
buffer.append('(');
buffer.append(i).append(',');
buffer.append("'test','testData'),");
}
buffer.deleteCharAt(buffer.length() - 1);
stmt.executeUpdate(buffer.toString());
stmt.close();
System.out.println(System.currentTimeMillis() - startTime);
}
}



findjob edited on 2004-10-22 19:37


话题树型展开
人气 标题 作者 字数 发贴时间
88022 [精华] Hibernate的优点 lummyliao 2017 2004-09-22 17:46
79833 Re:Hibernate的优点 didongusa 297 2004-09-23 08:44
79740 Re:Hibernate的优点 极品飞车 27 2004-09-27 14:06
79446 Re:Hibernate的优点 didongusa 103 2004-09-27 14:16
80027 Re:Hibernate的优点 huzhigang 446 2004-09-28 08:52
79739 Re:Hibernate的优点 holylz2004 8 2004-09-30 17:19
79281 Re:Hibernate的优点 dlint 142 2004-10-17 20:42
78886 Re:Hibernate的优点 jbwang 24 2004-10-18 22:58
79336 Re:Hibernate的优点 yclc 79 2004-10-20 11:55
78928 Re:Hibernate的优点 findjob 560 2004-10-21 20:24
79125 Re:Hibernate的优点 robbin 54 2004-10-22 17:14
78625 Re:Hibernate的优点 findjob 64 2004-10-22 17:51
80023 Re:Hibernate的优点 dengyin2000 62 2004-09-23 09:00
79145 Re:Hibernate的优点 findjob 3853 2004-10-22 19:19
78566 Re:Hibernate的优点 findjob 6 2004-10-22 19:31
78724 Re:Hibernate的优点 findjob 79 2004-10-22 19:46
78828 Re:Hibernate的优点 robbin 89 2004-11-18 23:05
78454 Re:Hibernate的优点 haibo 41 2004-11-19 10:59
78176 Re:Hibernate的优点 mocci 74 2004-11-23 17:24
77721 Re:Hibernate的优点 heoxsoft 100 2005-01-05 19:10
76433 Re:Hibernate的优点 wujiboy 38 2005-03-08 20:18
80205 Re:Hibernate的优点 sunsonbaby 46 2005-03-13 13:22
79565 Re:Hibernate的优点 didongusa 1130 2004-09-23 09:39
79504 Re:Hibernate的优点 didongusa 82 2004-09-24 05:44
79428 Re:Hibernate的优点 nothing 15 2004-09-24 06:53
79596 Re:Hibernate的优点 sankxuan 82 2004-09-24 09:33
79533 Re:Hibernate的优点 didongusa 122 2004-09-26 03:23
79916 Re:Hibernate的优点 gispda 418 2004-09-26 10:55
79561 Re:Hibernate的优点 didongusa 225 2004-09-26 12:31
79501 Re:Hibernate的优点 didongusa 133 2004-09-26 12:54
79944 Re:Hibernate的优点 gispda 44 2004-09-27 10:18

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923