當前位置:生活全書館 >

IT科技

> resultset java

resultset java

<link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea273683c8.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea303194c0eaf695827b59325e.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
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/oqp90q.html