getconnection.java

来自「THIS IS A FINANCIAL BANK APPLICATION」· Java 代码 · 共 53 行

JAVA
53
字号
package ma.bmce.tools;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class GetConnection {

	/*
	 * il faut modifier: set the "Debug" property to "false" in the
	 * CachedConnectionManager mbean configuration, in the jbossjca-service.xml file
	 */

	public Connection openConnection() {

		DataSource ds = null;
		Connection conn = null;

		try {

			InitialContext ic = new InitialContext();

			ds = (DataSource) ic.lookup("java:/PcardgDS");

		} catch (NamingException ex) {

			throw new IllegalStateException(ex);

		}

		try {
			if (ds != null) {
				System.out.println("ds non null");
				conn = ds.getConnection();
				System.out.println("connexion ok");

			} else
				System.out.println("ds  null");

			return conn;

		} catch (SQLException ex) {

			throw new IllegalStateException(ex);

		}

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?