Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use one method to edit several SQL tables?

Tags:

function

sql

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();
}
like image 777
Baksas Avatar asked Nov 19 '25 02:11

Baksas


1 Answers

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 :)

like image 186
sabhari karthik Avatar answered Nov 21 '25 18:11

sabhari karthik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!