0

我正在尝试返回数据库一行的所有信息,但我不断收到语法错误,我不知道为什么。如果我愿意,我可以毫无问题地返回数据库中的所有信息,但我一直在尝试只获取一行时遇到问题。另外,如何查询可能有空格的表行?这是相关代码

    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
4

2 回答 2

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带有占位符的 a,并让 JDBC 提供引号,并为 like 的字符串值转义引号O'Brien

PreparedStatement ps = con.prepareStatement("SELECT * FROM PureServlet WHERE PARTY_NAME=?");
ps.setString(1, partyName);
resultSet = ps.executeQuery();
于 2013-08-09T14:19:11.283 回答
-2

看起来派对名称前面要留一个空格;否则,这一切看起来就像一个大牌。

尝试:

String query = "SELECT * FROM PureServlet WHERE PARTY_NAME = " + partyName;
于 2013-08-09T14:11:05.580 回答