GlassFish MySql连接池配置
第一步:复制D:\glassfish\lib\install\templates\resources\jdbc\mysql_type4_datasource.xml
到D:\glassfish\domains\s317\config
[注:s317是 domain名称] , 并命名为msql.xml
第二步:编缉D:\glassfish\domains\s317\config\msql.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<jdbc-connection-pool
name="mysql_pool"
datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
res-type="javax.sql.DataSource">
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/s317"/>
</jdbc-connection-pool>
<jdbc-resource
enabled="true"
jndi-name="jdbc/MySqlDB"
object-type="user"
pool-name=" mysql_pool "/>
</resources>
第三步:复制mysql-connector-java-3.1.10-bin.jar到
D:\glassfish\domains\s317\lib\ext 目录下
第四步:D:\glassfish\bin>asadmin start-domain s317
第五步:asadmin add-resources msyql.xml
第六步:asadmin ping-connection-pool mysql_pool
第七步:向web.xml加入下面的内容
<resource-ref>
<description>DBPool Connection</description>
<res-ref-name>jdbc/MySqlDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
第八步:编写测试页面jdbc.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try{
String strJNDIName = "jdbc/MySqlDB";
Context initialcontext = null;
initialcontext = new InitialContext();
DataSource datasource = (DataSource) initialcontext.lookup(strJNDIName);
conn = datasource.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from jbpm_task");
while(rs.next()){
out.println(rs.getString("ID_"));
}
}
catch(Exception e){
System.out.println(e.toString());
}finally{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn= null;
}
}
%>
</body>
</html>