生成随机数及随机字母

[color=red][b]Java连接数据库:[/b][/color]

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBconnect {
private static final String DRIVER_STR="com.mysql.jdbc.Driver";
private static final String USER_NAME="root";
private static final String PASSWORD="root";
private static Connection conn = null;
private static Statement st = null;
//静态块
static{
try {
Class.forName(DRIVER_STR);
String CONN_STR="jdbc:mysql://localhost:3306/smit";
conn = DriverManager.getConnection(CONN_STR,USER_NAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

//更新操作
public boolean upDate(String sql)
{
int n = 0;

System.out.println(sql);

try {
st = conn.createStatement();
n = st.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("更新操作失败");
e.printStackTrace();
}

if (n>0)
return true;
return false;
}
//查找操作
public ResultSet query(String sql)
{
ResultSet res = null;
System.out.println(sql);
try {
st = conn.createStatement();
res = st.executeQuery(sql);
} catch (SQLException e) {
System.out.println("查找失败");
e.printStackTrace();
res= null;
}
return res;
}
}


[color=red][b]Java操作数据库生成随机数及字母:[/b][/color]

public class Click_Collection {
// 定义线程的名称
String name = null;

// 构造函数
public Click_Collection() {
}

public Click_Collection(String name) {
this.name = name;
}

static int[] a = new int[500];
static boolean flag = false;
static String chars = "ABCDE";
static String sql = null;

public static void main(String[] args) {
for (int w = 0; w < 2; w++) {
for (int i = 0; i < 500; i++) {
do {
flag = false;
int num = (int) (Math.random() * 500 + 1);
for (int j = 0; j < i; j++) {
if (num == a[j]) {
flag = true;
break;
}
}
if (!flag) {

a[i] = num;
}
} while (flag);
// System.out.println(a[i] + "\t");
// 频道列表
// 随机写数字
int n = (int) (Math.random() * 17); // 切换前的频道名称
int m = (int) (Math.random() * 11); // 切换前的频道名称
int x = (int) (Math.random() * 17); // 切换后的频道名称
int y = (int) (Math.random() * 11); // 切换后的频道名称
int l = (int) (Math.random() * 10);
int r = (int) (Math.random() * 2);
String re[] = { "爱奇艺", "VIKU" };
int scil[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
String s[] = { "cctv1", "cctv2", "cctv3", "cctv4", "cctv5",
"cctv6", "cctv7", "cctv8", "cctv9", "cctv10", "cctv11",
"cctv12", "北京卫视", "天津卫视", "内蒙古卫视", "河南卫视", "安徽卫视" };
String p[] = { "我爱中国味", "百变大咖秀", "快乐大本营", "天天向上", "笑霸来了",
"爱笑会议室", "壹周立波秀", "梦想星搭档", "郭的秀", "非常了得", "中国梦想秀" };
sql = "insert into collection(apik, style, operator,pn) values ('"
+ a[i]
+ "',"
+ "'col', "
+ "'"
+ chars.charAt((int) (Math.random() * 5))
+ "'"
+ ",'"
+ p[m]
+ "'"
+");";
Click_Collection th1 = new Click_Collection("A");
th1.run();
}
}
}
private DBconnect db = new DBconnect();
public void run() {
db.upDate(sql);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值