📄 connection.java
字号:
/*
* MM JDBC Drivers for MySQL
*
* $Id: Connection.java,v 1.8 2002/05/17 19:09:21 mark_matthews Exp $
*
* Copyright (C) 1998 Mark Matthews <mmatthew@worldserver.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* See the COPYING file located in the top-level-directory of
* the archive of this library for complete text of license.
*
* Some portions:
*
* Copyright (c) 1996 Bradley McLean / Jeffrey Medeiros
* Modifications Copyright (c) 1996/1997 Martin Rode
* Copyright (c) 1997 Peter T Mount
*/
/**
* A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are
* returned.
*
* <P>A Connection's database is able to provide information describing
* its tables, its supported SQL grammar, its stored procedures, the
* capabilities of this connection, etc. This information is obtained
* with the getMetaData method.
*
* <p><B>Note:</B> MySQL does not support transactions, so all queries
* are committed as they are executed.
*
* @see java.sql.Connection
* @author Mark Matthews mmatthew@worldserver.com
* @version $Id: Connection.java,v 1.8 2002/05/17 19:09:21 mark_matthews Exp $
*/
package com.mysql.jdbc;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.Hashtable;
import java.util.Properties;
public abstract class Connection
{
/**
* The I/O abstraction interface (network conn to
* MySQL server
*/
MysqlIO _io = null;
/**
* Has this connection been closed?
*/
private boolean _isClosed = true;
/**
* When did the last query finish?
*/
private long _lastQueryFinishedTime = 0;
/**
* The hostname we're connected to
*/
private String _host = null;
/**
* The port number we're connected to
* (defaults to 3306)
*/
private int _port = 3306;
/**
* The user we're connected as
*/
private String _user = null;
/**
* The password we used
*/
private String _password = null;
/**
* The database we're currently using
* (called Catalog in JDBC terms).
*/
protected String _database = null;
/**
* Are we in autoCommit mode?
*/
private boolean _autoCommit = true;
/**
* Are we in read-only mode?
*/
private boolean _readOnly = false;
/**
* Should we do unicode character conversions?
*/
private boolean _doUnicode = false;
/**
* If we're doing unicode character conversions,
* what encoding do we use?
*/
private String _encoding = null;
/**
* The JDBC URL we're using
*/
private String _myURL = null;
/**
* The max rows that a result set can contain.
*
* Defaults to -1, which according to the JDBC
* spec means "all".
*/
private int _maxRows = -1;
/**
* Has the max-rows setting been changed from
* the default?
*/
private boolean _maxRowsChanged = false;
/**
* The driver instance that created us
*/
private com.mysql.jdbc.Driver _myDriver;
/**
* The map of server variables that we retrieve
* at connection init.
*/
private Hashtable _serverVariables = null;
/**
* The largest packet we can send (changed
* once we know what the server supports, we
* get this at connection init).
*/
private int _maxAllowedPacket = 65536;
private int _netBufferLength = 16384;
/**
* Can we use the "ping" command rather than a
* query?
*/
private boolean _useFastPing = false;
//
// This is for the high availability :) routines
//
private boolean _highAvailability = false;
private int _maxReconnects = 3;
private double _initialTimeout = 2.0D;
// The command used to "ping" the database.
// Newer versions of MySQL server have a ping() command,
// but this works for everything.4
private static final String _PING_COMMAND = "SELECT 1";
/** Are transactions supported by the MySQL server we are connected to? */
private boolean _transactionsSupported = false;
/** Do we relax the autoCommit semantics? (For enhydra, for example) */
private boolean _relaxAutoCommit = false;
/** Should we return PreparedStatements for UltraDev's stupid bug? */
protected boolean _useUltraDevWorkAround = false;
/** Does the server suuport isolation levels? */
private boolean _hasIsolationLevels = false;
/**
* Should we capitalize mysql types
*/
private boolean _capitalizeDBMDTypes = false;
/**
* Does this version of MySQL support quoted identifiers?
*/
private boolean _hasQuotedIdentifiers = false;
/**
* Has ANSI_QUOTES been enabled on the server?
*/
private boolean _useAnsiQuotes = false;
/**
* Connect to a MySQL Server.
*
* <p><b>Important Notice</b>
*
* <br>Although this will connect to the database, user code should open
* the connection via the DriverManager.getConnection() methods only.
*
* <br>This should only be called from the org.gjt.mm.mysql.Driver class.
*
* @param Host the hostname of the database server
* @param port the port number the server is listening on
* @param Info a Properties[] list holding the user and password
* @param Database the database to connect to
* @param Url the URL of the connection
* @param D the Driver instantation of the connection
* @return a valid connection profile
* @exception java.sql.SQLException if a database access error occurs
*/
public void connectionInit(String host, int port, Properties info,
String database, String url, Driver d)
throws java.sql.SQLException
{
if (Driver.trace)
{
Object[] args = {host, new Integer(port), info, database, url, d};
Debug.methodCall(this, "constructor", args);
}
if (host == null)
{
_host = "localhost";
}
else
{
_host = host;
}
_port = port;
if (database == null)
{
throw new SQLException("Malformed URL '" + url +
"'.", "S1000");
}
_database = database;
_myURL = url;
_myDriver = d;
_user = info.getProperty("user");
_password = info.getProperty("password");
if (_user == null || _user.equals(""))
{
_user = "nobody";
}
if (_password == null)
{
_password = "";
}
// Check for driver specific properties
if (info.getProperty("relaxAutoCommit") != null)
{
_relaxAutoCommit = info.getProperty("relaxAutoCommit").toUpperCase().equals(
"TRUE");
}
if (info.getProperty("autoReconnect") != null)
{
_highAvailability = info.getProperty("autoReconnect").toUpperCase().equals(
"TRUE");
}
if (info.getProperty("capitalizeTypeNames") != null)
{
_capitalizeDBMDTypes = info.getProperty("capitalizeTypeNames").toUpperCase().equals(
"TRUE");
}
if (info.getProperty("ultraDevHack") != null)
{
_useUltraDevWorkAround = info.getProperty("ultraDevHack").toUpperCase().equals(
"TRUE");
}
if (_highAvailability)
{
if (info.getProperty("maxReconnects") != null)
{
try
{
int n = Integer.parseInt(info.getProperty("maxReconnects"));
_maxReconnects = n;
}
catch (NumberFormatException NFE)
{
throw new SQLException("Illegal parameter '" +
info.getProperty("maxReconnects") +
"' for maxReconnects", "0S100");
}
}
if (info.getProperty("initialTimeout") != null)
{
try
{
double n = Integer.parseInt(info.getProperty(
"initialTimeout"));
_initialTimeout = n;
}
catch (NumberFormatException NFE)
{
throw new SQLException("Illegal parameter '" +
info.getProperty("initialTimeout") +
"' for initialTimeout", "0S100");
}
}
}
if (info.getProperty("maxRows") != null)
{
try
{
int n = Integer.parseInt(info.getProperty("maxRows"));
if (n == 0)
{
n = -1;
} // adjust so that it will become MysqlDefs.MAX_ROWS
// in execSQL()
_maxRows = n;
}
catch (NumberFormatException NFE)
{
throw new SQLException("Illegal parameter '" +
info.getProperty("maxRows") +
"' for maxRows", "0S100");
}
}
if (info.getProperty("useUnicode") != null)
{
String useUnicode = info.getProperty("useUnicode").toUpperCase();
if (useUnicode.startsWith("TRUE"))
{
_doUnicode = true;
}
if (info.getProperty("characterEncoding") != null)
{
_encoding = info.getProperty("characterEncoding");
// Attempt to use the encoding, and bail out if it
// can't be used
try
{
String testString = "abc";
testString.getBytes(_encoding);
}
catch (UnsupportedEncodingException UE)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -