1.基于数据库批量造数据用于测试小demo
2.需要mysql-connector-java jar包,去官网下载https://dev.mysql.com/downloads/connector/j/
3.需要转换为sql server,oracle将驱动和连接地址更改即可
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 创建连接数据库
* */
public class util {
public static Connection getConnection() {
Connection conn = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch2.4", "root", "123456");
} catch (Exception e) { // jdbc:oracle:thin:@172.22.0.84:1521:devdb
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
// 关闭数据库
public void closeDB(ResultSet rs, Statement smt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (smt != null) {
smt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭数据库连接失败!");
e.printStackTrace();
}
}
// 测试连接是否成功
public static void main(String[] args) {
//2--建立数据库连接
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch2.4", "root", "123456");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//3--使用Connection 对象创建Statement,为传递sql语句做准备
Statement st;
try {
st = conn.createStatement();
for(int i = 1;i<20;i++) {
String rq = "20190806";
String monery = "1" + i;
int jifen = i;
String zh= "110000" + i;
int a = i%5;
String jg = "";
if(a==1) {
jg= "1000";
}else if(a==2) {
jg= "2000";
}else if(a==3) {
jg= "3000";
}else if(a==4) {
jg= "4000";
}else {
jg= "5000";
}
String sql = "insert into table value ("+ rq +","+monery+","+jifen+","+zh+","+jg+")";
st.executeUpdate(sql);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}