mysql-jdbc建立Statement與執行SQL

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.