master
X’moe 9 years ago
commit 790f251380

Binary file not shown.

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/derbyclient.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/mysql-connector-java-5.1.7-bin.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/ejb3/ejb3-persistence.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Logging/commons-logging-1.2-javadoc.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Logging/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/classmate-1.3.0.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/geronimo-jta_1.1_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/hibernate-commons-annotations-5.0.1.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/hibernate-core-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/hibernate-jpa-2.1-api-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/jandex-2.0.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/javassist-3.20.0-GA.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/required/jboss-logging-3.3.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/osgi/hibernate-osgi-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/osgi/org.osgi.compendium-4.3.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/osgi/org.osgi.core-4.3.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/c3p0/c3p0-0.9.2.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/c3p0/hibernate-c3p0-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/c3p0/mchange-commons-java-0.2.3.4.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_91]">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/jpa/hibernate-entitymanager-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/jpa-metamodel-generator/hibernate-jpamodelgen-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/hibernate-infinispan-5.1.0.Final-tests.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/hibernate-infinispan-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/infinispan-commons-8.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/infinispan-core-8.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/jboss-marshalling-osgi-1.4.10.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/jboss-transaction-api_1.1_spec-1.0.1.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/infinispan/jgroups-3.6.4.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/ehcache/ehcache-2.10.1.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/ehcache/hibernate-ehcache-5.1.0.Final.jar"/>
<classpathentry kind="lib" path="/Users/xmoe/Documents/JavaWorker/BusinessProject/lib/Hibernate/optional/ehcache/slf4j-api-1.7.7.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BusinessProject</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="WebContent"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="BusinessProject">
<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="BusinessProject"/>
<property name="java-output-path" value="/BusinessProject/bin"/>
</wb-module>
</project-modules>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v7.0"/>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary

Binary file not shown.

Binary file not shown.

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jndi/mysql1"
auth="Container"
type="javax.sql.DataSource"
factory="org.objectweb.jndi.DataSourceFactory"
username="root"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/TestDb"
maxActive="30"
maxIdle="30"
/>
<Resource name="jndi/mysql2"
auth="Container"
type="javax.sql.DataSource"
factory="org.objectweb.jndi.DataSourceFactory"
username="root"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.16.129.197:3306/TestDb"
maxActive="30"
maxIdle="30"
/>
</Context>

Binary file not shown.

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>BusinessProjectWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>WebMain</servlet-name>
<servlet-class>WebMain</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebMain</servlet-name>
<url-pattern>/WebMain</url-pattern>
</servlet-mapping>
</web-app>

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="ProjectMain.TInput" table="t_input" catalog="jpa_inst">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="TUser" class="ProjectMain.TUser" fetch="select">
<column name="operator" length="10" not-null="true" />
</many-to-one>
<property name="quantity" type="java.lang.Integer">
<column name="quantity" not-null="true" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="19" not-null="true" />
</property>
<property name="productId" type="java.lang.String">
<column name="product_id" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="ProjectMain.TInventory" table="t_inventory" catalog="jpa_inst">
<id name="productId" type="java.lang.String">
<column name="product_id" length="10" />
<generator class="assigned"></generator>
</id>
<property name="quantity" type="java.lang.Integer">
<column name="quantity" />
</property>
</class>
</hibernate-mapping>

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="ProjectMain.TOutput" table="t_output" catalog="jpa_inst">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="TUser" class="ProjectMain.TUser" fetch="select">
<column name="operator" length="10" not-null="true" />
</many-to-one>
<property name="quantity" type="java.lang.Integer">
<column name="quantity" not-null="true" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="19" not-null="true" />
</property>
<property name="productId" type="java.lang.String">
<column name="product_id" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="ProjectMain.TUser" table="t_user" catalog="TestDb">
<id name="userid" type="java.lang.String">
<column name="userid" length="10" />
<generator class="assigned"></generator>
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" not-null="true" />
</property>
<set name="TInputs" inverse="true">
<key>
<column name="operator" length="10" not-null="true" />
</key>
<one-to-many class="ProjectMain.TInput" />
</set>
<set name="TOutputs" inverse="true">
<key>
<column name="operator" length="10" not-null="true" />
</key>
<one-to-many class="ProjectMain.TOutput" />
</set>
<set name="TOutputs_1" inverse="true">
<key>
<column name="operator" length="10" not-null="true" />
</key>
<one-to-many class="ProjectMain.TOutput" />
</set>
<set name="TInputs_1" inverse="true">
<key>
<column name="operator" length="10" not-null="true" />
</key>
<one-to-many class="ProjectMain.TInput" />
</set>
</class>
</hibernate-mapping>

