package net.zhaodong.msg.dao.mysql.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.zhaodong.msg.dao.IMsgDAO;
import net.zhaodong.msg.db.DBConnection;
import net.zhaodong.msg.entity.Msgentity;
public class MsgDaoMysqlImpl implements IMsgDAO {
public int insertOnmsg(Msgentity msg) {
return 0;
}
public List<Msgentity> getAllMsgsList() {
List<Msgentity> msgList = new ArrayList<Msgentity>();
DBConnection db= new DBConnection();
Connection conn = db.getConn();
try {
Statement stmt = conn.createStatement();
String sql="selecr * from msg";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
int id =rs.getInt(1);
String content = rs.getString(2);
String author = rs.getString(3);
String publishtime = rs.getString(4);
Msgentity msg=new Msgentity(id,content,author,publishtime);
msgList.add(msg);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return msgList;
}
public Msgentity getOneMsgByid(int id) {
return null;
}
public static void main(String[] args){
IMsgDAO dao =new MsgDaoMysqlImpl();
List<Msgentity> msgs=dao.getAllMsgsList();
for(Msgentity msg:msgs){
System.out.println("留言的ID:"+msg.getId());
System.out.println("留言的內容:"+msg.getContent());
System.out.println("留言的作者:"+msg.getAnthor());
System.out.println("留言的發布時間:"+msg.getPublishtime());
}
}
}
你這個是main函數是自己敲的還是建類的時候自動生成的?