Java 在具体类需要数据库连接的项目上实现DAO
我试图在我的代码上实现DAO模式,但是我在我的具体类中需要mysql和sqlite连接,因为我执行需要数据库条目的逻辑操作。在创建了这个具体的类之后,我再次使用它来调用这些方法并使用它们,但是我想这没有任何意义,因为我为此创建了一个接口,并且应该以某种方式使用它。我是否也应该通过我的接口方法契约传递连接?请注意,这是可行的,但我觉得我做了一些多余的事情,所以我请求帮助 我有一个POJO类,我不打算在这里发布,它包含了太多的东西,它只是一个POJO类,与我的问题无关 我的DAO接口:Java 在具体类需要数据库连接的项目上实现DAO,java,mysql,sqlite,oop,design-patterns,Java,Mysql,Sqlite,Oop,Design Patterns,我试图在我的代码上实现DAO模式,但是我在我的具体类中需要mysql和sqlite连接,因为我执行需要数据库条目的逻辑操作。在创建了这个具体的类之后,我再次使用它来调用这些方法并使用它们,但是我想这没有任何意义,因为我为此创建了一个接口,并且应该以某种方式使用它。我是否也应该通过我的接口方法契约传递连接?请注意,这是可行的,但我觉得我做了一些多余的事情,所以我请求帮助 我有一个POJO类,我不打算在这里发布,它包含了太多的东西,它只是一个POJO类,与我的问题无关 我的DAO接口: public
public interface AlarmDAO {
public List<Alarm> getAlarmList(String timestamp) throws SQLException, ParseException;
public void insertAlarm(byte[] backlog_id, Date timestamp) throws SQLException;}
在最后一段代码中,我在另一个类的构造函数中初始化了我的具体类,并在它的一个方法中使用了它,但同样,我为什么要首先使用接口?非常感谢从接口中删除抛出SQLException
。除了您正在使用的DAO层之外,不要高举任何人。创建并抛出更多高级异常,如EAlreadyExist
,eDiscovery
,等等。即使是ParseException
也不是好主意,因为这意味着“用户”可能会把查询搞砸。仅使用有效的sql查询,并将数据放入其中
2) 使用,而不是手动关闭。如果发生异常,语句将不会关闭。顺便说一下,把地里的乱七八糟的东西清理干净。为什么会有语句
和其他“本地”对象
3) 我会创建一些IDBCore
,并将所有DB连接放在那里。最简单的方法是提供语句IDBCore.createStatement(string sql)
,并在所有其他类中使用它。它将DAO分为低级DAO(了解数据库)和高级DAO(只提供SQL和逻辑)
-我为什么首先使用界面?
在DAO层级别上,它们有点无用,因为所有人都知道所有其他人,但更高级别将/必须只知道接口,而不知道类 我对你的问题没有答案,但请仔细阅读SQL注入。如果时间戳是
,您的查询会做什么;升降台报警;选择“
它只在本地工作,我看不到会发生这种情况
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");
}
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());}