在Java中查询MySQL数据库时出现语法错误

时间:2013-07-22 16:47:34

标签: java mysql eclipse

我正在尝试返回数据库中一行的所有信息,但我一直收到语法错误,我不知道为什么。如果我想,我可以返回数据库中的所有信息没有问题,但我一直遇到试图获得一行的问题。另外,如何查询可能包含空格的表行?这是相关代码

    package core;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.HashMap;

public class DBConnection {

    private Connection con;
    private static Statement statement;
    private static ResultSet resultSet;
    public  static DBConnection connection;
    private static ResultSetMetaData meta;
    private static HashMap<String,Party> map;

    public static Party party;

    private DBConnection()
    {
        try 
        {
            map = new HashMap<String,Party>();
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://blah/reddb", "blah",
                    "blah");
            statement = con.createStatement();
        }
        catch (Exception e)
        {
            System.out.print("Error: "+e);
        }
    }
            //do not worry about this method it works fine, I put it here so prove that the db connection is not the problem. When this method is run I can get all the contents. See readOneParty() for issue
    public static void readAllData() //this method works fine
    {
        if(connection == null)
        {
            connection = new DBConnection();
        }
        try
        {
            map.clear();
            String query = "(SELECT * FROM PureServlet)";
            resultSet = statement.executeQuery(query);
            meta = resultSet.getMetaData();
            String columnName, value, partyName;
            while(resultSet.next())
            {
                partyName = resultSet.getString("PARTY_NAME");
                map.put(partyName, new Party()); //this is the map that keeps track of all parties
                party = map.get(partyName);
                for(int j=1;j<=meta.getColumnCount();j++) //necessary to start at j=1 because of MySQL index starting at 1
                {
                    columnName = meta.getColumnLabel(j);
                    value = resultSet.getString(columnName);
                    party.getPartyInfo().put(columnName, value); //this is the hashmap within the party that keeps 
                    //track of the individual values. The column Name = label, value is the value
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
            //this is the method where I'm getting an exception
    public static Party readOneParty(String partyName)
    {
        if(connection==null)
        {
            connection = new DBConnection();
        }
        try
        {
            String query = "SELECT * FROM PureServlet WHERE PARTY_NAME="+partyName;
            resultSet = statement.executeQuery(query); //exception occurs here
            meta = resultSet.getMetaData();
            String columnName, value;
            Party party = new Party();
            for(int j=1;j<=meta.getColumnCount();j++) //necessary to start at j=1 because of MySQL index starting at 1
            {
                columnName = meta.getColumnLabel(j);
                value = resultSet.getString(columnName);
                party.getPartyInfo().put(columnName, value); //this is the hashmap within the party that keeps 
                //track of the individual values. The column Name = label, value is the value
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return party;
    }
    public static HashMap<String,Party> getPartyCollection()
    {
        return map;
    }
}

这是我在尝试返回没有空格的派对时遇到的错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'VSN' in 'where clause'

这是当聚会中有空格时我得到的错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' GOURMET PATISSERIE' at line 1

2 个答案:

答案 0 :(得分:1)

从一个天真的角度来看,你缺少字符串周围的引号,所以改变:

String query = "SELECT * FROM PureServlet WHERE PARTY_NAME="+partyName;

到此:

String query = "SELECT * FROM PureServlet WHERE PARTY_NAME = '" + partyName + "'";

如果没有此更改,您可能会获得一个类似于以下内容的查询字符串:

SELECT * FROM PureServlet WHERE PARTY_NAME = some value

这将是无效的SQL - 您需要:

SELECT * FROM PureServlet WHERE PARTY_NAME = 'some value'

但是,最好将PreparedStatement与占位符一起使用,让JDBC提供引号,并转义类似O'Brien的字符串值的引号:

PreparedStatement ps = con.prepareStatement("SELECT * FROM PureServlet WHERE PARTY_NAME=?");
ps.setString(1, partyName);
resultSet = ps.executeQuery();

答案 1 :(得分:-2)

看起来聚会名称需要前面的空格;否则,这一切看起来都像一个大牌。

尝试:

String query = "SELECT * FROM PureServlet WHERE PARTY_NAME = " + partyName;