gpt4 book ai didi

java - 如何一起执行2个查询?

转载 作者:行者123 更新时间:2023-12-02 01:22:55 24 4
gpt4 key购买 nike

我正在开发一个项目,我想插入到两个不同的表中,所以我编写了这两个查询(查询1,查询2),当我仅使用一个查询运行程序时,我没有得到任何异常,但是在执行时在一起我有很多异常,我使用了preparedStatementexecute()并且没有工作注意:我经验不足,请简单解释一下

private void loadBusesToDB() throws SQLException{
Connection connection = connect();

String query = "INSERT INTO Bus (nomLigne, Marque, Matricule, Capacite)"
+ "VALUES (?, ?, ?, ?)";

String query2 = "INSERT INTO Lignes (nomLigne, Sntv Depart, SNTV Arrive, prix)"
+ "VALUES (?, ?, ?, ?)";

PreparedStatement ps = null;
PreparedStatement ps2 = null;
try {

ps = connection.prepareStatement(query);
ps2 = connection.prepareStatement(query2);

for(Bus bus : Bus.buses){
ps.setString(1, bus.getNomLigne());
ps.setString(2, bus.getMarque());
ps.setString(3, bus.getMatricule());
ps.setInt(4, bus.getCapacite());
ps.addBatch(); // THE INSERT HAPPENS HERE
}
ps.executeBatch();


for(Lignes ligne : Lignes.lignes){
ps2.setString(1, ligne.getNomLigne());
ps2.setString(2, ligne.getDepart());
ps2.setString(3, ligne.getArrive());
ps2.setFloat(4, ligne.getPrix());
ps2.addBatch(); // THE INSERT HAPPENS HERE
}
ps2.executeBatch();

} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("ERROR HERE");
throw ex;
}finally{
ps.close();
ps2.close();
connection.close();
}
}

错误:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.2 user lacks privilege or object not found: SNTV at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:509) at sntv.MainMenuController.loadBusesToDB(MainMenuController.java:135) at sntv.MainMenuController.initialize(MainMenuController.java:277) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)

最佳答案

正如 @uaraven 指出的那样,第二个查询中的列名称存在语法错误。在 SQL 中,任何名称中带有空格或特殊字符/符号或名称与保留字匹配的标识符(包括表、列、存储过程、函数等)在任何子句(SELECTFROM加入WHEREGROUP BYHAVINGORDER BY)。

现在,不同的 RDBMS 以不同的方式处理此类转义。根据您的数据库考虑以下事项。 SQLite 可能是唯一包含所有功能的 RDBMS。

双引号(ANSI-SQL 标准)(Oracle、DB2、Postgres、RedShift、Teradata、SQLite,其中添加了一些大小写规则;SQL Server/MySQL 确实支持 with 模式更改)

String query2 = "INSERT INTO Lignes (nomLigne, \"SNTV DEPART\", \"SNTV ARRIVE\", prix)"
+ " VALUES (?, ?, ?, ?)";

方括号(SQL Server、Sybase、SQLite、MS Access)

String query2 = "INSERT INTO Lignes (nomLigne, [Sntv Depart], [SNTV Arrive], prix)"
+ " VALUES (?, ?, ?, ?)";

反引号(MySQL、MariaDB、Google BigQuery 标准 SQL、SQLite、MS Access)

String query2 = "INSERT INTO Lignes (nomLigne, `Sntv Depart`, `SNTV Arrive`, prix)"
+ " VALUES (?, ?, ?, ?)";

关于java - 如何一起执行2个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378119/

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