【JSP开源代码栏目提醒】:以下是网学会员为您推荐的JSP开源代码-EX8-5.jsp,希望本篇文章对您学习有所帮助。
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>JSP通用范例大全</title>
</head>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); //加载JDBC驱动
String url="jdbc:microsoft:
sqlserver://localhost:1433;DatabaseName=school"; //school为你的数据库
String user="sa"; //连接数据库的用户名
String password="123"; //连接数据库的密码
Connection conn= DriverManager.getConnection(url,user,pass
word); //数据库连接对象
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //SQL语句对象
String sql="select * from Student"; //创建SQL语句
ResultSet rs=stmt.executeQuery(sql); //执行SQL语句并取得结果集
rs.absolute(4); //将记录指针移动到数据表的第4个位置
%>
<table border width=50%>
<caption>Student表中获取第4个数据</caption>
<tr>
<th>数据序号</th>
<th></th>
<th></th>
<th>性别</th>
<th>年龄</th>
<th>院系</th>
</tr>
<tr align=center>
<td><%=rs.getRow()%></td> <!-- 获取当前数据在数据表中位置 -->
<td><%=rs.getString("S_no")%></td>
<td><%=rs.getString("S_name")%></td>
<td><%=rs.getString("S_sex")%></td>
<td><%=rs.getString("S_age")%></td>
<td><%=rs.getString("S_dept")%></td>
</tr>
</table>
<%rs.close(); //关闭结果集
stmt.close(); //关闭SQL语句对象
conn.close(); //关闭数据库
%>
</body>
</html>