现在的位置: 首页 > 综合 > 正文

今天遇到关于JAVA INTEGER类型的问题 不过还好 现在解决了,

2017年11月10日 ⁄ 综合 ⁄ 共 6295字 ⁄ 字号 评论关闭
我的开发用到了struts和hibernate 数据库是用MYSQL,里面的一个数据表创建如下
在MYSQL的数据库中没有INT类型,所以只能用Integer类型,但在java中Integer中不是基本数据类型,
所以要对Integer的数据进行操作就有点小麻烦,我开始没注意到这个问题,后来找到Integer的构造方法
把要操作的int数据传递给这个构造方法获取对应的Integer对象,在对数据库进行操作。

-------------------------
问题总结:出现这个问题还是自己的对java数据类型操作的不熟练,还需要继续努力。
-------------------------

数据库创建代码

  1. CREATE TABLE `users` (
  2.   `id` int(10) unsigned NOT NULL auto_increment,
  3.   `username` varchar(25) default NULL,
  4.   `password` varchar(20) default NULL,
  5.   `first_name` varchar(15) default NULL,
  6.   `last_name` varchar(20) default NULL,
  7.   `date_create` time default NULL,
  8.   PRIMARY KEY  (`id`)
  9. ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gb2312;

下面是一个UsersDAO.JAVA 在这个里面有 public Users findById(java.lang.Integer id) 方法,
我的另外一个程序要调用这个类的fingById方法,
要传递一个Integer对象,怎么传递这个对象进来呢
用如下代码这里我传递i的值是2,用到了
Integer的一个构造方法
---------------------------------

public Integer(int value)
构造一个新分配的 Integer 对象,它表示指定的 int 值。

参数:
value - Integer 对象表示的值。

---------------------------------

  1.         UsersDAO user = new UsersDAO();
  2.         Integer i = new Integer(2);
  3.         Users us  =  user.findById(i);   
  4.         System.out.print(us.getUsername());
  5.         System.out.println(us.getPassword());

UsersDAO.JAVA 文件代码如下:

  1. package com.yourcompany.hibernate;
  2. import java.util.Date;
  3. import java.util.List;
  4. import org.apache.commons.logging.Log;
  5. import org.apache.commons.logging.LogFactory;
  6. import org.hibernate.LockMode;
  7. import org.hibernate.Query;
  8. import org.hibernate.criterion.Example;
  9. /**
  10.  * A data access object (DAO) providing persistence and search support for Users
  11.  * entities. Transaction control of the save(), update() and delete() operations
  12.  * can directly support Spring container-managed transactions or they can be
  13.  * augmented to handle user-managed Spring transactions. Each of these methods
  14.  * provides additional information for how to configure it for the desired type
  15.  * of transaction control.
  16.  * 
  17.  * @see com.yourcompany.hibernate.Users
  18.  * @author MyEclipse Persistence Tools
  19.  */
  20. public class UsersDAO extends BaseHibernateDAO {
  21.     private static final Log log = LogFactory.getLog(UsersDAO.class);
  22.     // property constants
  23.     public static final String USERNAME = "username";
  24.     public static final String PASSWORD = "password";
  25.     public static final String FIRST_NAME = "firstName";
  26.     public static final String LAST_NAME = "lastName";
  27.     public void save(Users transientInstance) {
  28.         log.debug("saving Users instance");
  29.         try {
  30.             getSession().save(transientInstance);
  31.             log.debug("save successful");
  32.         } catch (RuntimeException re) {
  33.             log.error("save failed", re);
  34.             throw re;
  35.         }
  36.     }
  37.     public void delete(Users persistentInstance) {
  38.         log.debug("deleting Users instance");
  39.         try {
  40.             getSession().delete(persistentInstance);
  41.             log.debug("delete successful");
  42.         } catch (RuntimeException re) {
  43.             log.error("delete failed", re);
  44.             throw re;
  45.         }
  46.     }
  47.     public Users findById(java.lang.Integer id) {
  48.         log.debug("getting Users instance with id: " + id);
  49.         try {
  50.             Users instance = (Users) getSession().get(
  51.                     "com.yourcompany.hibernate.Users", id);
  52.             return instance;
  53.         } catch (RuntimeException re) {
  54.             log.error("get failed", re);
  55.             throw re;
  56.         }
  57.     }
  58.     public List findByExample(Users instance) {
  59.         log.debug("finding Users instance by example");
  60.         try {
  61.             List results = getSession().createCriteria(
  62.                     "com.yourcompany.hibernate.Users").add(
  63.                     Example.create(instance)).list();
  64.             log.debug("find by example successful, result size: "
  65.                     + results.size());
  66.             return results;
  67.         } catch (RuntimeException re) {
  68.             log.error("find by example failed", re);
  69.             throw re;
  70.         }
  71.     }
  72.     public List findByProperty(String propertyName, Object value) {
  73.         log.debug("finding Users instance with property: " + propertyName
  74.                 + ", value: " + value);
  75.         try {
  76.             String queryString = "from Users as model where model."
  77.                     + propertyName + "= ?";
  78.             Query queryObject = getSession().createQuery(queryString);
  79.             queryObject.setParameter(0, value);
  80.             return queryObject.list();
  81.         } catch (RuntimeException re) {
  82.             log.error("find by property name failed", re);
  83.             throw re;
  84.         }
  85.     }
  86.     public List findByUsername(Object username) {
  87.         return findByProperty(USERNAME, username);
  88.     }
  89.     public List findByPassword(Object password) {
  90.         return findByProperty(PASSWORD, password);
  91.     }
  92.     public List findByFirstName(Object firstName) {
  93.         return findByProperty(FIRST_NAME, firstName);
  94.     }
  95.     public List findByLastName(Object lastName) {
  96.         return findByProperty(LAST_NAME, lastName);
  97.     }
  98.     public List findAll() {
  99.         log.debug("finding all Users instances");
  100.         try {
  101.             String queryString = "from Users";
  102.             Query queryObject = getSession().createQuery(queryString);
  103.             return queryObject.list();
  104.         } catch (RuntimeException re) {
  105.             log.error("find all failed", re);
  106.             throw re;
  107.         }
  108.     }
  109.     public Users merge(Users detachedInstance) {
  110.         log.debug("merging Users instance");
  111.         try {
  112.             Users result = (Users) getSession().merge(detachedInstance);
  113.             log.debug("merge successful");
  114.             return result;
  115.         } catch (RuntimeException re) {
  116.             log.error("merge failed", re);
  117.             throw re;
  118.         }
  119.     }
  120.     public void attachDirty(Users instance) {
  121.         log.debug("attaching dirty Users instance");
  122.         try {
  123.             getSession().saveOrUpdate(instance);
  124.             log.debug("attach successful");
  125.         } catch (RuntimeException re) {
  126.             log.error("attach failed", re);
  127.             throw re;
  128.         }
  129.     }
  130.     public void attachClean(Users instance) {
  131.         log.debug("attaching clean Users instance");
  132.         try {
  133.             getSession().lock(instance, LockMode.NONE);
  134.             log.debug("attach successful");
  135.         } catch (RuntimeException re) {
  136.             log.error("attach failed", re);
  137.             throw re;
  138.         }
  139.     }
  140. }

抱歉!评论已关闭.