package com.aoyy.dao;
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.aoyy.model.Affiche;
import com.aoyy.util.DBUtils;
/**
* @ filename: AfficheDaoImpl.java
* @ description: The implementation AfficheDaoImpl of interface AfficheDao, which add affiche, query all affiches, the lastest affiche, and the affiche with given afficheid
* @ author: anonymous
* @ version: 1.0
* @ date: today
* @ Copyrights (c) sharing
*/
public class AfficheDaoImpl implements AfficheDao {
/*------------- add affiche ----------*/
@Override
public boolean addAffiche(Affiche affiche) {
boolean result = true;
String sql = "insert into tb_affiche(afficheid, title, content, createtime) values(null, '" + affiche.getTitle() + "', '" + affiche.getContent() + "', '" + affiche.getCreatetime() + "')";
DBUtils dbu = new DBUtils();
Connection conn = null;
Statement stmt = null;
try {
conn = dbu.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
result = stmt.execute(sql);
} catch (SQLException ex) {
System.err.println("Falied to add affiche: " + ex.getMessage());
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return result;
}
/*------------- query affiche with afficheid ----------------*/
@Override
public Affiche getAffiche(int afficheid) {
Affiche affiche = null;
String sql = "select * from tb_affiche where afficheid=" + afficheid + "";
DBUtils dbu = new DBUtils();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = dbu.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
if (rs.next()) {
affiche = new Affiche();
affiche.setAfficheid(rs.getInt("afficheid"));
affiche.setTitle(rs.getString("title"));
affiche.setContent(rs.getString("content"));
affiche.setCreatetime(rs.getTimestamp("createtime"));
}
} catch (SQLException ex) {
System.err.println("Failed to query affice: " + ex.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn !=null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return affiche;
}
/*--------------- query the lastest affiche --------------------*/
@Override
public Affiche getLatestAffiche() {
Affiche affiche = null;
String sql = "select * from tb_affiche order by createtime desc limit 1";
DBUtils dbu = new DBUtils();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = dbu.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
if (rs.next()) {
affiche = new Affiche();
affiche.setAfficheid(rs.getInt("afficheid"));
affiche.setTitle(rs.getString("title"));
affiche.setContent(rs.getString("content"));
affiche.setCreatetime(rs.getTimestamp("createtime"));
}
} catch (SQLException ex){
System.err.println("Failed to query the latest affiche: " + ex.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return affiche;
}
/*------------ get all affiches -----------------*/
@Override
public List<Affiche> getAffiches() {
List<Affiche> list = null;
String sql = "select * from tb_affiche";
DBUtils dbu = new DBUtils();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = dbu.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
list = new ArrayList<Affiche>();
Affiche affiche = null;
while (rs.next()) {
affiche = new Affiche();
affiche.setAfficheid(rs.getInt("afficheid"));
affiche.setTitle(rs.getString("title"));
affiche.setContent(rs.getString("content"));
affiche.setCreatetime(rs.getTimestamp("createtime"));
list.add(affiche);
}
} catch (SQLException ex) {
System.err.println("Failed to query all affiches: " + ex.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return list;
}
public static void main(String[] args) {
AfficheDaoImpl dao = new AfficheDaoImpl();
/*Affiche affiche = new Affiche();
affiche.setTitle("Nature Language Processing");
affiche.setContent("We will hava a forum in this weekend");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
Date date = new Date();
Timestamp createtime = Timestamp.valueOf(formatter.format(date));
affiche.setCreatetime(createtime);
boolean result = dao.addAffiche(affiche);
System.out.println(result);
Affiche affiche = dao.getAffiche(1); */
List<Affiche> list =dao.getAffiches();
for (int i = 0; i < list.size(); i++) {
Affiche affiche = list.get(i);
System.out.println(affiche.getTitle()+":"+affiche.getContent()+":"+affiche.getCreatetime());
}
}
}