|
getString("author");
Timestamp time = rs.getTimestamp("createdate");
Timestamp last = rs.getTimestamp("MIDIFYDATE");
System.out.println("新闻id:"+id+" 标题:"+title+" 摘要:"+summary
+" 内容:"+content+" 作者:"+author+" 创建时间:"+time+" 修改时间:"+last);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//添加新闻信息
public void add(int id,String title,String summary,String content,
String author,Date createdate,Date last){
getConnection();
String sql = "insert into news values( , , , , , , )";
try {
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, title);
pstmt.setString(3, summary);
pstmt.setString(4, content);
pstmt.setString(5, author);
pstmt.setTimestamp(6, new java.sql.Timestamp(createdate.getTime()));
pstmt.setTimestamp(7, new java.sql.Timestamp(last.getTime()));
int i = pstmt.executeUpdate();//i表示插入的行数
if(i>0)
System.out.println("插入成功!");
else
System.out.println("插入失败!");
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
然后看测试类
import java.util.Date;
public class Test {
public static void main(String[] args) {
NewsDao nd = new NewsDao();
nd.getNewsList();
nd.add(2, "有蚊子", "今天竟然有蚊子", "今天竟然真的有蚊子,咬了我好几口!",
"admin", new Date(), new Date());
}
}
程序正常运行。
|