技术开发 频道

Jboss配置mysql数据库连接池


IT168技术文档】 
    6:Myeclispe 新建Web project 命名为:UseTest 
    新建JAVA类DatabaseConn.java
package com.db; import java.sql.*; import javax.naming.*; import javax.sql.DataSource; public class DatabaseConn { public static synchronized Connection getConnection() { try { Context envCtx = new InitialContext(); DataSource ds = (DataSource) envCtx.lookup("java:/test"); return ds.getConnection(); } catch (SQLException e) { System.out.println("数据源配置发生错误" + e.toString()); return null; } catch (NamingException e2) { System.out.print("数据源配置" + e2.toString()); return null; } } public static void close(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException ex) { } ; try { if (st != null) st.close(); } catch (SQLException ex) { } ; try { if (conn != null) conn.close(); } catch (SQLException ex) { } ; } }
    7:新建JSP页面:MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <%@ page import="java.sql.*"%> <%@ page import="com.db.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% Connection conn = DatabaseConn.getConnection(); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from test"); while(rs.next()) { out.println(rs.getInt("Test_id")); out.println(rs.getString("Test_name")); out.println(rs.getString("Test_password")); } DatabaseConn.close(rs,stmt,conn); %> </body> </html>
    8 :部署Web project 
    9:重新启动服务器 
    10:访问: http://127.0.0.1:8080/UseTest/MyJsp.jsp
0
相关文章