×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Pascal Surget
Added: Apr 14, 2018 1:54 AM
Modified: Sep 25, 2018 2:18 PM
Views: 3044
  1. package ca.qc.cgodin;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class DBManager {
  12.         // 1 - etablir une connexion
  13.         String connectionString = "jdbc:mysql://";
  14.         String login = "student";
  15.         String pwd = "cgodin2018";
  16.         Connection con = null;
  17.        
  18.         public DBManager() {
  19.                 connectionString += "localhost:3306/test_db?useSSL=false&serverTimezone=UTC";
  20.                 try {
  21.                         con = DriverManager.getConnection(connectionString, login, pwd);
  22.                 } catch (SQLException e) {
  23.                         // TODO Auto-generated catch block
  24.                         e.printStackTrace();
  25.                 }
  26.         }
  27.  
  28.         public List<Person> getPeople() {
  29.                 List<Person> people = null;
  30.                 // 2 - déclarer un statement
  31.                 Statement stmt = null;
  32.                 try {
  33.                         stmt = con.createStatement();
  34.                 } catch (SQLException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 }
  38.  
  39.                 // 3 - executer une requete Select
  40.                 ResultSet res = null;
  41.                 try {
  42.                         res = stmt.executeQuery("SELECT * FROM person");
  43.                 } catch (SQLException e) {
  44.                         // TODO Auto-generated catch block
  45.                         e.printStackTrace();
  46.                 }
  47.  
  48.                 // 4 - lecture du resultat obtenu
  49.                 try {
  50.                         while (res.next()) {
  51.                                 if (people == null) {
  52.                                         people = new ArrayList<Person>();
  53.                                 }
  54.                                 Person p = new Person();
  55.                                 p.setId(res.getInt("id"));
  56.                                 p.setName(res.getString("name"));
  57.                                 p.setAge(res.getInt("age"));
  58.  
  59.                                 people.add(p);
  60.                         }
  61.                 } catch (SQLException e) {
  62.                         // TODO Auto-generated catch block
  63.                         e.printStackTrace();
  64.                 }
  65.                 return people;
  66.         }
  67.        
  68.        
  69.         public void addPerson(Person p) {
  70.                 // 2 - déclarer un statement
  71.                 Statement stmt = null;
  72.                 try {
  73.                         stmt = con.createStatement();
  74.                 } catch (SQLException e) {
  75.                         // TODO Auto-generated catch block
  76.                         e.printStackTrace();
  77.                 }
  78.                
  79.                 // 3 - executer une requete Select
  80.                 try {
  81.                         String req = "INSERT INTO person (id, name, age) values ("+
  82.                                                  p.getId() +
  83.                                                  ",'"+
  84.                                                  p.getName()+
  85.                                                  "',"+
  86.                                                  p.getAge()+
  87.                                                  ")";
  88.                         stmt.executeUpdate(req);
  89.                 } catch (SQLException e) {
  90.                         // TODO Auto-generated catch block
  91.                         e.printStackTrace();
  92.                 }
  93.         }
  94. }