jsp复习资料(20)
2017-1-22编辑:ljnbset
编写JSP程序
假设你已经建立了Access数据库student.accdb,并通过ODBC创建了该数据库的数据源studentdata,编写程序数据库中表stu_info信息
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312" errorPage=""%>
<html>
<head>
<title>读取数据库表</title>
</head>
<body>
<%
Class.forNmae("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:studentdata";
String user="";
String pwd="";
Connection con=DriverManager.getConnection(url,user,pwd);
Statement stmt=con.createStatement("ResultSet.TYpe_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY");
ResultSet rs=stmt.executeQuery("select * from stu_info");
rs.last();
%>
<table>
<tr>
<td>学号</td> //假设只显示两列“学号、姓名”
<td>姓名</td>
</tr>
<%
rs.beforeFirst();
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("name")%></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>