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

时间:2021-01-17 09:09:33

The error states that the index of the illegal character is 6 but the 6th character is the space after SELECT

错误表明非法字符的索引是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();

i am then getting

我正在接受

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

I have also tried encoding but no luck.

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

1 个解决方案

#1


You are trying to execute an SQL query as a HTTP request. That makes no sense at all. The HttpPost constructor expects an URI, a select statement is not a URI.

您正在尝试将SQL查询作为HTTP请求执行。这毫无意义。 HttpPost构造函数需要URI,select语句不是URI。

You either need to use JDBC to execute the query, or you need to GET or POST a URI. I can't tell from your code what you are actually trying to do though.

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

#1


You are trying to execute an SQL query as a HTTP request. That makes no sense at all. The HttpPost constructor expects an URI, a select statement is not a URI.

您正在尝试将SQL查询作为HTTP请求执行。这毫无意义。 HttpPost构造函数需要URI,select语句不是URI。

You either need to use JDBC to execute the query, or you need to GET or POST a URI. I can't tell from your code what you are actually trying to do though.

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