日期:2014-05-20  浏览次数:20879 次

关于PreparedStatement的问题 小弟求帮助
/**
* 返回分销商类型
* @return
*/
public List<ClientType> getClientTypeList(){
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
List<ClientType> ctl = new ArrayList<ClientType>();
ClientType ct = null;
StringBuffer sb = new StringBuffer();
// String clientCategoryCode = "A";
String sql = "select * from t_data_dictionary where category_code = 'A' ";
try{
conn = DbUtil.getConnection();
// sb.append("select * from t_data_dictionary where category_code = ? ");
pstmt = conn.prepareStatement(sql);
// pstmt = conn.prepareStatement(sb.toString());
// pstmt.setString(1, clientCategoryCode);
// pstmt.setString(1,"A");
rs = pstmt.executeQuery();
while(rs.next()){
ct = new ClientType();
ct.setId(rs.getInt("id"));
ct.setName(rs.getString("name"));
ctl.add(ct);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
DbUtil.close(conn);
}

return ctl;
}

现在运行正常,rs.size() = 4 ; 但是如果放开注释,就是用PreparedStatement就会出现rs为空的结果 求帮助!小弟新学JAVA,碰到这样的问题感觉很茫然。。。。。。。

数据库建模代码:
/*==============================================================*/
/* Table: T_DATE_DICTIONARY */
/*==============================================================*/
create table T_DATE_DICTIONARY (
  ID number(5) not null,
  CATEGORY_CODE char(3) not null,
  NAME varchar2(20) not null,
  constraint PK_T_DATE_DICTIONARY primary key (ID)
);

数据库初始化代码:
--初始化分销商类型--

insert into t_date_dictionary (id,category_code,name) values (10000,'A','一级分销商');
insert into t_date_dictionary (id,category_code,name) values (10001,'A','二级分销商');
insert into t_date_dictionary (id,category_code,name) values (10002,'A','三级分销商');
insert into t_date_dictionary (id,category_code,name) values (10003,'A','总部');

--初始化物料类型--

insert into t_date_dictionary (id, category_code,name) values (20000,'B','医疗器械');
insert into t_date_dictionary (id, category_code,name) values (20001,'B','中成药');
insert into t_date_dictionary (id, category_code,name) values (20002,'B','西药');

--初始化物料计量单位--

insert into t_date_dictionary (id, category_code,name) values (30000,'C','盒');
insert into t_date_dictionary (id, category_code,name) values (30001,'C','片');
insert into t_date_dictionary (id, category_code,name) values (30002,'C','箱');

--初始化终端客户类型--

insert into t_date_dictionary (id, category_code,name) values (40000,'D','甲级医院');
insert into t_date_dictionary (id, category_code,name) values (40001,'D','乙级医院');
insert into t_date_dictionary (id, category_code,name) values (40002,'D','丙级医院');
insert into t_date_dictionary (id, category_code,name) values (40003,'D','药店');
insert into t_date_dictionary (id, category_code,name) values (40004,'D','其他');


------解决方案--------------------
你的CATEGORY_CODE是char(3)类型,估计就是这个原因了,因为'A'占一个字符,char会自动填充2个空格。。
你改为varchar(3)看看行不行。。
或者String clientCategoryCode = "A ";注意A后面加2个空格,