zoukankan      html  css  js  c++  java
  • 数据库中字段随机添加汉字

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

  • 相关阅读:
    包装类
    项目基础架构搭建
    开发环境的搭建和Shell编程
    Linux系统概述和编程基础
    java.lang.ExceptionInInitializerError
    mybatis中文文档
    如何造轮子
    Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099
    JDK 1.7与JDK 1.8版本的完美切换
    JDBC连接步骤
  • 原文地址:https://www.cnblogs.com/liangguangqiong/p/7810765.html
Copyright © 2011-2022 走看看