Guest User

Untitled

a guest
Mar 14th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package persistence.db;
  2.  
  3. import business_logic.Address;
  4. import business_logic.EMail;
  5. import business_logic.MainInterface;
  6. import business_logic.Telephone;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. public abstract class DAO<T extends MainInterface>{
  15.     private Connection connection;
  16.  
  17.     public DAO(Connection connection){
  18.         this.connection = connection;
  19.     }
  20.  
  21.     public Connection getConnection(){
  22.         return connection;
  23.     }
  24.    
  25.     public void closeAll(ResultSet rs, PreparedStatement pstmt){
  26.         try{
  27.             if(pstmt != null)
  28.                 pstmt.close();
  29.             if(rs != null)
  30.                 rs.close();
  31.         }catch(SQLException sqle){}
  32.     }
  33.    
  34.     public long getModified(String table, int id){
  35.         PreparedStatement pstmt = null;
  36.         ResultSet rs = null;
  37.         try{
  38.             pstmt = connection.prepareStatement("SELECT modified FROM "+table+" WHERE id = ?");
  39.             pstmt.setInt(1, id);
  40.             rs = pstmt.executeQuery();
  41.             if(rs.next())
  42.                 return rs.getLong("modified");
  43.             else
  44.                 return 0;
  45.         }catch(SQLException ex){
  46.             Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
  47.         }finally{
  48.             closeAll(rs, pstmt);
  49.         }
  50.         return 0;
  51.     }
  52.  
  53.     public long getCurrentTimestamp(){
  54.         return System.currentTimeMillis()/1000;
  55.     }
  56.  
  57.     public void setId(PreparedStatement pstmt, T a){
  58.         try{
  59.             ResultSet keys = pstmt.getGeneratedKeys();
  60.             if (keys.next())
  61.                 a.setId(keys.getInt(1));
  62.             keys.close();
  63.         }catch(SQLException ex){
  64.             Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
  65.         }
  66.     }
  67. }
  68.  
  69. package test;
  70.  
  71. import java.sql.Connection;
  72. import java.sql.DriverManager;
  73. import java.sql.SQLException;
  74. import java.util.logging.Level;
  75. import java.util.logging.Logger;
  76. import persistence.db.*;
  77. import business_logic.*;
  78.  
  79.  
  80. public class AddressDaoTest{
  81.     private static Connection connection;
  82.     private static AddressDAO dao;
  83.    
  84.     public static void main(String args[]){
  85.         try{
  86.             Class.forName("com.mysql.jdbc.Driver");
  87.         }catch(ClassNotFoundException ex){
  88.             Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  89.         }
  90.         try{
  91.             connection = DriverManager.getConnection("jdbc:mysql://localhost/schoolmanagement", "root", "");
  92.         }catch(SQLException ex){
  93.             Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
  94.         }
  95.         dao = new AddressDAO(connection);
  96.         Address ad = null;
  97.         try{
  98.             ad = new Address(0, 1, 0, 0, "Teststreet", "Testhnr", "1234", "Testcity");
  99.         }catch(SchulException ex){
  100.             Logger.getLogger(AddressDaoTest.class.getName()).log(Level.SEVERE, null, ex);
  101.         }
  102.         System.out.println("ID: "+ad.getId());///getId liefert 0, da dem Konstruktor 0 übergeben wird
  103.         dao.save(ad);
  104.         System.out.println("ID: "+ad.getId());//getId liefert immer noch 0
  105.     }
  106. }
Add Comment
Please, Sign In to add comment