📄 connectionpoolservlet.java
字号:
package connectionpoolservlet;import java.io.*;import java.sql.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;/** * Title: Servlet使用数据库连接池 * Description: 教学示范 * Copyright: Copyright (c) 2003 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class connectionpoolServlet extends HttpServlet { static final private String CONTENT_TYPE = "text/html; charset=GBK"; private connectionPoolManager con_PM; //定义Servlet使用的连接池管理器 connectionWithIdx con = new connectionWithIdx(); //定义一个带有索引的数据库连接类 private ResultSet rs = null; //定义数据库查询的结果集 private Statement statement = null; //定义查询数据库的Statement对象 public void init() throws ServletException { //初始化Servlet con_PM = connectionPoolManager.getInstance(); //获取连接池管理器的唯一实例 } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>connectionpoolServlet</title></head>"); out.println("<body>"); out.println("<h2 align=center>学生基本信息表中的数据</h2>"); try { con = con_PM.getConnection(); //从连接池管理器中得到数据库的连接 if (con==null) //若连接为空,则连接池中的连接已达上限 { out.println("<p align=center>服务器已达到最大连接,请稍后再使。</p>"); } else { statement = con.connection.createStatement(); //创建Statement接口实例 String sql = "select * from studentbase"; //创建取出studentbase表中所有数据的SQL语句 rs = statement.executeQuery(sql); //将数据存入结果集中 ResultSetMetaData rsData = rs.getMetaData(); //获取结果集信息 out.println("<table width=75% border=1 align=center><tr>"); for(int i=1; i<=rsData.getColumnCount(); i++) //输出字段名 { out.println("<td>" + rsData.getColumnLabel(i) + "</td>"); } out.println("</tr>"); while(rs.next()) //输出表中数据 { out.println("<tr>"); for(int i=1; i<=rsData.getColumnCount();i++) { out.println("<td>" + rs.getString(i) + "</td>"); } out.println("</tr>"); } out.println("</table>"); } } catch(SQLException ex){ //捕捉异常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } out.println("</body></html>"); con_PM.freeConnection(con.idx); //将使用的数据库连接返还给连接池管理器 } public void destroy() { //当Servlet关闭时释放资源 try { if(statement != null) { statement.close(); //关闭Statement接口实例 } } catch (SQLException ex) { System.out.println("\nERROR:----- SQLException -----\n"); System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); } super.destroy(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -