简体   繁体   中英

solution with JAVA or Postgres for insertion in database

看到这张图片
(source: hostingpics.net )

how can I add a new customer or supplier?, last time I was using this class for one table "customer":

Code:

public int addnewcustomer(){
    int idcust;
    DBConnection eConnexion = new DBConnection();
    try {
        //Statement state = eConnexion.getConnexion().createStatement();
        String sql = "INSERT INTO customer(name_cust, num_cust, adress_cust, city_cust , tel_cust, ref_cust)";

        sql+= "VALUES (?,?,?,?,?,?)";
        PreparedStatement insertQuery = eConnexion.getConnexion().prepareStatement(sql);
        insertQuery.setString(1,Name_cust);
        insertQuery.setString(2,Num_cust);
        insertQuery.setString(3,Adress_cust);
        insertQuery.setString(4,City_cust );
       insertQuery.setString(5,Tel_cust);
       insertQuery.setString(6,Ref_cust);

       insertQuery.executeUpdate();

} catch (SQLException e) {
        e.printStackTrace();
    //JOptionPane.showMessageDialog(null,"Erreur:the addition is not performed with Succee!");
        idcust = 0;
    }
    eConnexion.closeConnection();
    idcust= Services.getLastInsertedId("customer","id_customer");
    return idcust;
}

Currently, I attach all tables with new table "person". All tables now extend "person", I tried to add new customer with super variables "person" but I'm stuck in filling foreign key "id_pers FK".

First you need to persist a person into your database. After a successful(!) persist, you can query for the id the database used to insert the data. Most databases also provide a method to directly retrieve the used id after an insert. After you have successfully persisted the person you can use the id for the foreign key column.

You may consider using a transaction for these actions, as there should never be a person persisted without a customer/employee whatever extending the persons data. With a transaction, you can rollback the previous actions, for example if something goes wrong during the insertion of the customer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM