zoukankan      html  css  js  c++  java
  • mysql-jdbc创建Statement与执行SQL

    使用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()。

  • 相关阅读:
    LeetCode15 3Sum
    LeetCode10 Regular Expression Matching
    LeetCode20 Valid Parentheses
    LeetCode21 Merge Two Sorted Lists
    LeetCode13 Roman to Integer
    LeetCode12 Integer to Roman
    LeetCode11 Container With Most Water
    LeetCode19 Remove Nth Node From End of List
    LeetCode14 Longest Common Prefix
    LeetCode9 Palindrome Number
  • 原文地址:https://www.cnblogs.com/zyzl/p/4132372.html
Copyright © 2011-2022 走看看