Binary file not shown.

Binary file not shown.

@ -0,0 +1,64 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/inventory
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
-->
<!--C3P0配置 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="automaticTestTable">Test</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">120</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="c3p0.idleConnectionTestPeriod">18000</property>
<property name="c3p0.maxIdleTime">25000</property>
<property name="c3p0.idle_test_period">120</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost/inventory
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.profile">TestDb</property>
<property name="show_sql">false</property>
<mapping resource="ProjectMain/TOutput.hbm.xml" />
<mapping resource="ProjectMain/TInput.hbm.xml" />
<mapping resource="ProjectMain/TUser.hbm.xml" />
<mapping class="ProjectMain.TOutput" />
<mapping class="ProjectMain.TInput" />
<mapping class="ProjectMain.TUser" />
<mapping resource="ProjectMain/TInventory.hbm.xml" />
<mapping class="ProjectMain.TInventory" />
</session-factory>
</hibernate-configuration>

Binary file not shown.

Binary file not shown.

@ -0,0 +1,34 @@
<features xmlns='http://karaf.apache.org/xmlns/features/v1.2.0' name='hibernate-osgi'>
<feature name='hibernate-orm' description='Combines all Hibernate core dependencies and required modules into a single feature' version='5.1.0.Final'>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5</bundle>
<bundle>mvn:org.jboss.logging/jboss-logging/3.3.0.Final</bundle>
<bundle dependency='true'>mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final</bundle>
<bundle>mvn:org.javassist/javassist/3.20.0-GA</bundle>
<bundle dependency='true'>mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1</bundle>
<bundle>mvn:org.jboss/jandex/2.0.0.Final</bundle>
<bundle>mvn:com.fasterxml/classmate/1.3.0</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5</bundle>
<bundle>mvn:org.hibernate.common/hibernate-commons-annotations/5.0.1.Final</bundle>
<bundle>mvn:org.hibernate/hibernate-core/5.1.0.Final</bundle>
<bundle>mvn:org.hibernate/hibernate-entitymanager/5.1.0.Final</bundle>
<bundle>mvn:org.hibernate/hibernate-osgi/5.1.0.Final</bundle>
</feature>
<feature name='hibernate-envers' description='Feature for easily adding Envers support to hibernate-orm' version='5.1.0.Final'>
<feature>hibernate-orm</feature>
<bundle>mvn:org.hibernate/hibernate-envers/5.1.0.Final</bundle>
</feature>
<feature name='hibernate-infinispan' description='Feature for easily adding Infinispan-based caching support to hibernate-orm' version='5.1.0.Final'>
<feature>hibernate-orm</feature>
<bundle dependency='true'>mvn:org.infinispan/infinispan-commons/8.1.0.Final</bundle>
<bundle dependency='true'>mvn:org.jgroups/jgroups/3.6.4.Final</bundle>
<bundle>mvn:org.jboss.marshalling/jboss-marshalling-osgi/1.4.10.Final</bundle>
<bundle>mvn:org.infinispan/infinispan-core/8.1.0.Final</bundle>
<bundle>mvn:org.hibernate/hibernate-infinispan/5.1.0.Final</bundle>
</feature>
<feature name='hibernate-ehcache' description='Feature for easily adding Ehcache-based caching support to hibernate-orm' version='5.1.0.Final'>
<feature>hibernate-orm</feature>
<bundle>mvn:org.slf4j/slf4j-api/1.7.7</bundle>
<bundle dependency='true'>mvn:net.sf.ehcache/ehcache/2.10.1</bundle>
<bundle>mvn:org.hibernate/hibernate-ehcache/5.1.0.Final</bundle>
</feature>
</features>

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,91 @@
package DataBase;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbPool
{
private static DbPool dbPool;
private ComboPooledDataSource dataSource;
static
{
dbPool = new DbPool();
}
public DbPool()
{
try
{
dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/testDb?"
+ "autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
System.out.println("Connected...");
}
catch (PropertyVetoException e)
{
System.out.println("Couldn't connect...");
throw new RuntimeException(e);
}
}
public final static DbPool getInstance()
{
return dbPool;
}
public final Connection getConnection()
{
try
{
System.out.println("Getting connection...");
return dataSource.getConnection();
}
catch (SQLException e)
{
System.out.println("Failed to get connection...");
throw new RuntimeException("无法从数据源获取连接 ", e);
}
}
public static void main(String[] args) throws SQLException
{
Connection con = null;
try
{
con = DbPool.getInstance().getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from t_user;");
while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.getStatement().close();
}
catch (Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if (con != null)
con.close();
}
}
}

