獲得連接后,我們可以使用sql語句與數(shù)據(jù)庫交互。
通常sql語句由從Connection返回的 Statement
接口執(zhí)行。
JDBC語句
用于通用訪問數(shù)據(jù)庫。在使用靜態(tài)SQL語句時非常有用。
Statement接口不能接受參數(shù)。
Statement對象使用Connection對象的createStatement()方法創(chuàng)建,如下所示:
Statement stmt = conn.createStatement( );
我們可以使用Statement用它的三個execute方法之一來執(zhí)行一個SQL語句。
boolean execute(String SQL)
int executeUpdate(String SQL)
ResultSet executeQuery(String SQL)
執(zhí)行SELECT語句并返回結果。返回ResultSet對象。我們需要關閉一個 Statement
對象來清理分配的數(shù)據(jù)庫資源。
If we close the Connection object first, it will also close the Statement object.
Statement stmt = null; try { stmt = conn.createStatement( ); stmt execuate method(); } catch (SQLException e) { } finally { stmt.close(); }
更多建議: