index.jsp 页面
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 'index.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="styles.css"> --> <style
type="text/css"> table,tr,td,th{
border: 1px solid black;
margin: 0 auto;
border-spacing: 0px;
padding: 10px; } </style> </head> <body> <table> <tr><th>编号</th><th>姓名</th><th>性别</th><th>年龄</th></tr> <%
Connection conn=DBHelper.getConn();
//准备sql 语句
String sql="select * from User_name";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){ %> <tr> <td><%=rs.getInt("id") %></td> <td><%=rs.getString("name") %></td>
<td><%=rs.getString("sex") %></td> <td><%=rs.getInt("age")
%></td> </tr> <% } DBHelper.close(conn, ps, rs);
%> </table> </body> </html>
DBHelper 连接sql server 数据库
- package cn.shaojiajia.util; import java.sql.Connection; import
java.sql.DriverManager; import java.sql.PreparedStatement; import
java.sql.ResultSet; import java.sql.SQLException; public class
DBHelper { static { try {
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”); }
catch (ClassNotFoundException e) { e.printStackTrace(); } }
public static Connection getConn() { Connection conn = null; try
{ conn = DriverManager.getConnection(
“jdbc:sqlserver://127.0.0.1:1433;databaseName=User”, “sa”,
“1”); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void close(Connection conn,
PreparedStatement ps, ResultSet rs) { try { if(conn!=null)
conn.close(); } catch (SQLException e) { e.printStackTrace();
} try { if(ps!=null) ps.close(); } catch (SQLException e)
{ e.printStackTrace(); } try { if(rs!=null) rs.close();
} catch (SQLException e) { e.printStackTrace(); } } }
model层 构造方法
-
package cn.shaojiajia.model; public class Student { private Integer
id; private String name; private String sex; private Integer age;
@Override public String toString() { return “Student [id=” + id +
“, name=” + name + “, sex=” + sex + “, age=” + age + “]”; }
public Student(Integer id, String name, String sex, Integer age) {
super(); this.id = id; this.name = name; this.sex = sex;
this.age = age; } public Student() { super(); // TODO
Auto-generated constructor stub } public Integer getId() { return
id; } public void setId(Integer id) { this.id = id; } public String
getName() { return name; } public void setName(String name) {
this.name = name; } public String getSex() { return sex; } public
void setSex(String sex) { this.sex = sex; } public Integer getAge()
{ return age; } public void setAge(Integer age) { this.age = age;
} }最后数据库及表结构以及数据库内容 的填写 点点点.....