標籤:
package com.hanqi.test;import java.sql.*;public class XveSheng { Connection conn; Statement st; ResultSet rs; public Connection getConn() { return conn; } public void setConn(Connection conn) { this.conn = conn; } public Statement getSt() { return st; } public void setSt(Statement st) { this.st = st; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } public void lianjie() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); String strUrl = "jdbc:oracle:thin:@localhost:1521:orcl" ; conn = DriverManager.getConnection(strUrl, "test", "5211314"); System.out.println("串連資料庫成功!"); } catch (Exception e) { e.printStackTrace(); } } public void guanbi() { try { conn.close(); st.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
<%@page import="com.hanqi.test.XveSheng"%><%@page import="java.sql.*"%><%@ page language="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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>學生資訊</title></head><body><div align="center"><font size="20px">學生資訊表</font></div><table border="1" cellspacing="0" width="80%" align="center" style="text-align: center"><tr><td height="40">學號</td><td >姓名</td><td >性別</td><td >生日</td><td>班級</td></tr><% XveSheng xs = new XveSheng(); 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><td height="40"><%=sno %></td><td ><%=sname %></td><td ><%=ssex %></td><td ><%=sbirthday.toString() %></td><td><%=sclass %></td></tr> <% } xs.guanbi();%></table></body></html>
用JavaBean實現資料庫的串連和關閉,在jsp頁面輸出資料庫中student表中學生的資訊