nullITeye - AG环亚娱乐集团

nullITeye

2019年03月06日10时44分28秒 | 作者: 凌寒 | 标签: | 浏览: 1490

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= quot;com.mysql.jdbc.Driver quot;;
  private static final String USER_NAME= quot;root quot;;
  private static final String PASSWORD= quot;root quot;;
  private static Connection conn = null;
  private static Statement st = null;
//静态块
static{
  try {
  Class.forName(DRIVER_STR);
  String CONN_STR= quot;jdbc:mysql://localhost:3306/smit quot;;
  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( quot;更新操作失利 quot;);
  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( quot;查找失利 quot;);
  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 w++) {
for (int i = 0; i 500; i++) {
do {
flag = false;
int num = (int) (Math.random() * 500 + 1);
for (int j = 0; j 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);
}
}
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章