为什么我在SQL查询中收到非法字符错误?

时间:2015-05-13 18:41:49

标签: java sql jdbc

错误表明非法字符的索引是6,但第6个字符是SELECT

之后的空格
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://sql3.freemysqlhosting.net:3306/";

static final String USER = "****";
static final String PASS = "****";

public JSONArray getLocation(ArrayList postParameters)
{
    Connection conn = null;
    HttpEntity httpEntity = null;
    try{

        Class.forName(JDBC_DRIVER);

        System.out.println("Connecting ");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("Connection successful.");

        String sql = "SELECT * FROM locations WHERE locTag LIKE '%"+postParameters+"%'";

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(sql);
        httpPost.setEntity(new UrlEncodedFormEntity(postParameters,"UTF-8"));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();

我正在接受

java.lang.IllegalArgumentException: Illegal character in path at index 6: select * FROM locations WHERE locTag = '%[locTag=fast]%'

我也尝试过编码,但没有运气。

1 个答案:

答案 0 :(得分:0)

您正在尝试将SQL查询作为HTTP请求执行。这毫无意义。 HttpPost constructor需要一个URI,一个select语句一个URI。

您需要使用JDBC来执行查询,或者需要GET或POST URI。我不能从你的代码中告诉你实际上你想要做什么。