自我介绍下,我是一个在校学生,学习的专业是电子,一直偶然的机会接触到了java,有幸被java吸引到,于是开始了java的学习生涯,在此之前一直有在csdn浏览一些前辈的文章,最近也在学习的过程中,打算把学习过程学的内容和遇到的问题在这里记录下来,希望日复一日的积累可以慢慢的提升自己。
言归正传,这是第一篇日志,写的是关于通过jsp来修改存放在数据库表中的数据,在数据库表中的添加数据没有添加到这里,在这里简易的说明下,信息表一个有id,name,age三个属性,在添加信息表的时候,因为id的数据类型曾有了困惑,因为id又可能不是int型,不设为主键就不能递增,于是就另辟道路就想到了利用生成随机数的形式来创建id值,随机数的位数增大虽然可以降低重复的概率,无独有偶,彩票的概率虽然也是很低,但是还是有中奖的机会,那随机数当然也就有了中枪的机会咯,所以随机数id被pass掉,在寻求解答的过程中被一位老师点到了uuid,这个建议瞬间解决了这个问题。
世界这么大,人口这么多,如果同时所有人去生成uuid,这个uuid全部都不会重复,这看起来实在是让我钦佩不已,同时特别钦佩想出这个uuid算法的人。
下面就是整个小项目的代码:
这是注册的学生类
package pers.exercise.domain;
public class Student {
private String id;
private String name;
private int age;
public Student() {
}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
通过jdbc从数据库中表学生的信息列表存放到request域中,因为涉及到连接数据库操作,选择的是重定向到学生列表页,通过jsp实现表的前台展示
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生列表页");
Connection conn1=null;
PreparedStatement ps1=null;
ResultSet res1=null;
String sql1="select id,name,age from tbl_student;";
List<Student> sList=new ArrayList<Student>();
try {
conn1=DButil.getconn();
ps1=conn1.prepareStatement(sql1);
res1=ps1.executeQuery();
while(res1.next()){
Student s=new Student();
s.setId(res1.getString(1));
s.setName(res1.getString(2));
s.setAge(res1.getInt(3));
sList.add(s);
}
}
catch (SQLException e) {
e.printStackTrace();
}finally {
try {
DButil.myClose(conn1, ps1, res1);
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("sList", sList);
request.getRequestDispatcher("/jsp/student/list.jsp").forward(request, response);
创建了一个集合,将学生信息存放到集合中
因为涉及到欢迎的操作,将登录用户名也get过来
<%
List<Student> sList=(List<Student>)request.getAttribute("sList");
String username=(String)session.getAttribute("username");
%>
通过java脚本拼接,在jsp中将学生的信息打印在前台上
[link]
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
td{
text-align: center;
}
a{
text-decoration: none;
}
</style>
<script>
function addStu(){
window.location.href="/2_jsp2/jsp/student/add.jsp";
}
</script>
</head>
<body>
<font color="green" size="30" >您好:<%=username %></font>
<hr/>
<h1 align="center">学生管理系统</h1><br/><br/>
<table border="0" align="center" width="90%" cellpadding="6" cellspacing="0">
<tr>
<td style="text-align: left">
<button onclick="addStu()"><font color="black">添加学生</font></button>
</td>
</tr>
</table>
<table border="1" align="center" width="90%" cellpadding="6" cellspacing="0">
<tr>
<td>序号</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
<%
for(int i=0;i<sList.size();i++){
Student s= sList.get(i);
%>
<tr>
<td><%=i+1 %></td>
<td><%=s.getId() %></td>
<td><%=s.getName() %></td>
<td><%=s.getAge() %></td>
<td>
<a href="edit.do?id=<%=s.getId() %>>">
修改
</a>||
<a href="">
删除
</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
在这里通过url将id在传递到前台
<a href="edit.do?id=<%=s.getId() %>>">
修改
</a>||
<a href="">
删除
</a>
前台收到需要修改的数据的ID值,然后从数据库进行查询,把查到的数据存放到一个学生对象中,然后再存放到requset域中,重定向到修改的jsp中
String id=request.getParameter("id");
System.out.println(id);
/*
* JDBC连接数据库
*/
Connection conn=null;
PreparedStatement ps=null;
ResultSet res =null;
String sql="select id,name,age from tbl_student where id=?";
Student s=new Student();
try {
conn=DButil.getconn();
ps=conn.prepareStatement(sql);
ps.setString(1, id);
res=ps.executeQuery();
if(res.next()){
s.setId(res.getString(1));
s.setName(res.getString(2));
s.setAge(res.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DButil.myClose(conn, ps, res);
} catch (SQLException e) {
//最合理的还是在util里分别try
e.printStackTrace();
}
}
request.setAttribute("s", s);
response.sendRedirect(request.getContextPath()+"/jsp/student/edit.jsp");
}
从域中取出对象,然后分别赋到value属性,这时候出现了500的错误,
%>
<%
Student s=(Student)request.getAttribute("s");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="edit.do" method="post">
<input type="hidden" name="id" value="<%=s.getId()%>"><br/>
请输入要修改的姓名:<input type="text" name="name" value="<%=s.getName()%>" ><br/><br/>
请输入要修改的年龄:<input type="text" name="age" value="<%=s.getAge() %>" ><br/><br/>
<input type="submit" value="提交修改">
</form>>
</body>
</html>
这里出现了错误,反复思考了许久也没有找出问题的关键,目前让我还无法去解决,或许经过一晚上的睡眠,明天就可以豁然开朗,当然也希望有幸可以被您看到我的文章后可以帮我指点一下。
第一篇日志到这里就记录结束了,这个编辑模块确实很强大,很多功能还不太会用,哈哈哈哈。
22:05发布,
现在是 23:02 ,在审核的过程中,自己找到了,真是太蠢了哈哈哈, 这里一定要警示自己:request域一定是转发才可以获取, 转发的时候路径一定不要加项目名。
最后还是想留一句自己特别喜欢的话:努力了就好,剩下的佛系。