public class Note { //模型
private Integer ids ;
private String name;
private String content;
private Date date;
public Note() {
super();
}
public Note(Integer ids, String name, String content, Date date) {
super();
this.ids = ids;
this.name = name;
this.content = content;
this.date = date;
}
public Integer getIds() {
return ids;
}
public void setIds(Integer ids) {
this.ids = ids;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return " <tr>"
+"<td style = 'text-align: right'>序号: "+this.ids+"名字:"+this.name+ "</td>"
+"<td style = 'text-align: right'>内容: "+this.content+"日期:"+this.date+ "</td>"
+" </tr>" ;
}
}
public class DatabaseMethod2 { //添加数据,查询数据
private Connection coon;
private PreparedStatement ps;
private ResultSet rs;
public List<Note> selectAllMessage() {
String sql = "select * from text ";
List<Note> list = null;
init(sql);
try {
rs = ps.executeQuery();
if(! rs.isClosed()) {
list = new ArrayList<Note>();
while (rs.next()){
Note note = new Note();
note.setIds(rs.getInt("ids"));
note.setName(rs.getString("name"));
note.setContent(rs.getString("content"));
note.setDate(rs.getDate("date"));
list.add(note);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return list;
}
public void init(String sql) { //连接数据库,数据的初始化
coon = DBhelp.getConnection();
try {
ps = coon.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close() { //关闭资源
DBhelp.destroy(coon, ps, rs);
}
}
insex.jsp
<body> <!-- 写内容的页面 -->
<form action = "opration2.jsp" method = "post">
<input type = "hidden" name = "flag" value = "log"/> <!-- 隐藏域发送一个参数,用opration接收 -->
</form>
<%
DatabaseMethod2 dm = new DatabaseMethod2();
List<Note> list = dm.selectAllMessage();
for( Note n :list) {
out.print(n);
}
%>
</body>
login.jsp
<body>
<!-- 登录的页面 -->
<form action="index.jsp" method = "post">
<input type = "hidden" name = "flag" value = "log"/>
<table>
<tr>
<td>用户名:</td>
<td> <input type = "text" name = "user" /></td>
</tr>
<!-- <tr>
<td>密码:</td>
<td> <input type = "password" name = "password" /></td>
</tr> -->
<tr>
<td colspan = 1 > <input type = "submit" value = "提交"/></td>
</tr>
</table>
</form>
</body>
opration2.jsp
<body> <!-- 这个页面用来接收用户名登陆的判断,也来接收写下的内容,名字以及日期 -->
<%
request.setCharacterEncoding("utf-8"); //如果提交方式为post,如果出现中文乱码,用这三条语句解决
String user = request.getParameter("user");
if(user != null&&user.trim().length()>0 ){
session.setAttribute("currentuser", user); //提交对话,设置
request.getRequestDispatcher("opration.jsp").forward(request, response); //请求转发
}
</body>