Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1.  
  2. package practice1;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class JDBCTest_AutoClsoeable {
  11. public static void main(String[] args) {
  12. // 注意
  13. // 1. Service 視窗必須啟動資料庫服務 Database->JavaDB->Start Server
  14. // 2. 專案程式庫必須加入資料庫驅動程式
  15.  
  16. String url = "jdbc:derby://localhost:1527/EmployeeDB";
  17. String user = "test";
  18. String pass = "tiger";
  19. try( Connection con = DriverManager.getConnection(url, user, pass);
  20. Statement stmt = con.createStatement();
  21. ResultSet rs = stmt.executeQuery("select * from employee")) {
  22.  
  23. while(rs.next()) { // 取得下一筆資料,若有獲取到資料回傳 true
  24. // 讀取目前這筆資料的各項欄位
  25. int id = rs.getInt("id");
  26. String firstName = rs.getString("firstname");
  27. String lastName = rs.getString("lastname");
  28. java.util.Date birthdate = rs.getDate("birthdate");
  29. float salary = rs.getFloat("salary");
  30. // 輸出目前所讀到的員工資料
  31. System.out.println(id + " " + firstName + " " + lastName +
  32. " " + birthdate + " " + salary);
  33. }
  34.  
  35. } catch(SQLException e) {
  36. System.err.println(e);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement