⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e252. getting data from a result set.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
A result set contains the results of a SQL query. The results are kept in a set of rows, one of which is designated the current row. A row must be made current before data can be retrieved from it. The result set maintains a reference to the current row called the cursor. 
The cursor is positioned before the first row when a result set is created. When a result set's next() method is called, the cursor moves to the first row of the result set and that row becomes the current row. 

There are two ways to retrieve the data from the current row. The first uses a column index starting from 1. The second uses a column name. For example, with the query `\cv{SELECT col1, col2 FROM table}', the value for col2 can be retrieved using a column index of 2 or with the column name col2. This example demonstrates both methods. 

    try {
        // Create a result set containing all data from my_table
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM my_table");
    
        // Fetch each row from the result set
        while (rs.next()) {
            // Get the data from the row using the column index
            String s = rs.getString(1);
    
            // Get the data from the row using the column name
            s = rs.getString("col_string");
        }
    } catch (SQLException e) {
    }

Here is another example of retrieving data from a result that uses the various getXXX() methods. This example uses the table created in e248 Creating a MySQL Table to Store Java Types. 
    try {
        // Create a result set containing all data from mysql_all_table
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM mysql_all_table");
    
        // Fetch each row from the result set
        while (rs.next()) {
            boolean bool = rs.getBoolean("col_boolean");
            byte b = rs.getByte("col_byte");
            short s = rs.getShort("col_short");
            int i = rs.getInt("col_int");
            long l = rs.getLong("col_long");
            float f = rs.getFloat("col_float");
            double d = rs.getDouble("col_double");
            BigDecimal bd = rs.getBigDecimal("col_bigdecimal");
            String str = rs.getString("col_string");
            Date date = rs.getDate("col_date");
            Time t = rs.getTime("col_time");
            Timestamp ts = rs.getTimestamp("col_timestamp");
            InputStream ais = rs.getAsciiStream("col_asciistream");
            InputStream bis = rs.getBinaryStream("col_binarystream");
            Blob blob = rs.getBlob("col_blob");
        }
    } catch (SQLException e) {
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -