当前位置:文档之家› jsp实现连接数据库分页浏览 增删改查等功能

jsp实现连接数据库分页浏览 增删改查等功能

一利用jsp实现Oracle数据库中表格的分页浏览:二在Oracle数据库中建立表格,包含员工的工号、姓名、性别、工龄、工资等字段,并实现增删改查操作。

(1)所建的worker表格如下:(2)Input.jsp运行结果如下:(3)Inputcheck.jsp运行结果如下:Pagebreak.jsp代码如下:<%@page contentType="text/html"pageEncoding="UTF-8" import="java.sql.*"%><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>JSP page</title></head><body><center>分页显示记录内容<hr><table border="1" bgcolor="cccfff" align="center"><tr><th>工号</th><th>姓名</th><th>性别</th><th>工龄</th><th>工资</th></tr><%String oracledriver="oracle.jdbc.driver.OracleDriver";String oracleUrl="jdbc:oracle:thin:@localhost:1521:ORACLE";String username="scott";String password="tiger";Connection con;Class.forName(oracledriver);con=DriverManager.getConnection(oracleUrl,username,password);int intPageSize;int intRowCount;int intPageCount;int intPage;String strPage;int i;intPageSize = 10;strPage = request.getParameter("page");if(strPage==null){intPage =1;}else{intPage = ng.Integer.parseInt(strPage);if(intPage<1) intPage = 1;}Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);String sql = "select * from worker";ResultSet rs=stmt.executeQuery(sql);st();intRowCount = rs.getRow();intPageCount = (intRowCount+intPageSize-1)/intPageSize;if(intPage>intPageCount)intPage =intPageCount;if(intPageCount>0){rs.absolute((intPage-1)*intPageSize+1);i = 0;while(i<intPageSize && !rs.isAfterLast()){%><tr><td><%=rs.getString("id")%></td><td><%=rs.getString("name")%></td><td><%=rs.getString("sex")%></td><td><%=rs.getString("workyear")%></td><td><%=rs.getString("sal")%></td></tr><%rs.next();i++;}}%></table><hr><div align="center">第<%=intPage%>页共<%=intPageCount%>页<%if(intPage<intPageCount){%><a href="pagebreak.jsp?page=<%=intPage+1%>">下一页</a><%}if(intPage>1){%><a href="pagebreak.jsp?page=<%=intPage-1%>">上一页</a><%}rs.close();stmt.close();con.close();%></center></body></html>Input.jsp代码如下:<%@page contentType="text/html"pageEncoding="UTF-8"%><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>JSP中更新数据库</title></head><body bgcolor="ccceee"><br><br><br><center><form action="inputcheck.jsp" method="post"><table border ="0" width="200"><tr><td>工号</td><td><input type="text" name="id"></tr><tr><td>姓名</td><td><input type="text" name="name"></tr><tr><td>性别</td><td><input type="text" name="sex"></tr><tr><td>工龄</td><td><input type="text" name="workyear"></tr><tr><td>工资</td><td><input type="text" name="sal"></tr><tr align="center"><td colspan="2"><input name="sure" type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;<input name="clear" type="reset" value="取消"></td></table></form></center></body></html>Inputcheck.jsp 代码如下:<%@page contentType="text/html"pageEncoding="UTF-8" import="java.sql.*"%><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>JSP page</title></head><body><center><h5>已添加员工信息</h5><hr><%String empID = request.getParameter("empID");byte []b=empID.getBytes("ISO-8859-1");empID = new String(b,"UTF-8");String empName = request.getParameter("empName");byte []b1=empName.getBytes("ISO-8859-1");empName = new String(b1,"UTF-8");String empSex = request.getParameter("empSex");byte []b2=empSex.getBytes("ISO-8859-1");empSex = new String(b2,"UTF-8");String empWorkyear= request.getParameter("empWorkyear");byte []b3=empWorkyear.getBytes("ISO-8859-1");empWorkyear = new String(b3,"UTF-8");String empSal = request.getParameter("empSal");byte []b4=empSal.getBytes("ISO-8859-1");empSal = new String(b4,"UTF-8");String oracledriver="oracle.jdbc.driver.OracleDriver";String oracleUrl="jdbc:oracle:thin:@localhost:1521:ORACLE";String username="scott";String password="tiger";Class.forName(oracledriver);Connection con=DriverManager.getConnection(oracleUrl,username,password);Statement stmt=con.createStatement();String sql="insert into worker values('" +empID +"','"+empName+"','"+empSex+"',"+empWorkyear+","+empSal+")";stmt.executeUpdate(sql);stmt.close();con.close();%><h5>已删除员工信息</h5><hr><%String oracledriver1="oracle.jdbc.driver.OracleDriver";String oracleUrl1="jdbc:oracle:thin:@localhost:1521:ORACLE";String username1="scott";String password1="tiger";Class.forName(oracledriver1);Connection con1=DriverManager.getConnection(oracleUrl1,username1,password1);Statement stmt1=con1.createStatement();String sql1="delete from worker where empName='韩信'";stmt1.executeUpdate(sql1);stmt1.close();con1.close();%><h5>已更改员工信息</h5><hr><%String oracledriver2="oracle.jdbc.driver.OracleDriver";String oracleUrl2="jdbc:oracle:thin:@localhost:1521:ORACLE";String username2="scott";String password2="tiger";Class.forName(oracledriver2);Connection con2=DriverManager.getConnection(oracleUrl2,username2,password2);Statement stmt2=con2.createStatement();String sql2="update worker set empName='李莉莉' where empName='Kababa'";stmt2.executeUpdate(sql2);stmt2.close();con2.close();%><h5>经过以上操作后,Worker表如下:</h5><hr><table border=2 bgcolor="ccceee" align="center"><tr><td>编号</td><td>姓名</td><td>性别</td><td>工龄</td><td>工资</td></tr><%String oracledriver3="oracle.jdbc.driver.OracleDriver";String oracleUrl3="jdbc:oracle:thin:@localhost:1521:ORACLE";String username3="scott";String password3="tiger";Class.forName(oracledriver3);Connection con3=DriverManager.getConnection(oracleUrl3,username3,password3);Statement stmt3=con3.createStatement();String sql3="Select * from worker";ResultSet rs=stmt3.executeQuery(sql3);while(rs.next()){%><tr><td><%=rs.getString("empID")%></td><td><%=rs.getString("empName")%></td><td><%=rs.getString("empSex")%></td><td><%=rs.getString("empWorkyear")%></td><td><%=rs.getString("empSal")%></td></tr><%} rs.close();stmt3.close();con3.close();%></table></hr></center></body></html>。

相关主题