📄 connection.java
字号:
throw new SQLException("Unsupported character encoding '" +
_encoding +
"'.", "0S100");
}
}
}
if (Driver.debug)
System.out.println("Connect: " + _user +
" to " + _database);
try
{
_io = createNewIO(host, port);
_io.init(_user, _password);
if (_database.length() != 0)
{
_io.sendCommand(MysqlDefs.INIT_DB, _database, null);
}
_isClosed = false;
_serverVariables = new Hashtable();
if (_io.versionMeetsMinimum(3, 22, 1))
{
_useFastPing = true;
}
//
// If version is greater than 3.21.22 get the server
// variables.
if (_io.versionMeetsMinimum(3, 21, 22))
{
com.mysql.jdbc.Statement stmt = null;
com.mysql.jdbc.ResultSet results = null;
try
{
stmt = (com.mysql.jdbc.Statement)createStatement();
results = (com.mysql.jdbc.ResultSet)stmt.executeQuery(
"SHOW VARIABLES");
while (results.next())
{
_serverVariables.put(results.getString(1),
results.getString(2));
}
}
catch (java.sql.SQLException e)
{
throw e;
}
finally
{
if (results != null)
{
try
{
results.close();
}
catch (java.sql.SQLException sqlE)
{
}
}
if (stmt != null)
{
try
{
stmt.close();
}
catch (java.sql.SQLException sqlE)
{
}
}
}
if (_serverVariables.containsKey("max_allowed_packet"))
{
_maxAllowedPacket = Integer.parseInt((String)_serverVariables.get(
"max_allowed_packet"));
}
if (_serverVariables.containsKey("net_buffer_length"))
{
_netBufferLength = Integer.parseInt((String)_serverVariables.get(
"net_buffer_length"));
}
checkTransactionIsolationLevel();
checkServerEncoding();
}
if (_io.versionMeetsMinimum(3, 23, 15))
{
_transactionsSupported = true;
}
else
{
_transactionsSupported = false;
}
if (_io.versionMeetsMinimum(3, 23, 36))
{
_hasIsolationLevels = true;
}
else
{
_hasIsolationLevels = false;
}
// Start logging perf/profile data if the user has requested it.
String profileSql = info.getProperty("profileSql");
if (profileSql != null && profileSql.trim().equalsIgnoreCase("true"))
{
_io.setProfileSql(true);
}
else
{
_io.setProfileSql(false);
}
_hasQuotedIdentifiers = _io.versionMeetsMinimum(3, 23, 6);
if (_serverVariables.containsKey("sql_mode"))
{
int sqlMode = Integer.parseInt((String)_serverVariables.get(
"sql_mode"));
if ((sqlMode & 4) > 0)
{
_useAnsiQuotes = true;
}
else
{
_useAnsiQuotes = false;
}
}
_io.resetMaxBuf();
}
catch (java.sql.SQLException ex)
{
// don't clobber SQL exceptions
throw ex;
}
catch (Exception ex)
{
throw new java.sql.SQLException(
"Cannot connect to MySQL server on " +
_host + ":" + _port +
". Is there a MySQL server running on the machine/port you are trying to connect to? (" +
ex.getClass().getName() + ")", "08S01");
}
}
public boolean capitalizeDBMDTypes()
{
return _capitalizeDBMDTypes;
}
public boolean supportsTransactions()
{
return _transactionsSupported;
}
public boolean supportsIsolationLevel()
{
return _hasIsolationLevels;
}
public boolean supportsQuotedIdentifiers()
{
return _hasQuotedIdentifiers;
}
/**
* SQL statements without parameters are normally executed using
* Statement objects. If the same SQL statement is executed many
* times, it is more efficient to use a PreparedStatement
*
* @return a new Statement object
* @exception java.sql.SQLException passed through from the constructor
*/
public abstract java.sql.Statement createStatement()
throws java.sql.SQLException;
/**
* A SQL statement with or without IN parameters can be pre-compiled
* and stored in a PreparedStatement object. This object can then
* be used to efficiently execute this statement multiple times.
*
* <p>
* <B>Note:</B> This method is optimized for handling parametric
* SQL statements that benefit from precompilation if the driver
* supports precompilation.
* In this case, the statement is not sent to the database until the
* PreparedStatement is executed. This has no direct effect on users;
* however it does affect which method throws certain java.sql.SQLExceptions
*
* <p>
* MySQL does not support precompilation of statements, so they
* are handled by the driver.
*
* @param sql a SQL statement that may contain one or more '?' IN
* parameter placeholders
* @return a new PreparedStatement object containing the pre-compiled
* statement.
* @exception java.sql.SQLException if a database access error occurs.
*/
public abstract java.sql.PreparedStatement prepareStatement(String sql)
throws java.sql.SQLException;
/**
* A SQL stored procedure call statement is handled by creating a
* CallableStatement for it. The CallableStatement provides methods
* for setting up its IN and OUT parameters and methods for executing
* it.
*
* <B>Note:</B> This method is optimised for handling stored procedure
* call statements. Some drivers may send the call statement to the
* database when the prepareCall is done; others may wait until the
* CallableStatement is executed. This has no direct effect on users;
* however, it does affect which method throws certain java.sql.SQLExceptions
*
* @param sql a SQL statement that may contain one or more '?' parameter
* placeholders. Typically this statement is a JDBC function call
* escape string.
* @return a new CallableStatement object containing the pre-compiled
* SQL statement
* @exception java.sql.SQLException if a database access error occurs
*/
public java.sql.CallableStatement prepareCall(String sql)
throws java.sql.SQLException
{
throw new java.sql.SQLException("Callable statments not supported.",
"S1C00");
}
/**
* A driver may convert the JDBC sql grammar into its system's
* native SQL grammar prior to sending it; nativeSQL returns the
* native form of the statement that the driver would have sent.
*
* @param sql a SQL statement that may contain one or more '?'
* parameter placeholders
* @return the native form of this statement
* @exception java.sql.SQLException if a database access error occurs
*/
public String nativeSQL(String sql)
throws java.sql.SQLException
{
if (Driver.trace)
{
Object[] args = {sql};
Debug.methodCall(this, "nativeSQL", args);
Debug.returnValue(this, "nativeSQL", sql);
}
return sql;
}
/**
* If a connection is in auto-commit mode, than all its SQL
* statements will be executed and committed as individual
* transactions. Otherwise, its SQL statements are grouped
* into transactions that are terminated by either commit()
* or rollback(). By default, new connections are in auto-
* commit mode. The commit occurs when the statement completes
* or the next execute occurs, whichever comes first. In the
* case of statements returning a ResultSet, the statement
* completes when the last row of the ResultSet has been retrieved
* or the ResultSet has been closed. In advanced cases, a single
* statement may return multiple results as well as output parameter
* values. Here the commit occurs when all results and output param
* values have been retrieved.
*
* <p><b>Note:</b> MySQL does not support transactions, so this
* method is a no-op.
*
* @param autoCommit - true enables auto-commit; false disables it
* @exception java.sql.SQLException if a database access error occurs
*/
public void setAutoCommit(boolean autoCommit)
throws java.sql.SQLException
{
if (Driver.trace)
{
Object[] args = {new Boolean(autoCommit)};
Debug.methodCall(this, "setAutoCommit", args);
}
if (_transactionsSupported)
{
String sql = "SET autocommit=" +
(autoCommit ? "1" : "0");
execSQL(sql, -1);
_autoCommit = autoCommit;
}
else
{
if (autoCommit == false &&
_relaxAutoCommit == false)
{
throw new SQLException("MySQL Versions Older than 3.23.15 do not support transactions",
"08003");
}
else
{
_autoCommit = autoCommit;
}
}
return;
}
/**
* gets the current auto-commit state
*
* @return Current state of the auto-commit mode
* @exception java.sql.SQLException (why?)
* @see setAutoCommit
*/
public boolean getAutoCommit()
throws java.sql.SQLException
{
if (Driver.trace)
{
Object[] args = new Object[0];
Debug.methodCall(this, "getAutoCommit", args);
Debug.returnValue(this, "getAutoCommit", new Boolean(_autoCommit));
}
return _autoCommit;
}
/**
* The method commit() makes all changes made since the previous
* commit/rollback permanent and releases any database locks currently
* held by the Connection. This method should only be used when
* auto-commit has been disabled.
*
* <p><b>Note:</b> MySQL does not support transactions, so this
* method is a no-op.
*
* @exception java.sql.SQLException if a database access error occurs
* @see setAutoCommit
*/
public void commit()
throws java.sql.SQLException
{
if (Driver.trace)
{
Object[] args = new Object[0];
Debug.methodCall(this, "commit", args);
}
if (_isClosed)
{
throw new java.sql.SQLException("Commit attempt on closed connection.",
"08003");
}
// no-op if _relaxAutoCommit == true
if (_autoCommit && !_relaxAutoCommit)
{
throw new SQLException("Can't call commit when autocommit=true");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -