我的一个jsp部分代码,其中id是上一个页面传过来的参数(数据库查询得到的动态数据): String id;
 try 
 {
 id= request.getParameter("id"); 
 }catch(Exception e){
 id="wrong";
 }
%>
<%
String DBURL="jdbc:postgresql://localhost/postgres";
String  user="postgres";
String password="postgres";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String sqlString1=null;
try{
  Class.forName("org.postgresql.Driver").newInstance();
  con=DriverManager.getConnection(DBURL,user,password);
  }catch(ClassNotFoundException e){
    out.println("ClassNotFoundException:"+e.getMessage());
  }
if(id!="wrong"){
sqlString1="select office_id,company_name,line_1_label_address,line_2_label_address,line_3_label_address,line_4_label_address,line_5_label_address,postal_code,telephone,facsimile,email from xinaddress where company_name="+id;我运行时,我从上一个页面进这个页面时总是出现错误,

SQL Exception!:ERROR: syntax error at or near "Shipping" 
但是我如果传的参数是int类型时
上面用
int id;
 try 
 {
 id= Integer.parseInt(request.getParameter("id")); 
 }catch(Exception e){
 id=0;
 }
就没有错,在我的页面里面传的参数是string类型的,是不是也需要加integer.parseInt()类似的东西呢?
怎么加,加什么?
我这个不太懂啊?
大家帮帮忙!

解决方案 »

  1.   

    另外我要接受的参数中存在类似于这样的字符串“Moller-Maersk A/S ”里面存在空格,这个是不是也有影响啊!
      

  2.   

    这样试试呢where company_name = '"+id+"'
      

  3.   

    sqlString1="select office_id,company_name,line_1_label_address,line_2_label_address,line_3_label_address,line_4_label_address,line_5_label_address,postal_code,telephone,facsimile,email from xinaddress where company_name='"+id+"'"; 
      

  4.   

    最后的sql中,where company_name="+id; 应该改为where company_name='"+id+"'"  !!!!,goodLucky
      

  5.   

    if(id!="wrong"){  最好用equals比较where company_name='"+id+"'"
      

  6.   

    看看你数据库里company_name是什么类型的,如果是number类型的第二种说法就对了
      

  7.   

    你的sql语句写错了where company_name="+id,id是字符串时
    应该改成where company_name='"+id+"'" 
      

  8.   

    where company_name="+id; 应该改为where company_name='"+id+"'"
      

  9.   

    字符串必须加“''”,应该改成where company_name='"+id+"'" 
      

  10.   

    我不明白为什么你们不用 preparedStatement ? 拼接字符不累吗?
      

  11.   

    呵,搞不懂,现在Statement这个东东,真的少用为好