简体   繁体   English

有没有更好的方法在java中动态实现SQL语句?

[英]Is there a better way to dynamically implement SQL statements in java?

I have been working with a JDBC for the past couple of weeks and I have come upon a problem that I figure will have subjective answers.在过去的几周里,我一直在使用 JDBC,我遇到了一个我认为会有主观答案的问题。 Let's suppose we want to create a method that dynamically constructs a SQL statement that is to be executed based upon what a user inputs into some GUI.假设我们想要创建一个方法,该方法动态构造一个 SQL 语句,该语句将根据用户输入某些 GUI 的内容执行。 Based on what the user has put into the GUI, we need to gather that information, validate it, then pass it into the database.根据用户输入 GUI 的内容,我们需要收集该信息,对其进行验证,然后将其传递到数据库中。 That is, if the user has left any field empty, we simply do not add any extra conditionals to the SQL statement.也就是说,如果用户将任何字段留空,我们就不会向 SQL 语句添加任何额外的条件。

For example if the user left the hypothetical column "name" blank (and the table automatically generates primary keys) we might write例如,如果用户将假设的列“name”留空(并且表自动生成主键),我们可能会写

INSERT INTO <tableName>; 

to add a new row to the table.向表中添加一个新行。

Alternatively if the user has given a name, we write,或者,如果用户给出了一个名字,我们写,

INSERT INTO <tableName> (name) VALUES (?);

. .

With that context given lets suppose I construct a method that dynamically creates this SQL statement:在给定上下文的情况下,假设我构造了一个动态创建此 SQL 语句的方法:

public void addToDatabase(){
     Connection connection = createConnectionToDatabase();
     String str = "INSERT INTO <tableName>";
     if(!name.isBlank()){
          str += " (name) VALUES (?)"
     }
     str += ";";
     PreparedStatement statement = connection.prepareStatement(str);
     if(!name.isBlank()){
          statement.setString(1, name);
     }
     statement.execute();
     connection.close();

If you notice, we check if name is blank twice - which I find rather annoying since it should only be checked once in my opinion.如果您注意到,我们会检查名称是否为空两次——我觉得这很烦人,因为在我看来它应该只检查一次。 The first time we check if name is blank is to construct the proper string to be placed into the SQL statement.我们第一次检查名称是否为空是构造要放入 SQL 语句中的正确字符串。 The second time we check if the name is blank is to confirm if we need to pass the parameter into the prepared statement.第二次检查名称是否为空是为了确认是否需要将参数传递到准备好的语句中。 This creates a sort of catch-22 that forces us to check if the name is blank twice which I do not like.这创建了一种第 22 条军规,迫使我们两次检查名称是否为空,这是我不喜欢的。 Are there any better ways of handling this situation to only have to check for the name once?有没有更好的方法来处理这种情况,只需检查一次名称?

I found a few other answers stating that there is no better way of doing this kind of dyamic SQL statements but I don't like that answer and am hoping for a better one.我发现了一些其他答案,指出没有更好的方法来执行这种动态 SQL 语句,但我不喜欢那个答案,并希望有一个更好的答案。 Thanks.谢谢。

what you want is equivalent to你想要的相当于

String str = "INSERT INTO <tableName> name valuees (?)";
PreparedStatement statement = connection.prepareStatement(str);

if(!name.isBlank()){
    statement.setString(1, name);
}
else {
    statement.setNull(1, Types.VARCHAR);
}

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

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