📄 connectiontest.java
字号:
rsmd = this.rs.getMetaData(); numColumns = rsmd.getColumnCount(); for (int i = 0; i < numColumns; i++) { System.out.print(rsmd.getColumnName(i + 1)); System.out.print("\t\t"); } System.out.println(); while (this.rs.next()) { System.out.println(this.rs.getString(1).equals("\u0430") + "\t\t"); System.out.println(new String(this.rs.getBytes(1), "KOI8_R")); } char[] c = new char[] {0xd0b0}; System.out.println(new String(c)); System.out.println("\u0430"); } finally { //this.stmt.executeUpdate("DROP TABLE IF EXISTS t1"); } } } /** * Tests isolation level functionality * * @throws Exception if an error occurs */ public void testIsolationLevel() throws Exception { if (versionMeetsMinimum(4, 0)) { String[] isoLevelNames = new String[] { "Connection.TRANSACTION_NONE", "Connection.TRANSACTION_READ_COMMITTED", "Connection.TRANSACTION_READ_UNCOMMITTED", "Connection.TRANSACTION_REPEATABLE_READ", "Connection.TRANSACTION_SERIALIZABLE" }; int[] isolationLevels = new int[] { Connection.TRANSACTION_NONE, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE }; DatabaseMetaData dbmd = this.conn.getMetaData(); for (int i = 0; i < isolationLevels.length; i++) { if (dbmd.supportsTransactionIsolationLevel(isolationLevels[i])) { this.conn.setTransactionIsolation(isolationLevels[i]); assertTrue("Transaction isolation level that was set (" + isoLevelNames[i] + ") was not returned, nor was a more restrictive isolation level used by the server", this.conn.getTransactionIsolation() == isolationLevels[i] || this.conn.getTransactionIsolation() > isolationLevels[i]); } } } } /** * Tests the savepoint functionality in MySQL. * * @throws Exception if an error occurs. */ public void testSavepoint() throws Exception { DatabaseMetaData dbmd = this.conn.getMetaData(); if (dbmd.supportsSavepoints()) { System.out.println("Testing SAVEPOINTs"); try { this.conn.setAutoCommit(true); this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints"); this.stmt.executeUpdate( "CREATE TABLE testSavepoints (field1 int) TYPE=InnoDB"); // Try with named save points this.conn.setAutoCommit(false); this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)"); Savepoint afterInsert = this.conn.setSavepoint("afterInsert"); this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2"); Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate"); this.stmt.executeUpdate("DELETE FROM testSavepoints"); assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0); this.conn.rollback(afterUpdate); assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1); assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null) .toString())); this.conn.rollback(afterInsert); assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null) .toString())); this.conn.rollback(); assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0); // Try with 'anonymous' save points this.conn.rollback(); this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)"); afterInsert = this.conn.setSavepoint(); this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2"); afterUpdate = this.conn.setSavepoint(); this.stmt.executeUpdate("DELETE FROM testSavepoints"); assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0); this.conn.rollback(afterUpdate); assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1); assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null) .toString())); this.conn.rollback(afterInsert); assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null) .toString())); this.conn.rollback(); this.conn.releaseSavepoint(this.conn.setSavepoint()); } finally { this.conn.setAutoCommit(true); this.stmt.executeUpdate("DROP TABLE IF EXISTS testSavepoints"); } } else { System.out.println("MySQL version does not support SAVEPOINTs"); } } /** * Tests the ability to set the connection collation via properties. * * @throws Exception if an error occurs or the test fails */ public void testNonStandardConnectionCollation() throws Exception { if (versionMeetsMinimum(4, 1)) { String collationToSet = "utf8_bin"; String characterSet = "utf8"; Properties props = new Properties(); props.setProperty("connectionCollation", collationToSet); props.setProperty("characterEncoding", characterSet); Connection collConn = null; Statement collStmt = null; ResultSet collRs = null; try { collConn = getConnectionWithProps(props); collStmt = collConn.createStatement(); collRs = collStmt.executeQuery("SHOW VARIABLES LIKE 'collation_connection'"); assertTrue(collRs.next()); assertTrue(collationToSet.equalsIgnoreCase(collRs.getString(2))); } finally { if (collConn != null) { collConn.close(); } } } } public void testDumpQueriesOnException() throws Exception { Properties props = new Properties(); props.setProperty("dumpQueriesOnException", "true"); String bogusSQL = "SELECT 1 TO BAZ"; Connection dumpConn = getConnectionWithProps(props); try { dumpConn.createStatement().executeQuery(bogusSQL); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); } try { ((com.mysql.jdbc.Connection)dumpConn).clientPrepareStatement(bogusSQL).executeQuery(); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); } try { this.stmt.executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException"); this.stmt.executeUpdate("CREATE TABLE testDumpQueriesOnException (field1 int UNIQUE)"); this.stmt.executeUpdate("INSERT INTO testDumpQueriesOnException VALUES (1)"); PreparedStatement pStmt = dumpConn.prepareStatement("INSERT INTO testDumpQueriesOnException VALUES (?)"); pStmt.setInt(1, 1); pStmt.executeUpdate(); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage().indexOf("INSERT INTO testDumpQueriesOnException") != -1); } finally { this.stmt.executeUpdate("DROP TABLE IF EXISTS testDumpQueriesOnException"); } try { dumpConn.prepareStatement(bogusSQL); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage().indexOf(bogusSQL) != -1); } } /** * Tests functionality of the ConnectionPropertiesTransform interface. * * @throws Exception if the test fails. */ public void testConnectionPropertiesTransform() throws Exception { String transformClassName = SimpleTransformer.class.getName(); Properties props = new Properties(); props.setProperty(NonRegisteringDriver.PROPERTIES_TRANSFORM_KEY, transformClassName); NonRegisteringDriver driver = new NonRegisteringDriver(); Properties transformedProps = driver.parseURL(BaseTestCase.dbUrl, props); assertTrue("albequerque".equals(transformedProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY))); } /** * Tests functionality of using URLs in 'LOAD DATA LOCAL INFILE' * statements. * * @throws Exception if the test fails. */ public void testLocalInfileWithUrl() throws Exception { File infile = File.createTempFile("foo", "txt"); infile.deleteOnExit(); String url = infile.toURL().toExternalForm(); FileWriter output = new FileWriter(infile); output.write("Test"); output.flush(); output.close(); try { this.stmt.executeUpdate("DROP TABLE IF EXISTS testLocalInfileWithUrl"); this.stmt.executeUpdate("CREATE TABLE testLocalInfileWithUrl (field1 LONGTEXT)"); Properties props = new Properties(); props.setProperty("allowUrlInLocalInfile", "true"); Connection loadConn = getConnectionWithProps(props); Statement loadStmt = loadConn.createStatement(); try { loadStmt.executeQuery("LOAD DATA LOCAL INFILE '" + url + "' INTO TABLE testLocalInfileWithUrl"); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); throw sqlEx; } this.rs = this.stmt.executeQuery("SELECT * FROM testLocalInfileWithUrl"); assertTrue(this.rs.next()); assertTrue("Test".equals(this.rs.getString(1)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -