標籤:style blog http io ar color os 使用 sp
使用JDBC建立Connection後,執行SQL需要先建立Statement
Statement stmt = connection.createStatement();
建立代碼如下
public java.sql.Statement createStatement() throws SQLException { return createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); }//預設查詢結果 正向迴圈、唯讀查詢結果public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
//檢查 connection是否關閉 checkClosed(); Statement stmt = new com.mysql.jdbc.Statement(this, this.database); stmt.setResultSetType(resultSetType); stmt.setResultSetConcurrency(resultSetConcurrency); return stmt; }
執行查詢SQL時:
ResultSet rs = stmt.executeQuery("test");
返回的ResultSet的資料存放在rowData中(protected RowData rowData)。
執行next後是一個對象數組。
Statement的關閉方法,預設在關閉statment的同時,也關閉ResultSet。
public void close() throws SQLException {realClose(true, true);}
.....
this.isClosed = true;
.....
----------------------------------------------------
1、在DriverManager.getConnection的時候建立與mysql服務端的串連
2、Statement共用同一串連。
3、關閉Connection的時候才會關係串連,也會調用關係所有statement方法closeAllOpenStatements()。
mysql-jdbc建立Statement與執行SQL