⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkdatasource.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		System.out.println("XADataSource - attributesAsPassword=true");		xads.setAttributesAsPassword(true);		dsConnectionRequests((XADataSource) xads);		xads.setAttributesAsPassword(false);		System.out.println("XADataSource - databaseName=wombat, attributesAsPassword=true");		xads.setDatabaseName("wombat");		xads.setAttributesAsPassword(true);		dsConnectionRequests((XADataSource) xads);		xads.setAttributesAsPassword(false);		xads.setDatabaseName(null);	}	private static void dsConnectionRequests(DataSource ds) {		try {			Connection c1 = ds.getConnection();			System.out.println("  getConnection() - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection(null, null);			System.out.println("  getConnection(null, null) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection("fred", null);			System.out.println("  getConnection(fred, null) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection("fred", "wilma");			System.out.println("  getConnection(fred, wilma) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection(null, "wilma");			System.out.println("  getConnection(null, wilma) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection(null, "databaseName=wombat");			System.out.println("  getConnection(null, databaseName=wombat) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection("fred", "databaseName=wombat");			System.out.println("  getConnection(fred, databaseName=wombat) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection("fred", "databaseName=wombat;password=wilma");			System.out.println("  getConnection(fred, databaseName=wombat;password=wilma) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			Connection c1 = ds.getConnection("fred", "databaseName=wombat;password=betty");			System.out.println("  getConnection(fred, databaseName=wombat;password=betty) - OK");			c1.close();		} catch (SQLException sqle) {			System.out.println("  getConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}	}	private static void dsConnectionRequests(ConnectionPoolDataSource ds) {		try {			PooledConnection pc = ds.getPooledConnection();			System.out.println("  getPooledConnection() - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection(null, null);			System.out.println("  getPooledConnection(null, null) - OK");			Connection c1 = pc.getConnection();			c1.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection("fred", null);			System.out.println("  getPooledConnection(fred, null) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection("fred", "wilma");			System.out.println("  getPooledConnection(fred, wilma) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection(null, "wilma");			System.out.println("  getPooledConnection(null, wilma) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection(null, "databaseName=wombat");			System.out.println("  getPooledConnection(null, databaseName=wombat) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat");			System.out.println("  getPooledConnection(fred, databaseName=wombat) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat;password=wilma");			System.out.println("  getPooledConnection(fred, databaseName=wombat;password=wilma) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			PooledConnection pc = ds.getPooledConnection("fred", "databaseName=wombat;password=betty");			System.out.println("  getPooledConnection(fred, databaseName=wombat;password=betty) - OK");			Connection c1 = pc.getConnection();			c1.close();			pc.close();		} catch (SQLException sqle) {			System.out.println("  getPooledConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}	}	private static void dsConnectionRequests(XADataSource ds) {		try {			XAConnection xc = ds.getXAConnection();			System.out.println("  getXAConnection() - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection(null, null);			System.out.println("  getXAConnection(null, null) - OK");			Connection c1 = xc.getConnection();			c1.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection("fred", null);			System.out.println("  getXAConnection(fred, null) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection("fred", "wilma");			System.out.println("  getXAConnection(fred, wilma) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection(null, "wilma");			System.out.println("  getXAConnection(null, wilma) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection(null, "databaseName=wombat");			System.out.println("  getXAConnection(null, databaseName=wombat) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat");			System.out.println("  getXAConnection(fred, databaseName=wombat) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat;password=wilma");			System.out.println("  getXAConnection(fred, databaseName=wombat;password=wilma) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}		try {			XAConnection xc = ds.getXAConnection("fred", "databaseName=wombat;password=betty");			System.out.println("  getXAConnection(fred, databaseName=wombat;password=betty) - OK");			Connection c1 = xc.getConnection();			c1.close();			xc.close();		} catch (SQLException sqle) {			System.out.println("  getXAConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );		}	}	protected Xid getXid(int xid, byte b1, byte b2) {		return new cdsXid(xid, b1, b2);	}	public static String translateIso(int iso)	{		switch(iso)		{		case Connection.TRANSACTION_READ_COMMITTED: return "READ_COMMITTED";		case Connection.TRANSACTION_SERIALIZABLE: return "SERIALIZABLE";		case Connection.TRANSACTION_REPEATABLE_READ: return "REPEATABLE_READ";		case Connection.TRANSACTION_READ_UNCOMMITTED: return "READ_UNCOMMITTED";		}		return "unknown";	}	/**		When a connection is being pooled, the underlying JDBC embedded		connection object is re-used. As each application gets a new		Connection object, that is really a wrapper around the old connection		it should reset any connection spoecific state on the embedded connection		object.	*/	private static void testPoolReset(String type, PooledConnection pc) throws SQLException	{		System.out.println("Start testPoolReset " + type);		testPoolResetWork("C", pc.getConnection());		testPoolResetWork("", pc.getConnection());		testPoolResetWork("D", pc.getConnection());		pc.close();		System.out.println("End testPoolReset " + type);	}	private static void testPoolResetWork(String tableAction, Connection conn) throws SQLException	{		Statement s = conn.createStatement();		if (tableAction.equals("C"))		{			s.execute("CREATE TABLE testPoolResetWork (id int generated always as identity, name varchar(25))");		}		ResultSet rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");		rs.next();		String val = rs.getString(1);		if (!rs.wasNull() || (val != null))			System.out.println("FAIL - initial call to IDENTITY_VAL_LOCAL is not NULL!" + val);		rs.close();		s.executeUpdate("INSERT INTO testPoolResetWork(name) values ('derby-222')");		rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");		rs.next();		val = rs.getString(1);		System.out.println("IDENTITY_VAL_LOCAL=" + val);		rs.close();		if (tableAction.equals("D"))		{			s.execute("DROP TABLE testPoolResetWork");		}		s.close();		conn.close();	}	public void testJira95ds(Connection conn, String dbName) throws SQLException	{		System.out.print("\ntesting jira 95 for DataSource");		EmbeddedDataSource ds = new EmbeddedDataSource();		ds.setDatabaseName(dbName);		Connection conn1 = ds.getConnection();		conn1.close();	}		public void testJira95xads(Connection conn, String dbName) throws SQLException	{		System.out.print("testing jira 95 for XADataSource");		EmbeddedXADataSource dxs = new EmbeddedXADataSource();		dxs.setDatabaseName(dbName);		Connection conn2 = dxs.getXAConnection().getConnection();		conn2.close();	}}class cdsXid implements Xid, Serializable{  private static final long serialVersionUID = 64467338100036L;	private final int format_id;	private byte[] global_id;	private byte[] branch_id;	cdsXid(int xid, byte b1, byte b2)	{		format_id = xid;		global_id = new byte[Xid.MAXGTRIDSIZE];		branch_id = new byte[Xid.MAXBQUALSIZE];		for (int i = 0; i < global_id.length; i++) {			global_id[i] = b1;		}		for (int i = 0; i < branch_id.length; i++) {			branch_id[i] = b2;		}	}    /**     * Obtain the format id part of the Xid.     * <p>     *     * @return Format identifier. O means the OSI CCR format.     **/    public int getFormatId()    {        return(format_id);    }    /**     * Obtain the global transaction identifier part of XID as an array of      * bytes.     * <p>     *	 * @return A byte array containing the global transaction identifier.     **/    public byte[] getGlobalTransactionId()    {        return(global_id);    }    /**     * Obtain the transaction branch qualifier part of the Xid in a byte array.     * <p>     *	 * @return A byte array containing the branch qualifier of the transaction.     **/    public byte[] getBranchQualifier()    {        return(branch_id);    }}class EventCatcher implements ConnectionEventListener{	private final int catcher;	EventCatcher(int which) {		catcher=which;	}	// ConnectionEventListener methods	public void connectionClosed(ConnectionEvent event)	{		System.out.print("EVENT("+catcher+"):connectionClosed");		SQLException sqle = event.getSQLException();		if (sqle != null)			System.out.print(" SQLSTATE=" + sqle.getSQLState());		System.out.println("");	}	public void connectionErrorOccurred(ConnectionEvent event)	{		System.out.print("EVENT("+catcher+"):connectionErrorOccurred");		SQLException sqle = event.getSQLException();		if (sqle != null)			System.out.print(" SQLSTATE=" + sqle.getSQLState());		System.out.println("");	}}

⌨️ 快捷键说明

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