@ -0,0 +1,143 @@
package DataBase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
public class JTAConnManager
{
private static JTAConnManager instance;
private DataSource dataSource1;
private DataSource dataSource2;
// private DataSource dataSource;
static
{
instance = new JTAConnManager();
}
public JTAConnManager()
{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial ", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url ", "localhost:8080");
Context ctx = null;
try
{
ctx = new InitialContext();
}
catch (NamingException e)
{
// TODO Auto-generated catch block
System.err.println("Failed to create context!");
e.printStackTrace();
}
try
{
dataSource1 = (DataSource) ctx.lookup("java:comp/env/jndi/mysql1");
dataSource2 = (DataSource) ctx.lookup("java:comp/env/jndi/mysql2");
}
catch (NamingException e)
{
// TODO Auto-generated catch block
System.err.println("Failed to find context");
e.printStackTrace();
}
}
public final static JTAConnManager getInstance()
{
return instance;
}
public final Connection getConnection1()
{
try
{
return dataSource1.getConnection();
}
catch (SQLException e)
{
throw new RuntimeException("无法从数据源获取连接 database1", e);
}
}
public final Connection getConnection2()
{
try
{
return dataSource2.getConnection();
}
catch (SQLException e)
{
throw new RuntimeException("无法从数据源获取连接 database2", e);
}
}
public void TestConn1() throws SQLException
{
Connection con = null;
try
{
con = JTAConnManager.getInstance().getConnection1();
ResultSet rs = con.createStatement().executeQuery("select * from t_user");
while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.getStatement().close();
}
catch (Exception e)
{
System.err.println("Connecting(1)...error");
e.printStackTrace();
}
finally
{
if (con != null)
con.close();
}
}
public void TestConn2() throws SQLException
{
Connection con = null;
try
{
con = JTAConnManager.getInstance().getConnection2();
ResultSet rs = con.createStatement().executeQuery("select * from t_user");
while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.getStatement().close();
}
catch (Exception e)
{
System.err.println("Connecting(2)...error");
e.printStackTrace();
}
finally
{
if (con != null)
con.close();
}
}
}

@ -0,0 +1,80 @@
package DataBase;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class PooledConnManager {
private static PooledConnManager dbPool;
private ComboPooledDataSource dataSource;
static {
dbPool = new PooledConnManager();
// try {
// dbPool = new DbPool();
// } catch (NamingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
public PooledConnManager(){
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/TestDb?"
+ "autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
/*public DbPool() throws NamingException {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jndi/mysql");
}*/
public final static PooledConnManager getInstance() {
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接 ", e);
}
}
public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = PooledConnManager.getInstance().getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from t_user");
while(rs.next()){
System.out.println(rs.getString(1));
}
rs.getStatement().close();
} catch (Exception e) {
} finally {
if (con != null)
con.close();
}
}
}

@ -0,0 +1,107 @@
package DataBase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
//import sun.jdbc.odbc.ee.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
//import sun.jdbc.odbc.ee.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
public class XAConnManager
{
private static XAConnManager instance;
private MysqlXADataSource dataSource1;
private MysqlXADataSource dataSource2;
// private DataSource dataSource;
static
{
instance = new XAConnManager();
// try {
// dbPool = new DbPool();
// } catch (NamingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
public XAConnManager()
{
dataSource1 = new MysqlXADataSource();
dataSource1.setUser("root");
dataSource1.setPassword("123456");
dataSource1.setUrl("jdbc:mysql://127.0.0.1:3306/TestDb?"
+ "autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
dataSource2 = new MysqlXADataSource();
dataSource2.setUser("root");
dataSource2.setPassword("123456");
dataSource2.setUrl("jdbc:mysql://172.16.129.197:3306/TestDb2?"
+ "autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
}
public final static XAConnManager getInstance()
{
return instance;
}
public final Connection getConnection1()
{
try
{
return dataSource1.getConnection();
}
catch (SQLException e)
{
throw new RuntimeException("无法从数据源获取连接 database1", e);
}
}
public final Connection getConnection2()
{
try
{
return dataSource2.getConnection();
}
catch (SQLException e)
{
throw new RuntimeException("无法从数据源获取连接 database2", e);
}
}
public static void main(String[] args) throws SQLException
{
Connection con = null;
try
{
con = XAConnManager.getInstance().getConnection2();
ResultSet rs = con.createStatement().executeQuery("select * from t_user");
while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.getStatement().close();
}
catch (Exception e)
{
System.err.println("Connecting...error");
e.printStackTrace();
}
finally
{
if (con != null)
con.close();
}
}
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save