<%@ page contentType="text/html; charset=big5" language="java" import="java.sql.*" errorPage="" %>
<!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=big5">
<title>无标题文档</title>
</head><body>
<%
String name,number;
int maths,physics,english;
Connection conn;
Statement sql;
ResultSet rs;
try
{
Class.forName("oracle,jdbc,driver.OracleDriver").newInstance();
}
catch(ClassNotFoundException e){}
try{
String url="jdbc:odbc:thin:@localhost:1521:JPS";
String user="JSPDEV";
String password="100200";
conn=DriverManager.getConnection(url,user,password);
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//返回可流動的結果集。
rs=sql.executeQuery("SELECT * FROM STUDENT");
//將返回到最后一行
rs.last();
//獲取最后一行的行號
int lownumber=rs.getRow();
out.println("該表共有"+lownumber+"條記錄");
out.println("<br>現有逆序輸出記錄﹕");
out.println("<table border>");
out.println("<tr>");
out.println("<th width=100>"+"學號");
out.println("<th width=100>"+"姓名");
out.println("<th width=100>"+"數學成績");
out.println("<th width=100>"+"英語成績");
out.println("<th width=100>"+"物理成績");
out.println("</tr>");
//為了逆序輸出記錄﹐需將游標移動到最后一行
rs.afterLast();
while(rs.previous())
{out.println("<tr>");
number=rs.getString(1);
out.println("<td>"+number+"</td>");
name=rs.getString(2);
out.println("<td>"+name+"</td>");
maths=rs.getInt("maths");
out.println("<td>"+maths+"</td>");
english=rs.getInt("english");
out.println("<td>"+english+"</td>");
physics=rs.getInt("physics");
out.println("<td>"+physics+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("單獨輸出第5條記錄<br>");
rs.absolute(5);
number=rs.getString(1);
out.println(number+", ");
name=rs.getString(2);
out.println(name+", ");
maths=rs.getInt("maths");
out.println(maths+", ");
english=rs.getInt("english");
out.println(english+", ");
physics=rs.getInt("physics");
out.println(physics+". ");
conn.close();
}
catch(SQLException e1){}
%>
</body>
</html>
<!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=big5">
<title>无标题文档</title>
</head><body>
<%
String name,number;
int maths,physics,english;
Connection conn;
Statement sql;
ResultSet rs;
try
{
Class.forName("oracle,jdbc,driver.OracleDriver").newInstance();
}
catch(ClassNotFoundException e){}
try{
String url="jdbc:odbc:thin:@localhost:1521:JPS";
String user="JSPDEV";
String password="100200";
conn=DriverManager.getConnection(url,user,password);
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//返回可流動的結果集。
rs=sql.executeQuery("SELECT * FROM STUDENT");
//將返回到最后一行
rs.last();
//獲取最后一行的行號
int lownumber=rs.getRow();
out.println("該表共有"+lownumber+"條記錄");
out.println("<br>現有逆序輸出記錄﹕");
out.println("<table border>");
out.println("<tr>");
out.println("<th width=100>"+"學號");
out.println("<th width=100>"+"姓名");
out.println("<th width=100>"+"數學成績");
out.println("<th width=100>"+"英語成績");
out.println("<th width=100>"+"物理成績");
out.println("</tr>");
//為了逆序輸出記錄﹐需將游標移動到最后一行
rs.afterLast();
while(rs.previous())
{out.println("<tr>");
number=rs.getString(1);
out.println("<td>"+number+"</td>");
name=rs.getString(2);
out.println("<td>"+name+"</td>");
maths=rs.getInt("maths");
out.println("<td>"+maths+"</td>");
english=rs.getInt("english");
out.println("<td>"+english+"</td>");
physics=rs.getInt("physics");
out.println("<td>"+physics+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("單獨輸出第5條記錄<br>");
rs.absolute(5);
number=rs.getString(1);
out.println(number+", ");
name=rs.getString(2);
out.println(name+", ");
maths=rs.getInt("maths");
out.println(maths+", ");
english=rs.getInt("english");
out.println(english+", ");
physics=rs.getInt("physics");
out.println(physics+". ");
conn.close();
}
catch(SQLException e1){}
%>
</body>
</html>
int i;
while(rs.next()){
i++
number=rs.getString(1);
...
...
}
不用用rs.last,要取number,定义int i;
看看这篇文章吧,了解一下ResultSet
<!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=big5">
<title>无标题文档</title>
</head><body>
<%
String name,number;
int maths,physics,english;
Connection conn;
Statement sql;
ResultSet rs;
try
{
Class.forName("oracle,jdbc,driver.OracleDriver").newInstance();
}
catch(ClassNotFoundException e){}
try{
String url="jdbc:odbc:thin:@localhost:1521:JPS";
String user="JSPDEV";
String password="100200";
conn=DriverManager.getConnection(url,user,password);
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//返回可流動的結果集。
rs=sql.executeQuery("SELECT * FROM STUDENT");
out.println("<table border>");
out.println("<tr>");
out.println("<th width=100>"+"學號");
out.println("<th width=100>"+"姓名");
out.println("<th width=100>"+"數學成績");
out.println("<th width=100>"+"英語成績");
out.println("<th width=100>"+"物理成績");
out.println("</tr>");int i=0;
while(rs.previous())
{
i++;
out.println("<tr>");
number=rs.getString(1);
out.println("<td>"+number+"</td>");
name=rs.getString(2);
out.println("<td>"+name+"</td>");
maths=rs.getInt("maths");
out.println("<td>"+maths+"</td>");
english=rs.getInt("english");
out.println("<td>"+english+"</td>");
physics=rs.getInt("physics");
out.println("<td>"+physics+"</td>");
out.println("</tr>");}
//////////////////////////////////////////////////////
//注意:在while(rs.next()){}外部不要在调用rs.get....
////////////////////////////////////////////////////
out.println("</table>");
out.println("共有记录"+i+"条");
out.println("單獨輸出第5條記錄<br>");
conn.close();
}
catch(SQLException e1){}
%>
</body>
</html>你试试把,我觉得你最主要是把rs的数据显示出来,完了以后你在试试其他功能。
才發現原來是這里出錯了String url="jdbc:odbc:thin:@localhost:1521:JPS";應該改為String url="jdbc:oracle:thin:@localhost:1521:JPS";改好之后還是有錯誤java.sql.SQLException: No suitable driver 但是我明明有把oracle 的驅動放到LIB文件夾里面了啊
try
{
Class.forName("oracle,jdbc,driver.OracleDriver").newInstance();
}oracle,jdbc,driver.OracleDriver這里應該是英文的句話不是逗號﹐改為
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
就可以了。謝謝不留老哥了。