生成随机数及随机字母

Java连接数据库:

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


Java操作数据库生成随机数及字母:

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

你可能感兴趣的:(随机数)