2010-09-07 59 views
1

JDBC中有執行通用查詢的方法嗎? 我的意思是運行類似execute(String strSql)的地方,其中strSql可以是SELECT,INSERT,UPDATE,CREATE等等。JDBC通用查詢執行

如果不是,你會如何解決這個問題?

提出的解決方案:

@Override 
public void execQuery(String Query) throws SQLException { 
    this.statement = this.connection.createStatement(); 
    if (this.statement.execute(Query)) { 
     this.resultset = this.statement.getResultSet(); 
    } 
} 
+0

哦,天哪......對不起球員,我只是發現意外的解決方案在我的舊的來源之一,下面的功能解決了這個: – CoolStraw 2010-09-07 02:00:06

+0

@Override 公共無效使用ExecQuery(查詢字符串)拋出的SQLException { 這.statement = this.connection.createStatement();如果(this.statement.execute(Query)){ this.resultset = this.statement.getResultSet(); } } – CoolStraw 2010-09-07 02:00:33

+0

重新格式化的代碼;如果不正確請回復。 – trashgod 2010-09-07 03:19:26

回答

0

請注意,您提出的解決方案是一個容易攻擊SQL injection。改爲使用java.sql.PreparedStatement,如Using Prepared Statements中所述。

+0

嗨,垃圾,感謝格式化我的代碼!然後,我正在開發的應用程序執行用戶的SQL,因此SQL注入根本就不是問題;) – CoolStraw 2010-09-08 17:03:16

+0

用戶喜歡這些嗎? http://xkcd.com/327/ :-) – trashgod 2010-09-08 20:12:34

+0

優秀ahahaha:p – CoolStraw 2010-09-10 05:03:07