[英]how to execute 2 queries together?
我在一个项目上工作,我想插入2个不同的表中,所以我写了这2个查询(query1,query2),当我只用一个查询运行程序时,我没有任何异常,但是当我一起执行时,一堆异常,我使用了prepareStatement execute(),但没有起作用注意:我不是非常有经验,请轻松解释
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用户缺少特权或找不到对象:SNTV在net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:509)在sntv.MainMenuController.loadBusesToDB(MainMenuController。 sntv.MainMenuController.initialize(MainMenuController.java:277)处的java:135)javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)处的javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)处
正如@uaraven指出的那样,您在第二个查询中遇到有关列名的语法错误。 在SQL中,当在任何子句( SELECT
, FROM
, JOIN
,或)中引用时,任何名称(包括表,列,存储过程,函数等)中带有空格或特殊字符/符号或名称与保留字匹配的标识符都必须转义。 WHERE
, GROUP BY
, HAVING
, ORDER BY
)。
现在,不同的RDBMS的处理方式也不同。 请考虑以下内容,具体取决于您的数据库。 SQLite可能是唯一包含所有内容的RDBMS。
双引号 (ANSI-SQL标准)( Oracle,DB2,Postgres,RedShift,Teradata,SQLite,并为某些字符增加了大写规则; SQL Server / MySQL确实支持模式更改)
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 (?, ?, ?, ?)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.