程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> javaweb-javaWEB這段程序有點小問題,請高手幫忙指正一下.謝謝

javaweb-javaWEB這段程序有點小問題,請高手幫忙指正一下.謝謝

編輯:編程綜合問答
javaWEB這段程序有點小問題,請高手幫忙指正一下.謝謝

下面程序運行輸出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] + "\"" + ","; 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved