JDBC 告诉无法找到列
JDBC tells unable to find column
我想在单独的主测试文件中测试我的一些 JPA 算法。
我打算做什么:
我将 SQL 语句从 JAVA 发送到 MySQL5.1 服务器,并希望将结果返回到 ArrayList 中。作为错误信息,我得到列 "zip" 未找到
java.sql.SQLException: Column 'zip' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at de.telekom.umkreissuche.SQLConnect.exportSQLResult(SQLConnect.java:82)
at de.telekom.umkreissuche.SQLConnect.sendSQLStatement(SQLConnect.java:66)
at de.telekom.umkreissuche.Testing.main(Testing.java:48)
Exception in thread "main" java.lang.NullPointerException
at de.telekom.umkreissuche.Testing.main(Testing.java:59)
,但在我的 SQL 语句中,我没有选择 "zip"。
SQL-文件:
SELECT
d.`Web-Adresse` "Link zur Ausschreibung im Intranet"
, d.`Ausschreibungsnr.`
, d.`Stellenbezeichnung`
, d.`Ausschreibende Einheit`
, d.`Standort`
, d.`PLZ RASt` "*PLZ"
, DATE_FORMAT(d.`Ausschreibungsende`, "%d.%m.%Y") "Ausschreibungsende"
, d.`Zuordnung Besoldungsgruppen` "*Besoldungsgruppe"
, d.`Bewertung lt. Ausschreibung` "*Vergütungsgruppe"
FROM wicirclesearch.`zc_data` d
WHERE `PLZ RASt` in (:plz)
-- WHERE `PLZ RASt` in ('64289')
;
我在 Java 中所做的主要内容:
xml = new XMLConfigProperties("C:\Users\A22548675\workspace\umkreissuche-war\WebContent\data\config\dbMySQL.xml");
activeFilepath = new File("C:\Users\A22548675\workspace\umkreissuche-war\WebContent\data\config\mysql_plz.sql");
Map<String, String> token = new HashMap<String, String>();
token.put(":plz", "'64289'");
try {
// create a temporary SQL file
setTmpFileForReplacer(activeFilepath.getAbsolutePath());
// set search tokens into SQL file
fileTokenReplacer(activeFilepath.getAbsolutePath(), token);
// start sql connect with XML config params
con = new SQLConnect(xml.getProperty("jdbc.url"), xml.getProperty("jdbc.user"), xml.getProperty("jdbc.password"), xml.getProperty("jdbc.driver"));
// use and send modified SQL file as SQL query
testResultSQL = con.sendSQLStatement(activeFilepath.getAbsolutePath());
System.out.println(testResultSQL.toString());
// close SQL connection
con.getConnection().close();
} catch (Exception e) {e.printStackTrace();}
finally{
// recover SQL file from temporary one (temporary file delete)
resetFileFromTmpFile(activeFilepath.getAbsolutePath());
}
// if SQL query result empty: aboard the routine and give message to webspace
if(testResultSQL.isEmpty() || testResultSQL == null){
System.out.println("Result is leer");
}
else{
int i = 0;
for(ArrayList<String> liste: testResultSQL){
System.out.print("Row "+i+": [");
for(String e: liste){
System.out.print(e+", ");
}
System.out.println("]");
i++;
}
}
我的问题是:
为什么我在不存在选定列的情况下得到这些错误代码?
也许这是一个错误?或者我有语法错误?
我的错是什么?
这真的是我的。我没有实现我的导出SQL结果函数,不是我修改的
函数如下:
/**
* Export the SQL results
* @param resultSet {@link java.sql.ResultSet}
* @return {@link ArrayList<ArrayList>} with {@link java.lang.String}
* @throws SQLException
*/
private ArrayList<ArrayList<String>> exportSQLResult(ResultSet resultSet) throws SQLException{
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> tmpLine;
resultSet.beforeFirst();
while(resultSet.next()){
tmpLine = new ArrayList<String>();
tmpLine.add(resultSet.getString("zip"));
tmpLine.add(resultSet.getString("distance"));
results.add(tmpLine);
}
return results;
}
我如何解决我的问题:
我想改变我的方法。我放弃了直接选择,只想使用迭代。
谢谢你的帮助,拯救了我的一天。
问题解决了,是的
这是我的错,我更改了问题函数:(见请求)
/**
* Export the SQL results
* @param resultSet {@link java.sql.ResultSet}
* @return {@link ArrayList<ArrayList>} with {@link java.lang.String}
* @throws SQLException
*/
private ArrayList<ArrayList<String>> exportSQLResult(ResultSet resultSet) throws SQLException{
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> tmpLine;
resultSet.beforeFirst();
ResultSetMetaData md = resultSet.getMetaData();
int cols = md.getColumnCount();
tmpLine = new ArrayList<String>();
for(int i=1; i<=cols; ++i){
tmpLine.add(md.getColumnLabel(i));
}
results.add(tmpLine);
while(resultSet.next()){
tmpLine = new ArrayList<String>();
for(int i=1; i<=cols; ++i){
tmpLine.add(resultSet.getString(i));
}
results.add(tmpLine);
}
return results;
}
我想在单独的主测试文件中测试我的一些 JPA 算法。
我打算做什么: 我将 SQL 语句从 JAVA 发送到 MySQL5.1 服务器,并希望将结果返回到 ArrayList 中。作为错误信息,我得到列 "zip" 未找到
java.sql.SQLException: Column 'zip' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at de.telekom.umkreissuche.SQLConnect.exportSQLResult(SQLConnect.java:82)
at de.telekom.umkreissuche.SQLConnect.sendSQLStatement(SQLConnect.java:66)
at de.telekom.umkreissuche.Testing.main(Testing.java:48)
Exception in thread "main" java.lang.NullPointerException
at de.telekom.umkreissuche.Testing.main(Testing.java:59)
,但在我的 SQL 语句中,我没有选择 "zip"。
SQL-文件:
SELECT
d.`Web-Adresse` "Link zur Ausschreibung im Intranet"
, d.`Ausschreibungsnr.`
, d.`Stellenbezeichnung`
, d.`Ausschreibende Einheit`
, d.`Standort`
, d.`PLZ RASt` "*PLZ"
, DATE_FORMAT(d.`Ausschreibungsende`, "%d.%m.%Y") "Ausschreibungsende"
, d.`Zuordnung Besoldungsgruppen` "*Besoldungsgruppe"
, d.`Bewertung lt. Ausschreibung` "*Vergütungsgruppe"
FROM wicirclesearch.`zc_data` d
WHERE `PLZ RASt` in (:plz)
-- WHERE `PLZ RASt` in ('64289')
;
我在 Java 中所做的主要内容:
xml = new XMLConfigProperties("C:\Users\A22548675\workspace\umkreissuche-war\WebContent\data\config\dbMySQL.xml");
activeFilepath = new File("C:\Users\A22548675\workspace\umkreissuche-war\WebContent\data\config\mysql_plz.sql");
Map<String, String> token = new HashMap<String, String>();
token.put(":plz", "'64289'");
try {
// create a temporary SQL file
setTmpFileForReplacer(activeFilepath.getAbsolutePath());
// set search tokens into SQL file
fileTokenReplacer(activeFilepath.getAbsolutePath(), token);
// start sql connect with XML config params
con = new SQLConnect(xml.getProperty("jdbc.url"), xml.getProperty("jdbc.user"), xml.getProperty("jdbc.password"), xml.getProperty("jdbc.driver"));
// use and send modified SQL file as SQL query
testResultSQL = con.sendSQLStatement(activeFilepath.getAbsolutePath());
System.out.println(testResultSQL.toString());
// close SQL connection
con.getConnection().close();
} catch (Exception e) {e.printStackTrace();}
finally{
// recover SQL file from temporary one (temporary file delete)
resetFileFromTmpFile(activeFilepath.getAbsolutePath());
}
// if SQL query result empty: aboard the routine and give message to webspace
if(testResultSQL.isEmpty() || testResultSQL == null){
System.out.println("Result is leer");
}
else{
int i = 0;
for(ArrayList<String> liste: testResultSQL){
System.out.print("Row "+i+": [");
for(String e: liste){
System.out.print(e+", ");
}
System.out.println("]");
i++;
}
}
我的问题是: 为什么我在不存在选定列的情况下得到这些错误代码? 也许这是一个错误?或者我有语法错误?
我的错是什么? 这真的是我的。我没有实现我的导出SQL结果函数,不是我修改的
函数如下:
/**
* Export the SQL results
* @param resultSet {@link java.sql.ResultSet}
* @return {@link ArrayList<ArrayList>} with {@link java.lang.String}
* @throws SQLException
*/
private ArrayList<ArrayList<String>> exportSQLResult(ResultSet resultSet) throws SQLException{
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> tmpLine;
resultSet.beforeFirst();
while(resultSet.next()){
tmpLine = new ArrayList<String>();
tmpLine.add(resultSet.getString("zip"));
tmpLine.add(resultSet.getString("distance"));
results.add(tmpLine);
}
return results;
}
我如何解决我的问题: 我想改变我的方法。我放弃了直接选择,只想使用迭代。
谢谢你的帮助,拯救了我的一天。
问题解决了,是的
这是我的错,我更改了问题函数:(见请求)
/**
* Export the SQL results
* @param resultSet {@link java.sql.ResultSet}
* @return {@link ArrayList<ArrayList>} with {@link java.lang.String}
* @throws SQLException
*/
private ArrayList<ArrayList<String>> exportSQLResult(ResultSet resultSet) throws SQLException{
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
ArrayList<String> tmpLine;
resultSet.beforeFirst();
ResultSetMetaData md = resultSet.getMetaData();
int cols = md.getColumnCount();
tmpLine = new ArrayList<String>();
for(int i=1; i<=cols; ++i){
tmpLine.add(md.getColumnLabel(i));
}
results.add(tmpLine);
while(resultSet.next()){
tmpLine = new ArrayList<String>();
for(int i=1; i<=cols; ++i){
tmpLine.add(resultSet.getString(i));
}
results.add(tmpLine);
}
return results;
}