Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package hu.ttk.data;
  2.  
  3. import hu.ttk.data.entity.Student;
  4.  
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. //Adatbázishoz csatlakozás.
  10. public class DatabaseDAO implements DataProvider{  
  11.     private java.sql.Connection conn;
  12.    
  13.     private void initConnection() throws Exception{
  14.         if(conn==null || conn.isClosed()){
  15.             //driver betöltése.
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.             //connection string
  18.             String url = "jdbc:mysql://iroda.lanoga.eu:3307/test";
  19.             conn = DriverManager.getConnection(url,"test","lanogapass");
  20.         }
  21.        
  22.     }
  23.  
  24.     @Override
  25.     public ArrayList selectAll() throws Exception {
  26.         initConnection();
  27.         ArrayList back = new ArrayList();
  28.         //Egyszerű sql utasítások.
  29.         Statement st = conn.createStatement();
  30.         //Gyűjtemény, amiben a tábla összes eleme benne van.
  31.         ResultSet rs = st.executeQuery("select * from student");
  32.         while(rs.next()){
  33.             //Adatbázis oszlopai.
  34.             int id = rs.getInt("id");
  35.             String name = rs.getString("name");
  36.             String faculty = rs.getString("faculty");
  37.             int age = rs.getInt("age");
  38.             //Primitív tipus létrehozása. ""+id
  39.             Student stu = new Student(""+id, name, faculty,""+age);
  40.             back.add(stu);
  41.         }
  42.         rs=null;//Memória optimalizáslása.
  43.         st.close();
  44.         conn.close();
  45.         return back;
  46.     }
  47.  
  48.     //Beszúrás az adatbázisba.
  49.     public void insert(Student stu) throws Exception {
  50.         initConnection();
  51.         Statement st= conn.createStatement();
  52.         String sql = "insert into student (name,faculty,age)"
  53.                 + " values(\""+stu.getName()+"\",\""+stu.getFaculty()+"\","+stu.getAge()+")";
  54.         System.out.println("sql"+sql);
  55.         st.execute(sql);
  56.         st.close();
  57.         conn.close();
  58.     }
  59.  
  60.     //Módosítás az adatbázisban.
  61.     public void update(Student stu) throws Exception {
  62.         initConnection();
  63.         Statement st= conn.createStatement();
  64.         String sql = "update student set name=\""+stu.getName()+"\","
  65.                 + "faculty=\""+stu.getFaculty()+"\","+"age="+stu.getAge()
  66.                 +" where id="+stu.getId();
  67.         System.out.println("sql"+sql);
  68.         st.executeUpdate(sql);
  69.        
  70.         st.close();
  71.         conn.close();
  72.     }
  73.  
  74.     //Törlés az adatbázisban.
  75.     public void delete(Student stu) throws Exception {
  76.         initConnection();
  77.         Statement st= conn.createStatement();
  78.         String sql="delete from student where id="+stu.getId();
  79.        
  80.         System.out.println("sql"+sql);
  81.         st.execute(sql);
  82.        
  83.         st.close();
  84.         conn.close();
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement