Java資料庫——ResultSet介面

來源:互聯網
上載者:User

標籤:

使用SQL中的SELECT語句可以查詢出資料庫的全部結果,在JDBC的操作中資料庫的所有查詢記錄將使用ResultSet進行接收,並使用ResultSet顯示內容。

 

從user表中查詢資料

//=================================================// File Name       :MySQL_demo//------------------------------------------------------------------------------// Author          :Commonimport java.sql.*;//主類//Function        : MySQL_demopublic class MySQL_demo {//定義MySQL的資料庫驅動程式public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";//定義MySQL資料庫的串連地址public static final String DBURL = "jdbc:mysql://localhost:3306/mysql_demo";//MySQL資料庫的串連使用者名稱public static final String DBUSER = "root";//MySQL資料庫的串連密碼public static final String DBPASS = "123456";public static void main(String[] args) throws Exception {// TODO 自動產生的方法存根Connection conn = null;//資料庫連接Statement stmt = null;//資料庫操作ResultSet rs = null;//儲存查詢結果String sql = "SELECT id,name,password,age,sex,birthday FROM user";Class.forName(DBDRIVER);//載入驅動程式//串連MySQL資料庫時,要寫上串連的使用者名稱和密碼conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);stmt = conn.createStatement();//執行個體化Statement對象rs = stmt.executeQuery(sql);//執行個體化ResultSet對象while(rs.next()){int id = rs.getInt("id");String name = rs.getString("name");String pass = rs.getString("password");int age = rs.getInt("age");String sex = rs.getString("sex");Date d = rs.getDate("birthday");System.out.println("編號:"+id);System.out.println("名字:"+name);System.out.println("密碼:"+pass);System.out.println("年齡:"+age);System.out.println("性別:"+sex);System.out.println("生日:"+d);}rs.close(); //關閉結果集stmt.close(); //操作關閉conn.close(); //資料庫關閉}}

 

Java資料庫——ResultSet介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.