不知道大家在拼接字符串的时候是怎么做的?是不是采用,或:?
这样做有的时候不很安全,因为你不能确保你传入的字符串中没有这几个字符,那怎么做能保证万无一失呢?
ASCII码为0x01,0x02的字符是键盘所不能输入的(这两个字符称作隐藏字符,对应的8进制是001,002,打印出来的视觉效果与空格相同,但是相比空格的好处就是这两个字符是从键盘无法输入的,所以避免了如果数据中本身带空格,会错误的分割字符串的尴尬),因为用这个能保证万无一失。
- public String GetEnterpriseInfo(String code) {
- Connection cn = null;
- PreparedStatement stm = null;
- ResultSet rs = null;
- String s = “”;
- byte b1[] = {
0x02}; - byte b2[] = {
0x01}; - String str1 = new String(b1);
- String str2 = new String(b2);
- try {
- cn = DBUtil.getConn();
- String sql = “select station_id,station_desc from t_cfg_station_info where area_id like ‘%”+code+“%'”;
- stm = cn.prepareStatement(sql);
- rs = stm.executeQuery();
- while(rs.next()){
- s += rs.getString(1)+str1+rs.getString(2)+str2;
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- DBUtil.close(rs, stm, cn);
- }
- return s;
- }
结果:

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220033.html原文链接:https://javaforall.net
