gpt4 book ai didi

java - MySQL SELECT 在 JDBC 中不起作用

转载 作者:行者123 更新时间:2023-11-29 08:19:42 24 4
gpt4 key购买 nike

我有这个 MySQL 查询

SELECT 
kompetence.kompetence_odlisujici_subcategories.nazev as odlisujici_nazev,
tolerovana,
zadana,
aktualni
FROM kompetence_odlisujici, kompetence.kompetence_odlisujici_subcategories
WHERE kompetence_odlisujici.os_cislo_zamestnanec = ?
AND tolerovana <> 0
AND zadana <> 0
AND aktualni <> 0
AND year = ?
AND IPR.kompetence_odlisujici.active = 1
AND kompetence.kompetence_odlisujici_subcategories.id = PR.kompetence_odlisujici.id_odlisujici_subcategory

如果我在 MySQL Workbench 中尝试它,它工作正常,但是当我尝试在我的 java 代码中使用它时,我收到此错误

SQLException :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 'WHERE kompetence_odlisujici.os_cislo_zamestnanec = '123456' AND tolerovana <> 0 ' at line 1

我做错了什么?

感谢您的帮助。

编辑 java 代码

DataSourcePool dspService = sling.getService(DataSourcePool.class);
DataSource dsMySql = (DataSource) dspService.getDataSource("myConnection");
PreparedStatement preparedStatement = null;
if(dsMySql != null){
String sqlQuery = "SELECT kompetence.kompetence_odlisujici_subcategories.nazev as odlisujici_nazev, tolerovana, zadana, aktualni"+
"FROM IPR.kompetence_odlisujici, kompetence.kompetence_odlisujici_subcategories "+
"WHERE IPR.kompetence_odlisujici.os_cislo_zamestnanec = ? AND tolerovana <> 0 AND zadana <> 0 AND aktualni <> 0 AND year = ? AND IPR.kompetence_odlisujici.active = 1"+
"AND kompetence.kompetence_odlisujici_subcategories.id = IPR.kompetence_odlisujici.id_odlisujici_subcategory";
Connection mySqlConnection = mySqlConnection = dsMySql.getConnection();
preparedStatement = mySqlConnection.prepareStatement(sqlQuery);
String workingUser = this.getWorkingUser();
int year = Calendar.getInstance().get(Calendar.YEAR);
preparedStatement.setString(1, workingUser);
preparedStatement.setInt(2, year);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.last();
int count = resultSet.getRow();
resultSet.beforeFirst();
/*
parse result set here
*/
}

最佳答案

String sqlQuery = "SELECT kompetence.kompetence_odlisujici_subcategories.nazev as odlisujici_nazev, tolerovana, zadana, aktualni "+
"FROM IPR.kompetence_odlisujici, kompetence.kompetence_odlisujici_subcategories "+
"WHERE IPR.kompetence_odlisujici.os_cislo_zamestnanec = ? AND tolerovana <> 0 AND zadana <> 0 AND aktualni <> 0 AND year = ? AND IPR.kompetence_odlisujici.active = 1 "+
"AND kompetence.kompetence_odlisujici_subcategories.id = IPR.kompetence_odlisujici.id_odlisujici_subcategory";

aktualniFROM 之间没有空格,IPR.kompetence_odlisujici.active = 1AND kompetence.kompetence_odlisujici_subcategories 之间没有空格.id = IPR.kompetence_odlisujici.id_odlisujici_subcategory

我的建议是,您在类中将这些查询作为 private static final 进行。

关于java - MySQL SELECT 在 JDBC 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com