是这样的了: 
String   sql= "select   *   from   BasicInfo   b,PersonalInfo   p   where   (b.cStudentNo=p.cStudentNo)   and   b.?   =? "; 
PreparedStatement   stat   =con.prepareStatement(sql); 
stat.setString(1, "cStudentNo ");   //它不是常量,方便看而矣 
stat.setString(2,str2); 
ResultSet   rs=stat.executeQuery(); 
我查询条件是要这样写的,不过这样写就是要出错: 
      "[.......SQL   Server]第一行 "@P1附近有语法错误 "   怎么回事了?没有P1的什么了, 
      另外,我把第一个 "? "改为 "cStudentNo "这样查询是正确的了.   
              还有b.?   b是不能去掉的了,不然会出现行名不确定的错误。 
  要实现这个查询应该怎么做什么实现了?急~~~用不带参数的也不行, 
这样可以: 
Statement   stat=con.createStatement(); 
String   sql= "select   *   from   BasicInfo   b,PersonalInfo   p   where   (b.cStudentNo=p.cStudentNo)   and   b.cStudentNo   = "+ " ' "+str2+ " ' "; 
ResultSet   rs=stat.executeQuery(sql); 
  而这样也不行: 
    条件改为这样就会出错: 
String   sql= "select   *   from   BasicInfo   b,PersonalInfo   p   where   (b.cStudentNo=p.cStudentNo)   and   b. "+ " ' "+str1+ " '= "+ " ' "+str2+ " ' "; 
          错误为: 
"[.......]第1行 'cStudentNo '   附近有语法错误 "!  还有,我通过以下代码保证str1的值是 "cStudentNo " 
if(str1.equals( "cStudentNo ")){ 
      JOptionPane.showMessageDialog(null, "正確 ", "注意! ",2); 
}else{ 
      JOptionPane.showMessageDialog(null, "錯誤 ", "注意! ",2); 

结果是 "正確 "了.

解决方案 »

  1.   

    String       sql=   "select       *       from       BasicInfo       b,PersonalInfo       p       where       (b.cStudentNo=p.cStudentNo)       and       b.cStudentNo       =   "+   "   '   "+str2+   "   '   ";
    中的  "+   " 去掉
      

  2.   

    b.?
    把这个问号需要的东西当参数传过来,然后拼出sql
    你那么写肯定是不行的了