開心生活站

位置:首頁 > IT科技 > 

java,resultset

IT科技1.65W

<link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b2fbfaddf.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b38b8bad702ecfe21037ca964.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

   <link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java resultset是什麼?讓我們一起來了解一下吧!

java resultset是我們在運用jdbc進行對接的時候,查詢出的一個返回結果集合。Resultset的功能就是完成了存儲查詢結果,但是它只能讀取一次,不能做到滾動讀取。

 

java resultset

ResultSetMetaData:

我們可以應用 ResultSet.getMetaData() 方法來得到 ResultSetMetaData。通過該信息能夠得到表的結構,比如說列名,列的個數,列數據類型等。

一.獲取列名

ResultSetMetaData.getColumnName(m);

獲取第m位的列名

二.獲取列個數

ResultSetMetaData.getColumnCount();

獲取列的個數

三.獲得列類型

1.ResultSetMetaData.getColumnType(m);

獲取第m位的列類型,對應java.sql.Types中的數據信息

2.ResultSetMetaData.getColumnTypeName(m);

獲取第m位的列類型名稱

實戰演練,具體步驟如下:

package com.lingaolu.Utils; import java.io.FileReader;import java.io.IOException;import java.net.URL;import java.sql.*;import java.util.Properties; public class JdbcUtils {    private static String driver;    private static String url;    private static String userName;    private static String pw;     static{        try {            Properties p = new Properties();            ClassLoader classLoader = JdbcUtils.class.getClassLoader();            // 這個路徑相對於src的路徑來說            URL resource = classLoader.getResource("com/file/jdbc.properties");            String path = resource.getPath();            p.load(new FileReader(path));            driver = p.getProperty("driver");            url = p.getProperty("url");            userName = p.getProperty("user");            pw = p.getProperty("password");            Class.forName(driver);        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }     public static Connection createConnection() throws SQLException {        return DriverManager.getConnection(url, userName, pw);    }     public static void close(Statement stmt,Connection con){        if(null != stmt){            try {                stmt.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if(null != con){            try {                con.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }     public static void close(ResultSet set,Statement s,Connection con){        if(null != set){            try {                set.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        close(s,con);    }}package com.lingaolu.jdbcConnector; import com.lingaolu.Utils.JdbcUtils; import java.sql.*;import java.util.ArrayList;import java.util.List; public class Demo3 {    public static void main(String[] args) {        String sql = "select * from account";        List accounts = fineAccount(sql);        accounts.forEach(System.out::println);        System.out.println("----------------------------------");        sql = "select * from account where name='張三'";        accounts = fineAccount(sql);        accounts.forEach(System.out::println);    }     public static List fineAccount(String sql){        Connection con = null;        Statement stmt = null;        ResultSet resultSet = null;        List rerurnList = new ArrayList<>();        try {            con = JdbcUtils.createConnection();            stmt = con.createStatement();            resultSet = stmt.executeQuery(sql);            Account acc = null;            while(resultSet.next()){                // 引號裏的字段要與表裏的一樣                int id = resultSet.getInt("id");                String name = resultSet.getString("name");                double balance = resultSet.getDouble("balance");                int age = resultSet.getInt("age");                 acc = new Account();                acc.setId(id);                acc.setName(name);                acc.setBalance(balance);                acc.setMyAge(age);                 rerurnList.add(acc);            }        } catch (SQLException e) {            e.printStackTrace();        }finally {            JdbcUtils.close(resultSet,stmt,con);        }        return rerurnList;    }}

標籤:resultset java