我的数据库中有两个表 (在附件里)
我拼接sql的代码为
public static String getSQL(String sValue){
String sSQL=new String();
sSQL="select sPos from SearchAbstract where sWord='"+sValue+"'";
System.out.println(sSQL);
Connection con = null;
Statement st = null;
ResultSet rs = null;
String sTemp=new String();
try{
con=Dbcon.getConnection();
st=con.createStatement();
rs=st.executeQuery(sSQL);
while(rs.next()){
sSQL=rs.getString("sPos");
System.out.println(sSQL);
}
}catch(SQLException e){
System.out.println(e);
}
for(int i=0;i<sSQL.length();i++){
if(sSQL.indexOf("")>0){
sTemp=sTemp+""+sSQL.substring(sSQL.indexOf("<")+1, sSQL.indexOf(""))+",";
sSQL=sSQL.substring(sSQL.indexOf(">")+1);
}
}
sTemp=sTemp.substring(0, sTemp.length()-1);
System.out.println(sTemp);
sSQL="select * from baseArticle where sId in("+sTemp+")";
return sSQL;
}
运行的时候出现错误
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.String.substring(String.java:1444)
search.Searchservice.getSQL(Searchservice.java:35)
search.Searchservlet.doGet(Searchservlet.java:31)
search.Searchservlet.doPost(Searchservlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
其中 Searchservice.java:35行 是用红色标注的行