目标:使用JDNI读取数据源、在jsp页面中读取并呈现sql数据库的数据
思路:写好java程序show类,在jsp页面调用show类。实现目标。(具体思路不太清楚)
前期准备:
1.javabean封装数据:代码如下:
package test;
public class javabeanxueshengxinxi {
private int id;
private String sname;
private int age;
private String sex;
private int score;
private String hobby;
//使用javabean类封装学生信息
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
2、展示在jsp页面需要调用的类:代码如下:
package test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Showonindex {
static ResultSet rs = null;
public List<Students> showonindex() throws SQLException{
List<Students> studentlist=new ArrayList<Students>();
String sql = "select * from ms";
Object[] params={};
rs=this.executequery(sql, params);
while(rs.next())
{
Students students=new Students();
students.setId(rs.getInt("id"));
students.setSname(rs.getString("sname"));
students.setAge(rs.getInt("age"));
students.setSex(rs.getString("sex"));
students.setScore(rs.getInt("score"));
students.setHobby(rs.getString("hobby"));
students.add(students);
}
return studentlist;
}
}
3、jsp页面:代码如下:
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*,test.*,java.sql.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":"
+ request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doUserCreate.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css"
href="<%=request.getContextPath()%>/css/common.css">
</head>
<body>
<div>
<h1>使用JDNI读取数据源、在jsp页面中读取并呈现sql数据库的数据</h1>
<%
Showonindex showonindex = new Showonindex();
List<Students> studentlist = showonindex.showonindex();
int i = 0;
for (Students student : studentlist) {
i++;
%>
<tr>
<td><%=student.getId()%></td>
<td><%=student.getSname()%></td>
<td><%=student.getAge()%></td>
<td><%=student.getSex()%></td>
<td><%=student.getScore()%></td>
<td><%=student.getgetHobby()%></td>
</tr>
<%
}
%>
</div>
<%
shujuyuan sjy = new shujuyuan();
Connection con = sjy.getConnection();
%>
<%=con%>
<%
check ch = new check();
ch.show();
%>
</body>
</html>
困惑:
书本上的思路是:
1、使用javabean封装数据
2、使用javabean封装业务
3、使用jsp显示数据列表
4、使用jsp标签实现javabean属性的读取设置
我不太理解。(我想的是,用java类写个方法 在jsp页面调用显示即可,不懂为何要封装)希望大佬能给一些具体的思路逻辑(当然越详细越好),感激不尽~!!!