Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package practice1;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JDBCTest_AutoClsoeable {
- public static void main(String[] args) {
- // 注意
- // 1. Service 視窗必須啟動資料庫服務 Database->JavaDB->Start Server
- // 2. 專案程式庫必須加入資料庫驅動程式
- String url = "jdbc:derby://localhost:1527/EmployeeDB";
- String user = "test";
- String pass = "tiger";
- try( Connection con = DriverManager.getConnection(url, user, pass);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from employee")) {
- while(rs.next()) { // 取得下一筆資料,若有獲取到資料回傳 true
- // 讀取目前這筆資料的各項欄位
- int id = rs.getInt("id");
- String firstName = rs.getString("firstname");
- String lastName = rs.getString("lastname");
- java.util.Date birthdate = rs.getDate("birthdate");
- float salary = rs.getFloat("salary");
- // 輸出目前所讀到的員工資料
- System.out.println(id + " " + firstName + " " + lastName +
- " " + birthdate + " " + salary);
- }
- } catch(SQLException e) {
- System.err.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement