I'm trying to implement DAO pattern on my code,however I need mysql and sqlite connections in my concrete class since I do logic operations that require database entries. After creating this concrete class, I used it again to call these methods and use them, however it doesn't make any sense I guess since I created an interface for this, and should use it somehow.Should I also pass connections through my interface method-contracts?Please note that this works but I feel like I have done something redundant, so I'm asking for help.
I have a POJO class that I'm not going to post here,contains too much stuff and it's just a pojo class and has nothing to do with my question.
My DAO interface:
public interface AlarmDAO {
public List<Alarm> getAlarmList(String timestamp) throws SQLException, ParseException;
public void insertAlarm(byte[] backlog_id, Date timestamp) throws SQLException;}
And my concrete class:
public class AlarmConcrete implements AlarmDAO {
private SQLiteJDBC sqLiteJDBC;
private Utility utility;
private MysqlConnection mysqlConnection;
private Connection connection;
Statement stmt;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public AlarmConcrete(MysqlConnection mysqlConnection,SQLiteJDBC sqLiteJDBC){
this.sqLiteJDBC = sqLiteJDBC;
this.mysqlConnection = mysqlConnection;
}
/**
* @param timestamp
* @return
* @throws SQLException
* @throws ParseException
*/
public List<Alarm> getAlarmList(String timestamp) throws SQLException, ParseException {
List<Alarm> alarmList = new ArrayList<Alarm>();
ResultSet rs = null;
try {
connection = mysqlConnection.getConnection();
stmt = connection.createStatement();
String modifiedStamp = "?"+timestamp+"?";
String tokenize = modifiedStamp.replace("?","'");
String query = "select * FROM alarm WHERE `timestamp` >= " + tokenize+ " ORDER BY `timestamp` ASC";
rs = stmt.executeQuery(query);
while (rs.next()) {
Alarm alarmObject = new Alarm();
alarmObject.setBacklogId(rs.getBytes("backlog_id"));
Date date = format.parse(rs.getString("timestamp"));
alarmObject.setTimestamp(date);
alarmObject.setEventId(rs.getBytes("event_id"));
alarmList.add(alarmObject);
}
} catch (SQLException e) {
e.printStackTrace();
}
return alarmList;
}
/**
* @param backlog_id
* @param timestamp
* @throws SQLException
*/
public void insertAlarm(byte[] backlog_id, Date timestamp) throws SQLException {
try{
Statement statement=null;
Connection conn = sqLiteJDBC.getLiteConnection();
String modTime = "'"+format.format(timestamp)+"'";
System.out.println("Database opened");
System.out.println("VALUES ("+backlog_id+","+timestamp+")");
statement =conn.createStatement();
String sql = "INSERT INTO `alarm_entries` (`backlog_id`,`timestamp`)" + "VALUES (X'"+Utility.bytesToHex(backlog_id)+"'," +
""+modTime+")";
statement.executeUpdate(sql);
statement.close();
}catch (Exception e){
System.out.println("Couldn't insert latest alarm");
}
System.out.println("Records created");
}
}
I used it like this:
public ScheduledTask(MysqlConnection mysqlConnection,SQLiteJDBC conn) throws SQLException, ParseException, IOException {
this.mysqlConnection = mysqlConnection;
this.lastAlarm = new Alarm();
this.dbUtil = new DBUtil(mysqlConnection);
this.conn = conn;
this.commandLineArgs = new CommandLineArgs();
this.alarmEntryList = new AlarmConcrete(mysqlConnection,conn);
}...{....alarmEntryList.insertAlarm(lastAlarm.getBacklogId(), lastAlarm.getTimestamp());}
In last piece of code I initialized my concrete class in constructor of another class and used it in one of its methods, but again, why did I use interface in the first place?Many thanks
Aucun commentaire:
Enregistrer un commentaire