package com.company;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.sql.PreparedStatement;
public class OracleTest{
/设置参数/
private static Connection conn = null;
private static PreparedStatement pst = null;
/构造方法,链接数据库/
public OracleTest() {
try{
System.out.println(“正在连接数据库…”);
Class.forName(“oracle.jdbc.driver.OracleDriver”);
String url = “jdbc:oracle:thin:@192.168.0.74:1521/orcl”;//url为连接字符串
String user = “wzbase”;//数据库用户名
String pwd = “wzbase”;//数据库密码
conn=DriverManager.getConnection(url,user,pwd);
System.out.println(“数据库连接成功!!!”);
}catch(Exception e){ //抛出异常处理
System.out.println(e.getMessage());
e.printStackTrace();
}
}
//声明一个公有访问获取系统时间的对象
public static String getStringDate() {
Date currentTime = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(currentTime);
// System.out.println(“TIME:::”+dateString);
return dateString;
}
/**
* 生成6位随机数,由年月+8位随机数
* 生成流水号
*
*/
//public static String Getnum(){
//
// String time = getStringDate();
// int rand = 1000000000;
// rand++;
// int randomNumber=(int)(Math.random()*900000000)+rand;
// String serial = time+randomNumber;
// return serial;
//}
public static void main(String[] args) throws SQLException {
long begin = new Date(System.currentTimeMillis()).getTime(); //开始时间
OracleTest a = new OracleTest();//实例化对象,作用是调用构造方法
a.getClass();//无意义
String insertsql="INSERT INTO SDGL.test (ZD1,ZD2,ZD3) ";
conn.setAutoCommit(false);
String ZD1 = "0";
String ZD2 = null;
String ZD3 = "sql";
String time = getStringDate();
// time = time+“4”;
int random=0;
// 外层循环,总提交事务次数
for (int i = 1; i <= 2; i++) {
StringBuffer suffix = new StringBuffer();
// 第j次提交步长
for (int j = 1; j <= 10; j++) {
random++;
ZD1 = time;
ZD2 = time;
int length=7-(String.valueOf(random)).length();
for(int n=0;n<length;n++){
ZD1=ZD1+“0”;
}
ZD1=ZD1+random;
// 构建SQL后缀
System.out.println(ZD1);
suffix.append("select '"+ZD1+"','"+ZD2+"','"+ZD3+"' "+"from dual union all "+"");
}
// 构建完整SQL
String sql = insertsql + suffix.substring(0, suffix.length()-11);
System.out.println(sql);
PreparedStatement pst = conn.prepareStatement(sql);
try {
pst.addBatch();
pst.executeBatch();
// 提交事务
conn.commit();
} catch (Exception e) {
conn.rollback();
}
}
long end = new Date(System.currentTimeMillis()).getTime();
System.out.println("castime : " + (end - begin) + " ms");
// 关闭链接
conn.close();
}
}