尝试在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:"+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;
}
插入已完成但未提交。 有没有想过调试它?
根据要求,简要说明申请:
HsqldbConnection
:
public class HsqldbConnection {
private HsqldbConnection() {
try {
connect = DriverManager.getConnection(url, user, passwd);
} catch (SQLException e) {
e.printStackTrace();
}
}
// ...
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");
// ...
DAOFactory
:
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);
}
}
我必须在课程VehiculeDAO
中插入提交命令吗?
Test commit
:
public static void main(String [] 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();
}
}
答案 0 :(得分:0)
使用statement.executeUpdate()而不是statement.executeQuery()。