简体   繁体   English

我可以使用一种方法编辑多个SQL表吗?

[英]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. 我有一个理论问题和Java REST API,我必须在其中添加,删除和编辑几个SQL表的行。 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? 为每个表创建删除,编辑和创建方法与完整代码之间是否有任何真正的区别,并为SQL代码创建删除,编辑和创建函数,并使用从每个表的删除,编辑和创建方法传递的变量进行更新? 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: VS这个:

  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. 如果你的应用程序需要经常与数据库进行交互,并且当你没有“ Active Record ”库来处理数据库时,那么你的第二种方法在你的可读性,模块性方面会更有意义。码。 And, a point to note is that, it all depends on the size of your application, intensity of SQL code in your application. 并且需要注意的是,这一切都取决于应用程序的大小,应用程序中SQL代码的强度。 Good question by the way :) 顺便问好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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