Packagecom.hanqi.test;ImportJava.sql.*; Public classXvesheng {Connection conn; Statement St; ResultSet rs; PublicConnection Getconn () {returnConn; } Public voidSetconn (Connection conn) { This. conn =Conn; } PublicStatement getst () {returnSt; } Public voidsetst (Statement St) { This. St =St; } PublicResultSet getrs () {returnrs; } Public voidSetrs (ResultSet rs) { This. rs =rs; } Public voidLianjie () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); String strURL= "Jdbc:oracle:thin: @localhost: 1521:orcl" ; Conn= Drivermanager.getconnection (strURL, "test", "5211314"); System.out.println ("Connection to the database was successful!" "); } Catch(Exception e) {e.printstacktrace (); } } Public voidGuanbi () {Try{conn.close (); St.close (); Rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } }
<% @pageImport= "Com.hanqi.test.XveSheng"%><% @pageImport= "java.sql.*"%><%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Student Information</title></Head><Body><DivAlign= "Center"><Fontsize= "20px">Student Information Form</Font></Div><TableBorder= "1"cellspacing= "0"width= "80%"Align= "Center"style= "Text-align:center"><TR><TDHeight= "Max">School Number</TD><TD>Name</TD><TD>Gender</TD><TD>Birthday</TD><TD>Class</TD></TR><% XveshengXS= newXvesheng (); Xs.lianjie (); XS.SETST (Xs.getconn (). createstatement ()); Xs.setrs (XS.GETST (). ExecuteQuery ("SELECT * from Student")); while (Xs.getrs (). Next ()) {String sno= Xs.getrs (). getString (1); String sname= Xs.getrs (). getString (2); String Ssex= Xs.getrs (). getString (3); Date Sbirthday= Xs.getrs (). GetDate (4); String Sclass= Xs.getrs (). getString (5); %> <TR><TDHeight= "Max"><%=sno%></TD><TD><%=sname%></TD><TD><%=ssex%></TD><TD><%=sbirthday.tostring() %></TD><TD><%=sclass%></TD></TR> <%} xs.guanbi();%></Table></Body></HTML>
Using JavaBean to implement database connection and shutdown, Student table in JSP page output database information of middle school students