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

📄 connectiontest.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* Copyright  2002-2007 MySQL AB, 2008 Sun Microsystems This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as  published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL  as it is applied to this software. View the full text of the  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this  software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package testsuite.simple;import java.io.File;import java.io.FileWriter;import java.io.PrintStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.InetAddress;import java.net.NetworkInterface;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ParameterMetaData;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Savepoint;import java.sql.Statement;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.Properties;import java.util.StringTokenizer;import testsuite.BaseTestCase;import com.mysql.jdbc.NonRegisteringDriver;import com.mysql.jdbc.SQLError;import com.mysql.jdbc.StringUtils;import com.mysql.jdbc.Util;import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;import com.mysql.jdbc.log.StandardLogger;/** * Tests java.sql.Connection functionality ConnectionTest.java,v 1.1 2002/12/06 * 22:01:05 mmatthew Exp *  * @author Mark Matthews */public class ConnectionTest extends BaseTestCase {	/**	 * Constructor for ConnectionTest.	 * 	 * @param name	 *            the name of the test to run	 */	public ConnectionTest(String name) {		super(name);	}	/**	 * Runs all test cases in this test suite	 * 	 * @param args	 */	public static void main(String[] args) {		junit.textui.TestRunner.run(ConnectionTest.class);	}	/**	 * Tests catalog functionality	 * 	 * @throws Exception	 *             if an error occurs	 */	public void testCatalog() throws Exception {		String currentCatalog = this.conn.getCatalog();		this.conn.setCatalog(currentCatalog);		assertTrue(currentCatalog.equals(this.conn.getCatalog()));	}	/**	 * Tests a cluster connection for failover, requires a two-node cluster URL	 * specfied in com.mysql.jdbc.testsuite.ClusterUrl system proeprty.	 * 	 * @throws Exception	 *             DOCUMENT ME!	 */	public void testClusterConnection() throws Exception {		String url = System.getProperty("com.mysql.jdbc.testsuite.ClusterUrl");		if ((url != null) && (url.length() > 0)) {			Object versionNumObj = getSingleValueWithQuery("SHOW VARIABLES LIKE 'version'");			if ((versionNumObj != null)					&& (versionNumObj.toString().indexOf("cluster") != -1)) {				Connection clusterConn = null;				Statement clusterStmt = null;				try {					clusterConn = new NonRegisteringDriver().connect(url, null);					clusterStmt = clusterConn.createStatement();					clusterStmt							.executeQuery("DROP TABLE IF EXISTS testClusterConn");					clusterStmt							.executeQuery("CREATE TABLE testClusterConn (field1 INT) TYPE=ndbcluster");					clusterStmt							.executeQuery("INSERT INTO testClusterConn VALUES (1)");					clusterConn.setAutoCommit(false);					clusterStmt.executeQuery("SELECT * FROM testClusterConn");					clusterStmt							.executeUpdate("UPDATE testClusterConn SET field1=4");					// Kill the connection					String connectionId = getSingleValueWithQuery(							"SELECT CONNECTION_ID()").toString();					System.out							.println("Please kill the MySQL server now and press return...");					System.in.read();					System.out.println("Waiting for TCP/IP timeout...");					Thread.sleep(10);					System.out.println("Attempting auto reconnect");					try {						clusterConn.setAutoCommit(true);						clusterConn.setAutoCommit(false);					} catch (SQLException sqlEx) {						System.out.println(sqlEx);					}					//					// Test that this 'new' connection is not read-only					//					clusterStmt							.executeUpdate("UPDATE testClusterConn SET field1=5");					ResultSet rs = clusterStmt							.executeQuery("SELECT * FROM testClusterConn WHERE field1=5");					assertTrue("One row should be returned", rs.next());				} finally {					if (clusterStmt != null) {						clusterStmt								.executeQuery("DROP TABLE IF EXISTS testClusterConn");						clusterStmt.close();					}					if (clusterConn != null) {						clusterConn.close();					}				}			}		}	}	/**	 * DOCUMENT ME!	 * 	 * @throws Exception	 *             DOCUMENT ME!	 */	public void testDeadlockDetection() throws Exception {		try {			this.rs = this.stmt					.executeQuery("SHOW VARIABLES LIKE 'innodb_lock_wait_timeout'");			this.rs.next();			int timeoutSecs = this.rs.getInt(2);			this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");			this.stmt					.executeUpdate("CREATE TABLE t1 (id INTEGER, x INTEGER) TYPE=INNODB");			this.stmt.executeUpdate("INSERT INTO t1 VALUES(0, 0)");			this.conn.setAutoCommit(false);			this.conn.createStatement().executeQuery(					"SELECT * FROM t1 WHERE id=0 FOR UPDATE");			Properties props = new Properties();			props.setProperty("includeInnodbStatusInDeadlockExceptions", "true");						Connection deadlockConn = getConnectionWithProps(props);			deadlockConn.setAutoCommit(false);			// The following query should hang because con1 is locking the page			deadlockConn.createStatement().executeUpdate(					"UPDATE t1 SET x=2 WHERE id=0");			deadlockConn.commit();			Thread.sleep(timeoutSecs * 2 * 1000);		} catch (SQLException sqlEx) {			System.out					.println("Caught SQLException due to deadlock/lock timeout");			System.out.println("SQLState: " + sqlEx.getSQLState());			System.out.println("Vendor error: " + sqlEx.getErrorCode());			System.out.println("Message: " + sqlEx.getMessage());			//			// Check whether the driver thinks it really is deadlock...			//			assertTrue(SQLError.SQL_STATE_DEADLOCK.equals(sqlEx.getSQLState()));			assertTrue(sqlEx.getErrorCode() == 1205);			// Make sure INNODB Status is getting dumped into error message			assertTrue(sqlEx.getMessage().indexOf("INNODB MONITOR") != -1);		} finally {			this.conn.setAutoCommit(true);			this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");		}	}	/**	 * DOCUMENT ME!	 * 	 * @throws Exception	 *             DOCUMENT ME!	 */	public void testCharsets() throws Exception {		if (versionMeetsMinimum(4, 1)) {			try {				Properties props = new Properties();				props.setProperty("useUnicode", "true");				props.setProperty("characterEncoding", "UTF-8");				Connection utfConn = getConnectionWithProps(props);				this.stmt = utfConn.createStatement();				this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");				// this.stmt.executeUpdate("SET CHARACTER SET latin1");				this.stmt.executeUpdate("CREATE TABLE t1 ("						+ "comment CHAR(32) ASCII NOT NULL,"						+ "koi8_ru_f CHAR(32) CHARACTER SET koi8r NOT NULL"						+ ") CHARSET=latin5");				this.stmt						.executeUpdate("ALTER TABLE t1 CHANGE comment comment CHAR(32) CHARACTER SET latin2 NOT NULL");				this.stmt						.executeUpdate("ALTER TABLE t1 ADD latin5_f CHAR(32) NOT NULL");				this.stmt.executeUpdate("ALTER TABLE t1 CHARSET=latin2");				this.stmt						.executeUpdate("ALTER TABLE t1 ADD latin2_f CHAR(32) NOT NULL");				this.stmt						.executeUpdate("ALTER TABLE t1 DROP latin2_f, DROP latin5_f");				this.stmt						.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('a','LAT SMALL A')");				/*				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('b','LAT SMALL B')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('c','LAT SMALL C')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('d','LAT SMALL D')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('e','LAT SMALL E')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('f','LAT SMALL F')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('g','LAT SMALL G')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('h','LAT SMALL H')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('i','LAT SMALL I')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('j','LAT SMALL J')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('k','LAT SMALL K')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('l','LAT SMALL L')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('m','LAT SMALL M')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('n','LAT SMALL N')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('o','LAT SMALL O')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('p','LAT SMALL P')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('q','LAT SMALL Q')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('r','LAT SMALL R')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('s','LAT SMALL S')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('t','LAT SMALL T')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('u','LAT SMALL U')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('v','LAT SMALL V')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('w','LAT SMALL W')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('x','LAT SMALL X')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('y','LAT SMALL Y')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('z','LAT SMALL Z')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('A','LAT CAPIT A')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('B','LAT CAPIT B')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('C','LAT CAPIT C')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('D','LAT CAPIT D')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('E','LAT CAPIT E')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('F','LAT CAPIT F')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('G','LAT CAPIT G')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('H','LAT CAPIT H')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('I','LAT CAPIT I')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('J','LAT CAPIT J')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('K','LAT CAPIT K')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('L','LAT CAPIT L')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('M','LAT CAPIT M')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('N','LAT CAPIT N')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('O','LAT CAPIT O')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('P','LAT CAPIT P')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('Q','LAT CAPIT Q')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('R','LAT CAPIT R')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('S','LAT CAPIT S')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('T','LAT CAPIT T')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('U','LAT CAPIT U')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('V','LAT CAPIT V')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('W','LAT CAPIT W')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('X','LAT CAPIT X')"); this.stmt.executeUpdate("INSERT				 * INTO t1 (koi8_ru_f,comment) VALUES ('Y','LAT CAPIT Y')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES ('Z','LAT CAPIT Z')");				 */				String cyrillicSmallA = "\u0430";				this.stmt						.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment) VALUES ('"								+ cyrillicSmallA + "','CYR SMALL A')");				/*				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL BE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL VE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL GE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL DE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL IE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL IO')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL ZHE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL ZE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL I')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL KA')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL EL')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL EM')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL EN')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL O')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL PE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL ER')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL ES')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL TE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL U')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL EF')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL HA')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL TSE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL CHE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL SHA')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL SCHA')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL HARD SIGN')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL YERU')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL SOFT SIGN')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL E')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL YU')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR SMALL YA')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR CAPIT A')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)				 * VALUES (_koi8r'?��','CYR CAPIT BE')");				 * this.stmt.executeUpdate("INSERT INTO t1 (koi8_ru_f,comment)

⌨️ 快捷键说明

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