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