package test;
import hibernate.News;
import hibernate.NewsDAO;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Main {
//hibernate随机插入汉字
public static void main(String[] args) {
for (int i = 0;i<10;i++) {
//数据库的表和字段
News n= new News();
String s1 = getRandomJianHan(100);
String title = getRandomJianHan(10);
n.setNtitle(title);
n.setContext(s1);
n.setPath(getRandomJianHan(5));
n.setNname(getRandomJianHan(3));
n.setNtime(getDate("2000-01-01 00:00:00","2017-11-08 00:00:00"));
NewsDAO dao = new NewsDAO();
dao.merge(n);
}
}
//设置时间格式
public static Timestamp getDate(String s,String e){
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start = sdf.parse(s).getTime();
long end = sdf.parse(e).getTime();
double t = Math.random()*(end-start)+start;
return new Timestamp((long)t);
}catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
//随机获取汉字
public static String getRandomJianHan(int len){
String ret="";
for(int i=0;i<len;i++){
String str = null;
int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39))); //获取高位值
lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
try
{
str = new String(b, "GBk"); //转成中文
}
catch (UnsupportedEncodingException ex)
{
ex.printStackTrace();
}
ret+=str;
}
return ret;
}
}