教育网研究一 hibernate 插入和保存对象

hibernate保存出错
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
更新报错怎么办
原来是有取值为空
 DBOperate dbo = new DBOperate();
     news_inf inf1 = new news_inf();
   inf1 = (news_inf) dbo.getObjetctById(inf1, "news_inf", request.getParameter("id"));//先这里取值为空了
  //   inf1.setId(PubFunc.parseInt(request.getParameter("id")));//这句可以不要的
  inf1.setTitle(new String(request.getParameter("title").getBytes("gb2312"), "iso-8859-1"));//我有过滤器,不用考虑写反了
      inf1.setDatetime(d.toLocaleString());
      inf1.setTypeid(PubFunc.parseInt(request.getParameter("typeid"))); 
      inf1.setFrees(request.getParameter("frees"));
      inf1.setNewss(request.getParameter("newss"));
      inf1.setRedian(request.getParameter("redian"));
      inf1.setContent(new String(request.getParameter("content").getBytes("gb2312"),"iso-8859-1"));
 
      dbo.insert_update(inf1, "update");

这里把我的 DBOperate.java也帖出来
package com.pp.db;
import my.HibernateUtil;
import java.sql.*;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
import javax.naming.*;
import com.jetsum.util.PubFunc;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import jxl.*;
import java.io.FileInputStream;
import java.io.File;
import my.bean.member;
import java.sql.PreparedStatement;
import java.io.*;

public class DBOperate {
 private static Logger log = Logger.getLogger("helloappLogger");

  private Connection conn = null;

  private Session session = null;

  private PreparedStatement ptmt = null;

  private ResultSet rs = null;

  private SessionFactory sessionFactory = null;

  Transaction transaction = null;

  private int rowCount;

  private int pageCount;

  private int length;

  private String pagestr;

  public DBOperate() {
  }

  public int getLength() {
    return (this.length);
  }

  public void setLength(int length) {
    this.length = length;
  }

