循环批量造数据

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();
		}

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值