?? nonregisteringdriver.java
字號:
public boolean acceptsURL(String url) throws java.sql.SQLException {
return (parseURL(url, null) != null);
}
/**
* Try to make a database connection to the given URL. The driver should
* return "null" if it realizes it is the wrong kind of driver to connect
* to the given URL. This will be common, as when the JDBC driverManager
* is asked to connect to a given URL, it passes the URL to each loaded
* driver in turn.
*
* <p>
* The driver should raise an java.sql.SQLException if it is the right
* driver to connect to the given URL, but has trouble connecting to the
* database.
* </p>
*
* <p>
* The java.util.Properties argument can be used to pass arbitrary string
* tag/value pairs as connection arguments.
* </p>
*
* <p>
* My protocol takes the form:
* <PRE>
* jdbc:mysql://host:port/database
* </PRE>
* </p>
*
* @param url the URL of the database to connect to
* @param info a list of arbitrary tag/value pairs as connection arguments
*
* @return a connection to the URL or null if it isnt us
*
* @exception java.sql.SQLException if a database access error occurs
* @throws SQLException DOCUMENT ME!
*
* @see java.sql.Driver#connect
*/
public java.sql.Connection connect(String url, Properties info)
throws java.sql.SQLException {
Properties props = null;
if ((props = parseURL(url, info)) == null) {
return null;
} else {
try {
Connection newConn = new com.mysql.jdbc.Connection(host(props),
port(props), props, database(props), url, this);
return (java.sql.Connection) newConn;
} catch (SQLException sqlEx) {
// Don't wrap SQLExceptions, throw
// them un-changed.
throw sqlEx;
} catch (Exception ex) {
throw new SQLException(
"Cannot load connection class because of underlying exception: '"
+ ex.toString() + "'.",
SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE);
}
}
}
//
// return the database name property
//
/**
* Returns the database property from <code>props</code>
*
* @param props the Properties to look for the database property.
*
* @return the database name.
*/
public String database(Properties props) {
return props.getProperty("DBNAME");
}
/**
* Returns the hostname property
*
* @param props the java.util.Properties instance to retrieve the hostname
* from.
*
* @return the hostname
*/
public String host(Properties props) {
return props.getProperty("HOST", "localhost");
}
/**
* Report whether the driver is a genuine JDBC compliant driver. A driver
* may only report "true" here if it passes the JDBC compliance tests,
* otherwise it is required to return false. JDBC compliance requires
* full support for the JDBC API and full support for SQL 92 Entry Level.
*
* <p>
* MySQL is not SQL92 compliant
* </p>
*
* @return is this driver JDBC compliant?
*/
public boolean jdbcCompliant() {
return false;
}
/**
* Returns the port number property
*
* @param props the properties to get the port number from
*
* @return the port number
*/
public int port(Properties props) {
return Integer.parseInt(props.getProperty("PORT", "3306"));
}
//
// return the value of any property this driver knows about
//
/**
* Returns the given property from <code>props</code>
*
* @param name the property name
* @param props the property instance to look in
*
* @return the property value, or null if not found.
*/
public String property(String name, Properties props) {
return props.getProperty(name);
}
/**
* Gets the drivers major version number
*
* @return the drivers major version number
*/
static int getMajorVersionInternal() {
return safeIntParse("3");
}
/**
* Get the drivers minor version number
*
* @return the drivers minor version number
*/
static int getMinorVersionInternal() {
return safeIntParse("0");
}
/**
* Constructs a new DriverURL, splitting the specified URL into its
* component parts
*
* @param url JDBC URL to parse
* @param defaults Default properties
*
* @return Properties with elements added from the url
*
* @exception java.sql.SQLException
*/
//
// This is a new URL-parser. This file no longer contains any
// Postgresql code.
//
Properties parseURL(String url, Properties defaults)
throws java.sql.SQLException {
Properties urlProps = new Properties(defaults);
if (url == null) {
return null;
} else {
/*
* Parse parameters after the ? in the URL and remove
* them from the original URL.
*/
int index = url.indexOf("?");
if (index != -1) {
String paramString = url.substring(index + 1, url.length());
url = url.substring(0, index);
StringTokenizer queryParams = new StringTokenizer(paramString,
"&");
while (queryParams.hasMoreTokens()) {
StringTokenizer vp = new StringTokenizer(queryParams
.nextToken(), "=");
String param = "";
if (vp.hasMoreTokens()) {
param = vp.nextToken();
}
String value = "";
if (vp.hasMoreTokens()) {
value = vp.nextToken();
}
if ((value.length() > 0) && (param.length() > 0)) {
urlProps.put(param, value);
}
}
}
StringTokenizer st = new StringTokenizer(url, ":/", true);
if (st.hasMoreTokens()) {
String protocol = st.nextToken();
if (protocol != null) {
if (!protocol.equalsIgnoreCase("jdbc")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the colon following 'jdbc'
if (st.hasMoreTokens()) {
String colon = st.nextToken();
if (colon != null) {
if (!colon.equals(":")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for sub-protocol to be mysql
if (st.hasMoreTokens()) {
String subProto = st.nextToken();
if (subProto != null) {
if (!subProto.equalsIgnoreCase("mysql")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the colon following 'mysql'
if (st.hasMoreTokens()) {
String colon = st.nextToken();
if (colon != null) {
if (!colon.equals(":")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Look for the "//" of the URL
if (st.hasMoreTokens()) {
String slash = st.nextToken();
String slash2 = "";
if (st.hasMoreTokens()) {
slash2 = st.nextToken();
}
if ((slash != null) && (slash2 != null)) {
if (!slash.equals("/") && !slash2.equals("/")) {
return null;
}
} else {
return null;
}
} else {
return null;
}
// Okay the next one is a candidate for many things
if (st.hasMoreTokens()) {
String token = st.nextToken();
if (token != null) {
if (!token.equals(":") && !token.equals("/")) {
// Must be hostname
urlProps.put("HOST", token);
if (st.hasMoreTokens()) {
token = st.nextToken();
} else {
return null;
}
}
// Check for Port spec
if (token.equals(":")) {
if (st.hasMoreTokens()) {
token = st.nextToken();
urlProps.put("PORT", token);
if (st.hasMoreTokens()) {
token = st.nextToken();
}
}
}
if (token.equals("/")) {
if (st.hasMoreTokens()) {
token = st.nextToken();
urlProps.put("DBNAME", token);
// We're done
return urlProps;
} else {
urlProps.put("DBNAME", "");
return urlProps;
}
}
} else {
return null;
}
} else {
return null;
}
}
return urlProps;
}
private static int safeIntParse(String intAsString) {
try {
return Integer.parseInt(intAsString);
} catch (NumberFormatException nfe) {
return 0;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -