📄 databaseinit.java
字号:
package dao;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
public class DataBaseInit {
/**
* 插入文本数据。
* @throws IOException
*/
public static void insertvalues()throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("insertvalues.sql")));
String temp=null;
try {
System.out.println("正在插入商品信息,请稍等...");
while((temp=br.readLine())!=null){
if(!temp.equals("")&&!temp.startsWith("--")){
DBCon.execute(temp.substring(0,temp.length()-1));
}
}
System.out.println("商品信息插入完毕。");
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
/**
* 插入商品的图片。
* @throws SQLException
* @throws IOException
*/
public static void productPicInit()throws SQLException,IOException{
StringBuffer sql=new StringBuffer();
sql.append("insert into photo (pid,image) values (?,?)");
try {
System.out.println("正在插入商品图片,请稍等...");
for(int i=1;i<100;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/pics/"+(i%20+1)+".png");
ps.setInt(1, i);
ps.setBinaryStream(2, is, is.available());
ps.execute();
}
System.out.println("商品图片插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
/**
* 插入分类图片
* @throws SQLException
* @throws IOException
*/
public static void categoryPicInit() throws SQLException,IOException{
StringBuffer sql=new StringBuffer();
sql.append("update category set photo=? where id=? ");
try {
System.out.println("正在插入分类图片,请稍等...");
for(int i=1;i<99;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/categorypic/"+(i%20+1)+".gif");
ps.setBinaryStream(1, is, is.available());
ps.setInt(2, i);
ps.execute();
}
System.out.println("商品分类图片插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
/**
* 插入商品的大图。
* @throws SQLException
* @throws IOException
*/
public static void bigPicInit() throws SQLException,IOException{
int[] products={
56,68,67,26, 38,62,28,44, 88,36,59,66,
92,29,83,90, 58,82, 34,87,89,4};
StringBuffer sql=new StringBuffer();
sql.append("update photo set image=? where id=? ");
try {
System.out.println("正在插入商品大图,请稍等...");
for(int i=0;i<products.length;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/bigpic/"+(i+1)+".jpg");
ps.setBinaryStream(1, is, is.available());
ps.setInt(2, products[i]);
ps.execute();
}
System.out.println("商品大图插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
/**
* 帖子初始化
* @throws SQLException
*/
public static void postInit() throws SQLException{
String [] posttitle = {"SwissGear瑞士军刀 笔记本单肩包 15.4寸 GA7483",
"TARGUS泰格斯 轻巧型迷你斜背包 9寸 TSM071AP",
"HP惠普 商务笔记本电脑包 KK970PA#AB2",
"金士顿kingston数码产品真伪验证方法",
"买威刚C802优盘 送110元个人相册书",
"人性化操作体验!城际通J630评测",
"最受欢迎 十款二千元级别车载GPS推荐",
"中小企业首选 热门黑白激光一体机推荐",
"3G iPhone美销量首超RAZR"};
String [] user = {"xindou","tudou","tommy","路人甲","路人乙","admin","master","root","tarena"};
String [] content = {"顶~","灌水ing...","貌似有道理。。","鄙视楼主。","同意楼上。","做人要厚道。","灌,是一种美德。"};
StringBuffer sql = new StringBuffer();
sql.append("insert into post (title,transactionpoint,readaccess,icon,content,forbideurl,");
sql.append("forbidesmile,forbidegerweb,usesign,floor,sendtime,tid,uid,lastmodified) ");
sql.append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
System.out.println("正在插入论坛帖子信息,请稍等...");
Connection con = DBCon.getCon();
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(sql.toString());
int floor =0;
//模拟产生约1500条帖子
for(int i=1;i<100;i++){ //在主题中循环,为每个主题插入n条帖子
floor = RandomUtil.nextInt(30); //假设回帖数量不会超过30条
for(int f=0;f<floor;f++){
pstmt.setString(1, posttitle[RandomUtil.nextInt(posttitle.length)]); //title
pstmt.setInt(2, RandomUtil.nextInt(300));
pstmt.setInt(3, 2);
pstmt.setInt(4, 2);
pstmt.setString(5, content[RandomUtil.nextInt(content.length)]);
pstmt.setBoolean(6, false);
pstmt.setBoolean(7, false);
pstmt.setBoolean(8, false);
pstmt.setBoolean(9, false);
pstmt.setInt(10, f);
pstmt.setDate(11, new Date(Date.valueOf("2008-1-1").getTime()+(long)(f*1000*60*60*f)));
pstmt.setInt(12, i);
pstmt.setInt(13, RandomUtil.nextInt(user.length)+1);
pstmt.setDate(14, RandomUtil.getNextDate(Date.valueOf("2008-6-1"),Date.valueOf("2008-11-25")));
pstmt.addBatch();
}
pstmt.executeBatch();
}
System.out.println("论坛帖子信息插入完毕。");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("帖子信息插入异常!");
throw e;
}
}
/**
* 主题初始化
*/
public static void topicInit() throws SQLException{
String [] topictype ={"讨论","精华","求助","海阔天空"};
String [] user = {"xindou","tudou","tommy","路人甲","路人乙"};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -