这章是接着前面的,作为补充数据库操作的例子呵呵供大家参考。
(4)分页显示记录
showByPage.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<%! int pageSize=3; //每页显示的记录数。
int pageCount=0; //分页后的总页数。
%>
<%-- 客户通过表单提交欲要显示的页码数--%>
<FORM action="" method=get >
输入页码数<Input Type=text name=showPage size=4 >
<Input Type=submit name=g value=提交>
</FORM>
<% Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try { con=DriverManager.getConnection("jdbc:odbc:sun","sa","");
sql=
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//返回可滚动的结果集:
rs=sql.executeQuery("SELECT * FROM students");
//将游标移动到最后一行:
rs.last();
//获取最后一行的行号:
int lastRow=rs.getRow();
//计算分页后的总页数:
pageCount=(lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1);
//当前显示的初始页数:
int showPage=1;
//告知客户总页数:
%>
<P> 共有<%=pageCount%>页
<BR>每页显示<%=pageSize%>条记录.
<% //获取客户想要显示的页数:
String integer=request.getParameter("showPage");
if(integer==null)
{ integer="1";
}
try {showPage=Integer.parseInt(integer);
}
catch(NumberFormatException e)
{showPage=1;
}
if(showPage<=1)
{showPage=1;
}
if(showPage>=pageCount)
{showPage=pageCount;
}
%>
<BR>目前显示第<%=showPage%>页
<% //如果要显示第showPage页,那么游标应移到posion的值是:
int posion=(showPage-1)*pageSize+1;
rs.absolute(posion); // 设置游标的位置
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"学号");
out.print("<TH width=100>"+"姓名");
out.print("<TH width=50>"+"数学成绩");
out.print("<TH width=50>"+"英语成绩");
out.print("<TH width=50>"+"物理成绩");
out.print("</TR>");
for (int i=1;i<=pageSize;i++)
{ out.print("<TR>");
out.print("<TD >"+rs.getString(1)+"</TD>");
out.print("<TD >"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getInt("数学成绩")+"</TD>");
out.print("<TD >"+rs.getInt("英语成绩")+"</TD>");
out.print("<TD >"+rs.getInt("物理成绩")+"</TD>");
out.print("</TR>") ;
rs.next();
}
out.print("</Table>");
con.close();
}
catch(SQLException e1) {}
%>
</BODY>
</HTML>
(5) 连接Oracle数据库的例子
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con=null;
Statement sql=null;
ResultSet rs=null;
try{Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e){}
try
{ con=
DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.35:1521:Lea","scott","tiger");
sql=con.createStatement();
rs=sql.executeQuery("select * from emp");
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"EMPNO");
out.print("<TH width=50>"+"Ename");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
int n=rs.getInt(1);
out.print("<TD >"+n+"</TD>");
String e=rs.getString(2);
out.print("<TD >"+e+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e1) {out.print(""+e1);}
%>
</BODY>
</HTML>
(6)查询电子表格Excel的例子
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try { con=DriverManager.getConnection("jdbc:odbc:star","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM 品名 ");
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"品名");
out.print("<TH width=50>"+"单位");
out.print("<TH width=50>"+"单价");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
String name=rs.getString(1);
out.print("<TD >"+name+"</TD>");
String unit=rs.getString(2);
out.print("<TD >"+unit+"</TD>");
String unitprice=rs.getString(3);
out.print("<TD >"+unitprice+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e1) {}
%>
</BODY>
</HTML>
(7)使用同步连接
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<%! //声明一个共享的连接对象:
Connection con=null;
%>
<% Statement sql=null;
ResultSet rs=null;
//第一个客户负责建立连接对象:
if(con==null)
{ try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{out.print(e);
}
try {con=DriverManager.getConnection("jdbc:odbc:moon","sa","");
sql=con.createStatement();
rs =sql.executeQuery("SELECT * FROM students");
out.print("i am first");
}
catch(SQLException e)
{out.print(e);
}
}
//其它客户通过同步块使用这个连接:
else
{ synchronized(con)
{ try { sql=con.createStatement();
rs =sql.executeQuery("SELECT * FROM students");
out.print("i am not first");
}
catch(SQLException e)
{out.print(e);
}
}
}
try
{ out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"学号");
out.print("<TH width=100>"+"姓名");
out.print("<TH width=50>"+"数学成绩");
out.print("<TH width=50>"+"英语成绩");
out.print("<TH width=50>"+"物理成绩");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
String number=rs.getString(1);
out.print("<TD >"+number+"</TD>");
String name=rs.getString(2);
out.print("<TD >"+name+"</TD>");
int math=rs.getInt("数学成绩");
out.print("<TD >"+math+"</TD>");
int english=rs.getInt("英语成绩");
out.print("<TD >"+english+"</TD>");
int physics=rs.getInt("物理成绩");
out.print("<TD >"+physics+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
}
catch(SQLException e1) {}
%>
</BODY>
</HTML>
呵呵,上面举的例子都是具有代表性的例子。
在文章最后还要声明一下:本篇文章是参考JSP实用教程写的,再次提出感谢,另外该书不错,本人看了,推荐JSP初学者可以买一本看看,获益非浅的。