简体   繁体   中英

Can I use one method to edit several SQL tables?

I've got a theoretical question and Java REST API, in which I have to add, remove and edit rows of several SQL tables. Is there any real difference between creating a delete, edit and create method for each table, with full code, and create a delete, edit and create function for SQL code and update with variables passed from delete, edit and create methods for each table? Thank you for your help.

So like this:

  private static void Create_1(){
    String NewValue= "Something";
    String SQLCommand = "INSERT INTO Table1 (Column1) VALUES ("+NewValue+")";
    PreparedStatement PST = conn.prepareStatement(SQLCommand);
    PST.executeUpdate();
}

  private static void Create_2(){
    NewValue= "Something";
    String SQLCommand = "INSERT INTO Table2 (Column2) VALUES ("+NewValue+")";
    PreparedStatement PST = conn.prepareStatement(SQLCommand);
    PST.executeUpdate();
}

VS this:

  private static void Create_1(){
    String NewValue= "Something";
    String Table = "Table1";
    String Column = "Column1";
    Create_Base(NewValue, Table, Column);
}

 private static void Create_2(){
    String NewValue= "Something";
    String Table = "Table2";
    String Column = "Column2";
    Create_Base(NewValue, Table, Column);
}

 private static void Create_Base(String NewValue, String Table, String Column){
    String SQLCommand = "INSERT INTO "+Table+" ("+Column+") VALUES ("+NewValue+")";
    PreparedStatement PST = conn.prepareStatement(SQLCommand);
    PST.executeUpdate();
}

I will say that there is a thin line of difference between the two approaches of yours. When you have a application which needs to interact with database quite so often, and when you don't have an ' Active Record ' library to deal with the database, then your second method will make more sense with respect to readability, modularity of your code. And, a point to note is that, it all depends on the size of your application, intensity of SQL code in your application. Good question by the way :)

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