Java提交HsqlDB
尝试在java中执行sql插入,但无法使用hsqldb提交:Java提交HsqlDB,java,hsqldb,Java,Hsqldb,尝试在java中执行sql插入,但无法使用hsqldb提交: public class VehiculeDAO extends DAO<Vehicule> { public Vehicule create(int marque, int moteur, int prix, String nom) { System.out.println("\t marque:" + marque +"moteur:"+moteur+"prix:"+prix+"nom:"+
public class VehiculeDAO extends DAO<Vehicule> {
public Vehicule create(int marque, int moteur, int prix, String nom) {
System.out.println("\t marque:" + marque +"moteur:"+moteur+"prix:"+prix+"nom:"+nom);
Vehicule vehicule = new Vehicule();
String query = " INSERT INTO VEHICULE (MARQUE, MOTEUR, PRIX, NOM) VALUES (0,4,100,'test2');";
query += "commit;";
try {
connect.setAutoCommit(true);
ResultSet result = this.connect.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery(" INSERT INTO VEHICULE (MARQUE, MOTEUR, PRIX, NOM) VALUES (0,4,100,'test3')" );
connect.commit();
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
//return;
return vehicule;
}
TestDAO
:
public class TestDAO {
public static void main(String[] args) throws SQLException {
DAO<Vehicule> vehiculedao = DAOFactory.getVehiculeDAO();
Vehicule vehicule = vehiculedao.create( 0 ,5, 100, "test");
// ...
提交命令必须插入类vehicleDAO
,我猜
测试提交
:
公共静态void main(字符串[]args){
试试{
String url = "jdbc:hsqldb:file:hsqldb/database/VEHICULE";
String user = "";
String passwd = "";
Connection conn = DriverManager.getConnection(url, user, passwd);
Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
PreparedStatement prepare = conn.prepareStatement("INSERT INTO VEHICULE (MARQUE, MOTEUR, PRIX, NOM) VALUES (0,4,100,'testcommit')");
prepare.executeUpdate();
conn.commit();
prepare.close();
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}使用statement.executeUpdate()而不是statement.executeQuery()。您正在将自动提交设置为true,并调用
Connection.commit()
在这些情况下是一个错误,因为驱动程序将在语句执行后自动提交。您的问题可能在其他地方。请创建一个实际演示问题的示例。我同意,但没有找到问题所在,下面是packageprivate HsqldbConnection()的最低演示{try{connect=DriverManager.getConnection(url,user,passwd);}catch(SQLException e){e.printStackTrace();}}}公共类TestDAO{public static void main(String[]args)抛出SQLException{DAO vehicleDao=DAOFactory.getvehicleDao();vehicleule vehicleDao=vehicleDao.create(0,5100,“test”);Vehicleue Vehicleu2=VehicleuDao.find(“tout”);不要在注释中发布代码,编辑您的问题。不,我曾经尝试过executeUpdate(),但同样的问题->没有在hsqldb数据库上执行提交。我已经使用executeUpdate()执行了提交测试类,请参阅upper。但是没有执行提交命令。我真的不明白为什么?
public class DAOFactory {
protected static final Connection conn = HsqldbConnection.getInstance();
public static DAO getMarqueDAO(){
return new MarqueDAO(conn);
}
public static DAO getVehiculeAO(){
return new MarqueDAO(conn);
}
}
String url = "jdbc:hsqldb:file:hsqldb/database/VEHICULE";
String user = "";
String passwd = "";
Connection conn = DriverManager.getConnection(url, user, passwd);
Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
PreparedStatement prepare = conn.prepareStatement("INSERT INTO VEHICULE (MARQUE, MOTEUR, PRIX, NOM) VALUES (0,4,100,'testcommit')");
prepare.executeUpdate();
conn.commit();
prepare.close();
state.close();
} catch (SQLException e) {
e.printStackTrace();
}