public String getNameById(String sql) throws DataAccessException {
String name="";
log.debug("sql:"+sql);
Session session = null;
try {
session = this.getHibernateTemplate().getSessionFactory().openSession();
Connection conn = session.connection();
Statement stmt = conn.createStatement();
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
name = res.getString(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return name;
}
String hql = "select agent_No from agent_info where agent_id='"
+ agent.getAgentId().substring(0, 3) + "'";agent_NO字段字数据库是number类型。
agent_id在数据库的类型是varchar2.上面的方法调用这样的hql,返回类型是String,可以吗。

解决方案 »

  1.   

    你这样写应该是不行,不过为什么要返回为为String类型呢?
    你可以得到后在转为String类型的。
      

  2.   

    应该是不可以的,但是如果你用javabean映射的number型的,好象从数据库里面出来就成string了
    你直接这样接收应该不行吧.
      

  3.   

    这样取出的agent_No 是BigDecimal的类型,可以用BigDecimal的intvalue方法转化为整型(int)。
      

  4.   

    能否说的更详细点。建表如下
    create table AGENT_INFO
    (
      AGENT_NO          NUMBER(10) not null,
      AGENT_ID          VARCHAR2(144) not null,
      AGENT_NAME        VARCHAR2(100),
      AGENT_LEVEL       NUMBER(4),
    .........................
    alter table AGENT_INFO
      add constraint AGENT_INFO_PK primary key (AGENT_NO)
      using index 
      tablespace PBM_MED_IDX
      pctfree 10
      initrans 2
      maxtrans 255
      storage
      (
        initial 64K
        next 2M
        minextents 1
        maxextents unlimited
        pctincrease 0
      );
    项目使用的技术是hibernate  JSF spring。