dao包
创建MessageDao.java文件
public class MessageDao {
static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String URL="jdbc:sqlserver://127.0.0.1:1433;database=MessageMS";
//静态代码块,
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<MessageBean> getMessages() throws SQLException{
List<MessageBean >list=new ArrayList<MessageBean>();
String sql="\r\n select mid,mcontent,publish from MessageInfo ";
try(Connection conn=DriverManager.getConnection(URL,"sa","123456");
PreparedStatement pstmt=conn.prepareStatement(sql)){
try(ResultSet rs=pstmt.executeQuery()){
while(rs.next()) {
MessageBean mi=new MessageBean();
mi.setMcontent(rs.getString("mcontent"));
mi.setMid(rs.getInt("mid"));
mi.setPublich(rs.getString("publish"));
list.add(mi);
}
}
}
return list;
}
public int del(int mid) throws SQLException {
int n=0;
String sql="delete MessageInfo where mid=?";
try(Connection conn=DriverManager.getConnection(URL,"sa","123456");
PreparedStatement pstmt=conn.prepareStatement(sql);){
pstmt.setInt(1, mid);
n=pstmt.executeUpdate();
}
return n;
}
}
在entity 包
创建MessageBean.java 实体类文件
public class MessageBean {
private int mid ;
private String mcontent;
private String publish;
public int getMid() {
return mid;
}
public void setMid(int mid) {
this.mid = mid;
}
public String getMcontent() {
return mcontent;
}
public void setMcontent(String mcontent) {
this.mcontent = mcontent;
}
public Str