Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package com.curistec.tpbdddao;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14. import java.util.Properties;
  15.  
  16. public class RectangleDAO {
  17. Connection conn = null;
  18. Statement statement;
  19. ResultSet resultSet;
  20.  
  21. public RectangleDAO() throws FileNotFoundException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
  22. Properties conf = new Properties();
  23. conf.load(new FileInputStream("conf.properties"));
  24. String driver = conf.getProperty("driverjdbc");
  25. String url = conf.getProperty("url");
  26. String database = conf.getProperty("database");
  27. String user= conf.getProperty("user");
  28. String password= conf.getProperty("password");
  29. Class.forName(driver).newInstance();
  30. conn =DriverManager.getConnection(url+database,user,password);
  31.  
  32. statement = conn.createStatement();
  33. }
  34.  
  35. public Rectangle getRectangle(int id) throws SQLException{
  36. int longueur,largeur;
  37. String sql = "SELECT * FROM rectangles WHERE id = "+id;
  38. resultSet = statement.executeQuery(sql);
  39. resultSet.next();
  40. id = resultSet.getInt("ID");
  41. longueur= resultSet.getInt("longueur");
  42. largeur= resultSet.getInt("largeur");
  43. return new Rectangle(id,longueur,largeur);
  44.  
  45. }
  46.  
  47. public List<Rectangle> getRectangles() throws SQLException{
  48. int id,longueur,largeur;
  49. List<Rectangle> list = new ArrayList<Rectangle>();
  50. String sql = "SELECT * FROM rectangles";
  51. resultSet = statement.executeQuery(sql);
  52.  
  53. while(resultSet.next()){
  54. id = resultSet.getInt("ID");
  55. longueur= resultSet.getInt("longueur");
  56. largeur= resultSet.getInt("largeur");
  57. list.add(new Rectangle(id,longueur,largeur));
  58. }
  59.  
  60. return list;
  61. }
  62.  
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement