下面程序運行輸出System.out.println(words);後報錯.錯誤是:[Ljava.lang.String;@11b886b7,請問怎麼解決?在線等
package com.db.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.db.action.ConnDB;
public class JquerySearch extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response. setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
String sql = "select * from info";
conn = ConnDB.getConn();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
StringBuffer sb=new StringBuffer();
while(rs.next()){
String[] www={'"'+rs.getString("Names")+'"'};
if(www!=null &&www.length>0){
for(String s:www){
sb.append(s+",");
}
}
}
if(sb.length()>0)
sb.deleteCharAt(sb.length()-1);
String []words={sb.toString()};
if(request.getParameter("SearchText") != null) {
String key = request.getParameter("SearchText");
if(key.length() != 0){
String json="[";
for(int i = 0; i < words.length; i++) {
if(words[i].startsWith(key)){
json += "\""+ words[i] + "\"" + ",";
}
}
json = json.substring(0,json.length()-1>0?json.length()-1:1);
json += "]";
System.out.println("json:" + json);
System.out.println(words);
out.println(json);
}
}
}catch(Exception e){
out.println(e);
}
}
}
words是數組,而且只有一個元素,你看一下上面的定義
你把所有結果放在了一個字符串裡,words定義的時候一次性放進去
for(int i = 0; i < words.length; i++) {
if(words[i].startsWith(key)){
json += "\""+ words[i] + "\"" + ",";
}