データベースの 1 行のすべての情報を返そうとしていますが、構文エラーが発生し続け、その理由がわかりません。必要に応じて、データベース内のすべての情報を問題なく返すことができますが、1 行だけを取得しようとすると問題が発生し続けます。また、スペースが含まれている可能性のあるテーブル行をどのようにクエリしますか? 関連するコードは次のとおりです
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