  /**
   * 得到值对象
   * @param object Object
   * @return Object
   */
  public Object getObjetctById(Object object, String objectstring, String id) {
    int i = 0;
    try {
      conn = DBConnection.getConnection();
      SessionFactory sessionFactory = this.getSessionFactory();
      if (sessionFactory != null) {
        session = sessionFactory.openSession(conn);
        transaction = session.beginTransaction();
        String hql = "from " + objectstring + " where id=" + id;
        //System.out.println("hql:" + hql);
        Query query = session.createQuery(hql);
        List list = query.list();
        if (list.size() > 0) {
          object = list.get(0);
        }
        //System.out.println("listsize:" + list.size());
        transaction.commit();
      }

    }
    catch (Exception e) {
      e.printStackTrace();
      log.error(PubFunc.getNow() + "Hibernate得到对象值错误!" + e.getMessage());
      if (e instanceof HibernateException) {
        i = ( (HibernateException) e).getThrowableCount();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (HibernateException) e1).getThrowableCount();
        }
      }
      if (e instanceof SQLException) {
        i = ( (SQLException) e).getErrorCode();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (SQLException) e).getErrorCode();
        }
      }

    }
    finally {
      close(session);
      DBConnection.closeConnection(conn);
    }
    return object;
  }

  /**
   * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
   * @param c char 需要判断的字符
   * @return boolean 返回true,Ascill字符
   */
  public static boolean isLetter(char c) {
    int k = 0x80;
    return c / k == 0 ? true : false;
  }

  /**
   * 判断一个字符是否是空格
   * @param c char 需要判断的字符
   * @returnboolean 返回true,是空格
   */
  public static boolean isSpace(char c) {
    int k = 0x32;
    return c / k == 0 ? true : false;
  }

  /**
   * 截取指定长度(中文字符数)的字符串.(两个asciII字符算一个中文字符)
   */
  public static String getTitle(String str, int length) {
    int nowLength = 0; // 当前所得字符长度(以asciII长度计算,一个中文字符算2个asciII字符)
    int countAllLength = 0; // 统计全部的字符串长度(以asciII长度计算,一个中文字符算2个asciII字符)
    int total = 2 * length;
    StringBuffer retStr = new StringBuffer("");
    for (int i = 0; i < str.length(); i++) {
      char tmpChar = str.charAt(i);

      // 如果当前取得的字符为asciII字符
      if (isLetter(tmpChar)) {
        if (nowLength < total - 2) {
          retStr.append(tmpChar);
          nowLength++;
        }
        countAllLength++;
      }

      // 如果当前取得的字符为其它字符(如汉,日,韩文字符)
      else {
        if (nowLength < (total - 3)) {
          retStr.append(tmpChar);
          nowLength += 2;
        }
        countAllLength += 2;
      }
    }

    // 如果字符总数小于或等于要截取的长度则直接返回该字符串
    if (countAllLength <= total) {
      return str;

      // 如果字符总数大于要截取的长度则返回截取后的长度
    }
    else {
      // retStr.append('…');
      return retStr.toString();
    }
  }

  /**
   * 删除值对象
   * @param obj
   * @return
   */
  public int deleteObject(Object obj) {
    int i = 0;
    try {
      conn = DBConnection.getConnection();
      SessionFactory sessionFactory = this.getSessionFactory();
      if (sessionFactory != null) {
        session = sessionFactory.openSession(conn);
        transaction = session.beginTransaction();
        session.delete(obj);
        transaction.commit();
      }
    }
    catch (Exception e) {
      log.error(PubFunc.getNow() + "Hibernate删除失败!" + e.getMessage());
      if (e instanceof HibernateException) {
        i = ( (HibernateException) e).getThrowableCount();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (HibernateException) e1).getThrowableCount();
        }
      }
      if (e instanceof SQLException) {
        i = ( (SQLException) e).getErrorCode();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (SQLException) e).getErrorCode();
        }
      }
    }
    finally {
      close(session);
      DBConnection.closeConnection(conn);
    }
    return i;
  }

  /**
   * 更新位置顺序
   * @param tablename String
   * @param items String
   * @param ziduan String
   * @return int
   */
  public int updateOrderBy(String tablename, String items, String ziduan) {
    int i = 0;
    String[] arrItems = items.split(",");
    Statement stmt = null;
    try {
      conn = DBConnection.getConnection();
      stmt = conn.createStatement();
      conn.setAutoCommit(false);
      for (int v = 0; v < arrItems.length; v++) {
        String sql = "update " + tablename + " set " + ziduan + "=" + v +
            " where id="
            + Integer.parseInt(arrItems[v]);
        i = stmt.executeUpdate(sql);
        if (i < 1) {
          conn.rollback();
          break;
        } // end if (i < 1).
      } // end for i.
      conn.commit();
      conn.setAutoCommit(true);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeStatement(stmt);
      DBConnection.closeConnection(conn);
    }
    return i;
  }

  /**
   * 得到会员评价分数
   * @param memberid int
   * @throws Exception
   * @return int
   */
  public int getMemberNums(int memberid) throws Exception {
    DBOperate dbo = new DBOperate();
    List listone = new ArrayList();
    List listtwo = new ArrayList();
    List listthree = new ArrayList();
    int nums = 0;
    dbo.setLength(10000000);
    String hql1 = "from sourcets where memberid=" + memberid + " and voteid='" +
        new String("好".getBytes("gbk"), "iso-8859-1") + "'";
    String hql2 = "from sourcets where memberid=" + memberid + " and voteid='" +
        new String("中".getBytes("gbk"), "iso-8859-1") + "'";
    String hql3 = "from sourcets where memberid=" + memberid + " and voteid='" +
        new String("差".getBytes("gbk"), "iso-8859-1") + "'";

    listone = (List) dbo.findSQLHibernate(hql1, 1);
    listtwo = (List) dbo.findSQLHibernate(hql2, 1);
    listthree = (List) dbo.findSQLHibernate(hql3, 1);

    nums=2*listone.size()+listtwo.size()-listthree.size();
    System.out.println("nums:"+nums);
    return nums;
  }

  public int getRowCount() {
    return this.rowCount;
  }

  /**
   * Hibernate得到列表
   * @param hql String
   * @param ipage int   //实现分页取值
   * @return List
   */
  public List findSQLHibernate(String hql, int ipage) {
    List list = null;
    int i = 0;
    try {
      conn = DBConnection.getConnection();
    //  Configuration config = new Configuration().configure();
    //  SessionFactory sessionFactory = config.buildSessionFactory();
      SessionFactory sessionFactory = this.getSessionFactory();
      if (sessionFactory != null) {
        session = sessionFactory.openSession(conn);
        transaction = session.beginTransaction();
        list=session.createQuery(hql).list();
        transaction.commit();
        rowCount = list.size();
        int offset = 1;
        int pagesize = getLength();
        if (getLength() < 1) {
          pagesize = rowCount;
          pageCount = 1;
        }
        else {
          pageCount = rowCount / getLength()
              + ( (rowCount % getLength()) > 0 ? 1 : 0);
          offset = (ipage - 1) * getLength() + 1;
          if (offset < 1) {
            offset = 1;
          }
          if (offset > rowCount) {
            offset = rowCount;
          }
        }
        int toNums = (offset - 1) + pagesize;
        if (list.size() > 0) {
          list = list.subList( (offset - 1),
                              toNums > rowCount ? rowCount : toNums);
        }
      }
    }
    catch (Exception e) {
      log.error(PubFunc.getNow() + "Hibernate得到列表!" + e.getMessage());
      e.printStackTrace();
      if (e instanceof HibernateException) {
        i = ( (HibernateException) e).getThrowableCount();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (HibernateException) e1).getThrowableCount();
        }
      }
      if (e instanceof SQLException) {
        i = ( (SQLException) e).getErrorCode();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (SQLException) e).getErrorCode();
        }
      }
    }
    finally {
      close(session);
      DBConnection.closeConnection(conn);
    }

    return list;
  }

  public String getMemberid(String membername) {
    String sql = "select id from member where username='" + membername + "'";
    String memberid = "";
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      if (rs.next()) {
        memberid = rs.getString("id");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    return memberid;
  }

  public int getDays(String memberid, int days) {
    int dayss = 0;
    int dayvs = 0;
    String sql =
        "select (TO_DAYS(NOW())-TO_DAYS(datetime)) as dayss from member where id=" +
        memberid;
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      if (rs.next()) {
        dayss = rs.getInt("dayss");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    //System.out.println("days:"+dayss);
    dayvs = days - dayss;
    if (dayvs < 0) {
      dayvs = 0;
    }
    return dayvs;
  }

  public String getUsername(String membername) throws Exception {
    String userid = "";
    String hql = "from member where username='" +
        new String(membername.getBytes("gbk"), "iso-8859-1") + "'";
    DBOperate dbo = new DBOperate();
    dbo.setLength(1); ;
    List list1 = (List) dbo.findSQLHibernate(hql, 1);
    if (list1.size() > 0) {
      member member1 = (member) list1.get(0);
      userid = String.valueOf(member1.getId());
    }
    return userid;
  }

  public boolean syday(String memberid, int days) {
    boolean flag = true;
    if (days == 0) {
      flag = false;
    }
    String sql = "select id from member where id=" + memberid +
        " and (TO_DAYS(NOW())-TO_DAYS(datetime))>" + days;
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      if (rs.next()) {
        flag = false;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    return flag;
  }

  public List findSQLHibernateMySQL(String hql) {

    List list = new ArrayList();
    try {
      conn = DBConnection.getConnection();
      SessionFactory sessionFactory = this.getSessionFactory();
      if (sessionFactory != null) {
        session = sessionFactory.openSession(conn);
        Query q = this.session.createQuery(hql);
        list = q.list();
        Iterator iter = list.iterator();
        if (iter.hasNext()) {
          return list;
        }
      }
    }
    catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
    finally {
      close(session);
      DBConnection.closeConnection(conn);
    }
    return list;
  }

  /**
   * 插入或更新
   * @param obj Object 对象
   * @param worktype  worktype为insert 或者 update
   * String 操作类型 author:李永胜
   */
  public int insert_update(Object obj, String worktype) {
    int i = 0;
    try {
      conn = DBConnection.getConnection();
      SessionFactory sessionFactory = this.getSessionFactory();
      if (sessionFactory != null) {
        session = sessionFactory.openSession(conn);
        transaction = session.beginTransaction();
        if ("insert".equals(worktype)) {
          session.save(obj);
        }
        if ("update".equals(worktype)) {
          session.update(obj);
        }
        transaction.commit();
      }
    }
    catch (Exception e) {
      log.error(PubFunc.getNow() + "Hibernate插入或更新失败!" + e.getMessage());
      e.printStackTrace();
      if (e instanceof HibernateException) {
        i = ( (HibernateException) e).getThrowableCount();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (HibernateException) e1).getThrowableCount();
        }
      }
      if (e instanceof SQLException) {
        i = ( (SQLException) e).getErrorCode();
        try {
          transaction.rollback();
        }
        catch (Exception e1) {
          i = ( (SQLException) e).getErrorCode();
        }
      }
    }
    finally {
      close(session);
      DBConnection.closeConnection(conn);
    }
    return i;
  }

  public String getHuiYuanJibie(int code) {
    String hname = "";
    String sql = "select * from hyjb order by qcode";
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      while (rs.next()) {
        System.out.println("code:" + rs.getInt("qcode"));
        if (code >= rs.getInt("qcode")) {
          hname = new String(rs.getString("hyjb").getBytes("iso-8859-1"), "gbk");
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    return hname;
  }

  public String getSourceid(String sourcename) {
    String sourceid = "";
    String sql =
        "select id from Source_inf where state='1' and filename like '%" +
        sourcename + "%' order by id desc";
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      while (rs.next()) {
        sourceid = sourceid + rs.getString("id") + ",";
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    return sourceid;
  }

  public String setOrderids(String table) {
    String orderid = "";
    java.util.Date date = new java.util.Date();
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy-MM-dd");
    String datetimes = format.format(date);
    String sql = "from jfdh where date_format(datetime,'%Y-%m-%d')='" +
        datetimes + "'";

    System.out.println("datetimes:" + datetimes);
    if ("1".equals(table)) {
      sql = "from czjl where date_format(datetime,'%Y-%m-%d')='" + datetimes +
          "'";
    }
    DBOperate dbo = new DBOperate();
    dbo.setLength(5000000);
    List list1 = new ArrayList();
    list1 = (List) dbo.findSQLHibernate(sql, 1);
    System.out.println("size:" + list1.size());

    String orderid_sub = String.valueOf( (list1.size() + 1));
    if (orderid_sub.length() == 1) {
      orderid_sub = "0000" + orderid_sub;
    }
    if (orderid_sub.length() == 2) {
      orderid_sub = "000" + orderid_sub;
    }

    if (orderid_sub.length() == 3) {
      orderid_sub = "00" + orderid_sub;
    }

    if (orderid_sub.length() == 4) {
      orderid_sub = "0" + orderid_sub;
    }

    String datetimess = datetimes.substring(2, 10).replaceAll("-", "");
    datetimess = datetimess.replaceAll(" ", "");
    orderid = datetimess + orderid_sub;
    return orderid;
  }

  public int getCountsxzjl(String sourceid, String qtime) {
    int counts = 0;
    String hql = null;
    try {
      hql = "from sourcets where sourceid=" + sourceid +
          " and voteid<>'" + new String("好".getBytes("gbk"), "iso-8859-1") +
          "' and voteid<>'" + new String("中".getBytes("gbk"), "iso-8859-1") +
          "' and voteid<>'" + new String("差".getBytes("gbk"), "iso-8859-1") +
          "' order by datetime desc";
      if (!"".equals(qtime)) {
        hql = "from sourcets where sourceid=" + sourceid +
            " and date_format(datetime,'%Y-%m-%d')='" + qtime +
            "' and voteid<>'" + new String("好".getBytes("gbk"), "iso-8859-1") +
            "' and voteid<>'" + new String("中".getBytes("gbk"), "iso-8859-1") +
            "' and voteid<>'" + new String("差".getBytes("gbk"), "iso-8859-1") +
            "'  order by datetime desc";
      }

      DBOperate dbo = new DBOperate();
      dbo.setLength(5000000);
      List list1 = new ArrayList();

      list1 = (List) dbo.findSQLHibernate(hql, 1);
      counts = list1.size();

    }
    catch (UnsupportedEncodingException ex) {
    }
    return counts;
  }

  public int getCounts(String memberid, String qtime, String ztime) {
    int counts = 0;
    String hql = "from xzjl where memberidx=" + memberid + " and datetime >='" +
        qtime + "' and datetime <= '" + ztime + "' order by datetime desc";

    DBOperate dbo = new DBOperate();
    dbo.setLength(5000000);

    if ("".equals(qtime) && "".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberid + " order by datetime desc";
    }

    if (!"".equals(qtime) && "".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberid + " and datetime >='" +
          qtime + "' order by datetime desc";
    }

    if ("".equals(qtime) && !"".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberid + " and datetime <='" +
          ztime + "' order by datetime desc";
    }

    if (!"".equals(qtime) && !"".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberid + " and datetime>='" +
          qtime + "' and datetime <='" +
          ztime + "' order by datetime desc";
    }

    System.out.println("hql:" + hql);

    List list1 = new ArrayList();

    list1 = (List) dbo.findSQLHibernate(hql, 1);
    counts = list1.size();
    return counts;
  }

  public int getCounts(String memberidx, String qtime, String ztime,
                       String typeid) throws Exception {
    int i = 0;
    String hql = "from xzjl where memberidx=" + memberidx + " and datetime >='" +
        qtime + "' and datetime <= '" + ztime + "' and sourceid in (" +
        getSourceid(Integer.parseInt(typeid)).substring(0,
        (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
        ") order by datetime desc";

    DBOperate dbo = new DBOperate();
    dbo.setLength(5000000);

    if ("".equals(qtime) && "".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberidx + " and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && "".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberidx + " and datetime >='" +
          qtime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if ("".equals(qtime) && !"".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberidx + " and datetime <='" +
          ztime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && !"".equals(ztime)) {
      hql = "from xzjl where memberidx=" + memberidx + " and datetime>='" +
          qtime + "' and datetime <='" +
          ztime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    System.out.println("hql:" + hql);

    List list1 = new ArrayList();

    list1 = (List) dbo.findSQLHibernate(hql, 1);
    i = list1.size();
    return i;
  }

  public int getCountsnew(String qtime, String ztime,
                          String typeid, String sourcename) throws Exception {
    int i = 0;
    String hql = "";

    DBOperate dbo = new DBOperate();
    dbo.setLength(5000000);

    if ("".equals(qtime) && "".equals(ztime) && "".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl order by datetime desc";
    }

    if (!"".equals(qtime) && "".equals(ztime) && "".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime>='" + qtime + "' order by datetime desc";
    }

    if ("".equals(qtime) && !"".equals(ztime) && "".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime<='" + ztime + "' order by datetime desc";
    }

    if (!"".equals(qtime) && !"".equals(ztime) && "".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime>='" + qtime + "' and datetime<='" + ztime +
          "' order by datetime desc";
    }

    if ("".equals(qtime) && "".equals(ztime) && !"".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && "".equals(ztime) && !"".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime >='" +
          qtime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if ("".equals(qtime) && !"".equals(ztime) && !"".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime <='" + ztime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && !"".equals(ztime) && !"".equals(typeid) &&
        "".equals(sourcename)) {
      hql = "from xzjl where datetime>='" + qtime + "' and datetime <='" +
          ztime + "' and sourceid in (" +
          getSourceid(Integer.parseInt(typeid)).substring(0,
          (getSourceid(Integer.parseInt(typeid)).length() - 1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && "".equals(ztime) && !"".equals(sourcename)) {
      hql = "from xzjl where datetime >='" +
          qtime + "' and sourceid in (" +
          getSourceid(new String(sourcename.getBytes("gbk"), "iso-8859-1")).
          substring(0,
                    (getSourceid(new String(sourcename.getBytes("gbk"),
                                            "iso-8859-1")).length() -
                     1)) +
          ") order by datetime desc";
    }

    if ("".equals(qtime) && "".equals(ztime) && !"".equals(sourcename)) {
      hql = "from xzjl where sourceid in (" +
          getSourceid(new String(sourcename.getBytes("gbk"), "iso-8859-1")).
          substring(0,
                    (getSourceid(new String(sourcename.getBytes("gbk"),
                                            "iso-8859-1")).length() -
                     1)) +
          ") order by datetime desc";
    }

    if ("".equals(qtime) && !"".equals(ztime) && !"".equals(sourcename)) {
      hql = "from xzjl where datetime <='" +
          ztime + "' and sourceid in (" +
          getSourceid(new String(sourcename.getBytes("gbk"), "iso-8859-1")).
          substring(0,
                    (getSourceid(new String(sourcename.getBytes("gbk"),
                                            "iso-8859-1")).length() -
                     1)) +
          ") order by datetime desc";
    }

    if (!"".equals(qtime) && !"".equals(ztime) && !"".equals(sourcename)) {
      hql = "from xzjl where datetime >='" + qtime + "' and datetime <='" +
          ztime + "' and sourceid in (" +
          getSourceid(new String(sourcename.getBytes("gbk"), "iso-8859-1")).
          substring(0,
                    (getSourceid(new String(sourcename.getBytes("gbk"),
                                            "iso-8859-1")).length() -
                     1)) +
          ") order by datetime desc";
    }

    System.out.println("hql:" + hql);

    List list1 = new ArrayList();

    list1 = (List) dbo.findSQLHibernate(hql, 1);
    i = list1.size();
    return i;
  }

  public String getSourceid(int typeid) {
    String sourceid = "";
    String typeids = getTypeid(String.valueOf(typeid));
    String sql = "select id from Source_inf where state='1' and typeid  in (" +
        typeids.substring(0, (typeids.length() - 1)) + ") order by id desc";
    //System.out.println("sql:"+sql);
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      while (rs.next()) {
        sourceid = sourceid + rs.getString("id") + ",";
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      DBConnection.closeResultSet(rs);
      DBConnection.closePtmt(ptmt);
      DBConnection.closeConnection(conn);
    }
    return sourceid;
  }

  public String getTypeid(String firstid) {
    String typeid = firstid + ",";
    String sql = "select id from Source_type where state='1' and levels='" +
        firstid + "'";
    String sql1 = "select id from Source_type where state='1' and levels=?";
    ResultSet rs1 = null;
    PreparedStatement ptmt1 = null;
    try {
      conn = DBConnection.getConnection();
      ptmt = conn.prepareStatement(sql);
      rs = ptmt.executeQuery();
      while (rs.next()) {
        typeid = typeid + rs.getString("id") + ",";
        ptmt1 = conn.prepareStatement(sql1);
        ptmt1.setString(1, rs.getString("id"));
        rs1 = ptmt1.executeQuery();
        while (rs1.next()) {
          typeid = typeid + rs1.getString("id") + ",";
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      //System.out.println("typeid:" + typeid);
      DBConnection.closeResultSet(rs);
      DBConnection.closeResultSet(rs1);
      DBConnection.closePtmt(ptmt);
      DBConnection.closePtmt(ptmt1);
      DBConnection.closeConnection(conn);
    }
    return typeid;
  }

  /**
   * 得到sessionFactory
   * @return SessionFactory author:李永胜
   */
  public SessionFactory getSessionFactory() {
  
        try {
          sessionFactory = HibernateUtil.getSessionFactory();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
    
    return sessionFactory;
  }

  public static void main(String[] argv) {
    DBOperate dbo = new DBOperate();
    //dbo.load_inf("c://789.xls");
    try {
      dbo.updateTgcode();
    }
    catch (Exception ex) {
    }
  }

  /**
   * 关闭Session
   * @param session
   * author: 李永胜
   */
  public void close(Session session) {
    try {
      if (session != null) {
        session.flush();
        session.close();
      }
     
    }
    catch (HibernateException e) {
      log.error(PubFunc.getNow() + "清空 session失败!" + e);
    }
  }

  /**
   * 得到分页字符串
   * @param ipage int
   * @param levelid String
   * @return String
   */
  public String getPagestr1(String jspname, int ipage, String levelid) {
    String strPage = "";
    if (getLength() > 0) {
      strPage +=
          "<font style='color: #000000;text-decoration: none;font-size: 12px;'>[第";
      strPage += String.valueOf(ipage);
      strPage += "页]每页" + getLength() + "条";
      strPage += "共";
      strPage += String.valueOf(pageCount);
      strPage += "页";
      strPage += String.valueOf(rowCount);
      strPage += "条  ";

      int istart, iend;
      istart = ipage - 5;
      if (istart < 0) {
        istart = 0;
      }
      iend = istart + 10;
      if (iend > pageCount) {
        iend = pageCount;
      }
      istart = iend - 10;
      if (istart < 0) {
        istart = 0;
      }
    }
    int shang = ipage - 1;
    if (shang <= 0) {
      shang = 1;
    }
    int end = ipage + 1;
    if (end >= pageCount) {
      end = pageCount;
    }
    String str = "&nbsp;&nbsp;&nbsp;<a href='" + jspname +
        "?page=1&levelid=" + levelid + "' style='text-decoration:none'>首 页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + shang +
        "&levelid=" + levelid + "' style='text-decoration:none'>上一页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + end +
        "&levelid=" + levelid + "' style='text-decoration:none'>下一页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + pageCount +
        "&levelid=" + levelid + "' style='text-decoration:none'>尾 页</a>";
    this.pagestr = strPage + str;
    return pagestr;

  }

  /**
   * 得到分页字符串
   * @param ipage int
   * @param levelid String
   * @return String
   */
  public String getPagestr11(String jspname, int ipage, String levelid,
                             String dat, String nums) {
    String strPage = "";
    if (getLength() > 0) {
      strPage +=
          "<font style='color: #000000;text-decoration: none;font-size: 12px;'>[第";
      strPage += String.valueOf(ipage);
      strPage += "页]每页" + getLength() + "条";
      strPage += "共";
      strPage += String.valueOf(pageCount);
      strPage += "页";
      strPage += String.valueOf(rowCount);
      strPage += "条  ";

      int istart, iend;
      istart = ipage - 5;
      if (istart < 0) {
        istart = 0;
      }
      iend = istart + 10;
      if (iend > pageCount) {
        iend = pageCount;
      }
      istart = iend - 10;
      if (istart < 0) {
        istart = 0;
      }
    }
    int shang = ipage - 1;
    if (shang <= 0) {
      shang = 1;
    }
    int end = ipage + 1;
    if (end >= pageCount) {
      end = pageCount;
    }
    String str = "&nbsp;&nbsp;&nbsp;<a href='" + jspname +
        "?page=1&levelid=" + levelid + "&dat=" + dat + "&nums=" + nums +
        "' style='text-decoration:none'>首 页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + shang +
        "&levelid=" + levelid + "&dat=" + dat + "&nums=" + nums +
        "' style='text-decoration:none'>上一页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + end +
        "&levelid=" + levelid + "&dat=" + dat + "&nums=" + nums +
        "' style='text-decoration:none'>下一页</a>";
    str += "&nbsp;<a href='" + jspname + "?page=" + pageCount +
        "&levelid=" + levelid + "&dat=" + dat + "&nums=" + nums +
        "' style='text-decoration:none'>尾 页</a>";
    this.pagestr = strPage + str;
    return pagestr;

  }

  /**
   * 得到分页字符串
   * @param ipage int
   * @param

 

//中文乱码的处理

//String nettype=  new String(request.getParameter("type").getBytes("ISO-8859-1"),"utf-8");
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值