📄 tnsparse.java
字号:
strtmp = strtmp + " ";
int i = 0;
int j = 0;
int countera = 0;
int counterb = 0;
while (true) {
i = strtmp.indexOf("(", i + 1);
if (i == -1)
break;
countera = countera + 1;
}
while (true) {
j = strtmp.indexOf(")", j + 1);
if (j == -1)
break;
counterb = counterb + 1;
}
if (countera != counterb)
return false;
else
return true;
}
/*
* public String connClose ()
* @param
* @return
*
* Close SQL connection handle
*/
public void connClose ()
throws SQLException {
if (conn != null) {
try {
if (!conn.isClosed()) {
conn.close();
conn = null;
}
}
/* If SQLException, just set it to null */
catch (SQLException e) {conn = null;}
}
}
/*
* public String close ()
* @param
* @return
*
* Flush out filestr, aliasstr, tnsnamesora and close SQL Connection if open
*/
public void close ()
throws Exception {
filestr = "";
aliasstr = "";
tnsnamesora = null;
connClose ();
}
/*
* public String getVersion (String alias)
* @param
* @return String: Current version
*
* Return current TNSParse version
*/
public String getVersion () {
return version;
}
/***************************
**** PROTECTED METHODS ****
***************************/
/***************************
***** PRIVATE METHODS *****
***************************/
/*
* private String getConnectString (String alias)
* @param alias: Alias/Service name
* @return String: Full connection string
*
* Return full connection string from <ServiceName> to to last ")"
*/
private String getConnectString (String alias)
throws Exception {
String retstr = new String ("");
String tokstr = new String ();
String strtmp = new String (alias.toUpperCase().trim());
int i = 0;
int j = 0;
StringTokenizer st = new StringTokenizer(aliasstr, eol);
while(st.hasMoreTokens()) {
tokstr = st.nextToken().trim();
if (tokstr.lastIndexOf(".") != -1) {
if (strtmp.lastIndexOf(".") == -1)
strtmp = strtmp + tokstr.substring(tokstr.lastIndexOf("."), tokstr.length());
}
if (strtmp.trim().equalsIgnoreCase(tokstr.trim())) {
i = filestr.indexOf(strtmp.trim(), 0);
j = filestr.indexOf(strchar5, i);
if ((i != -1) && (j != -1))
retstr = filestr.substring(i, j);
break;
}
}
return retstr;
}
/*
* private void checkAliasExist (String alias)
* @param alias: Alias/Service name
* @return
*
* Checks if Alias/Service name exists
*
* throws: Exception if does not exist
*/
private void checkAliasExist (String alias)
throws Exception {
String strtmp = new String (getConnectString (alias));
if (strtmp.equalsIgnoreCase(""))
throwException ("Service name '" + alias.toUpperCase() +
"' does not exist in file '" + tnsnamesora.getAbsolutePath() +
"'", false);
}
/*
* private boolean isTcpAvailable (String connstr)
* @param connstr: Full connection string to test
* @return boolean: true if TCP available, false otherwise
*
* Checks if connection string has TCP protocol defined
*/
private boolean isTcpAvailable (String connstr)
throws Exception {
if (connstr.indexOf("(PROTOCOL=TCP)") == -1)
return false;
else
return true;
}
/*
* private String getFirstTcpString (String connstr)
* @param connstr: Full connection string to parse
* @return String: First available TCP connection string
*
* Returns first available TCP connection string used by getThinTcpConn()
*/
private String getFirstTcpString (String connstr)
throws Exception {
String retstr = new String ("");
int i = 0;
int j = 0;
while (true) {
i = connstr.indexOf("(ADDRESS=", j);
if (i == -1)
break;
i = connstr.indexOf("=", i) + 1;
j = connstr.indexOf("))", i) + 1;
if ((i != -1) && (j != -1))
retstr = connstr.substring(i, j);
if (retstr.indexOf("PROTOCOL=TCP") != -1) {
i = connstr.indexOf("(CONNECT_DATA=", j);
j = connstr.indexOf("))", i) + 2;
if ((i != -1) && (j != -1))
retstr = retstr + connstr.substring(i, j);
break;
}
else
retstr = "";
}
return retstr;
}
/*
* private String parseTcpString (String tcpstring, String getval)
* @param tcpstring: Unparsed TCP string returned by getFirstTcpString()
* @return getval: Value to get e.g. "HOST", "PORT" etc
*
* Parses TCP string and returns value of "HOST", "PORT" etc
*/
private String parseTcpString (String tcpstring, String getval)
throws Exception {
String retstr = new String ("");
int i = 0;
int j = 0;
if (getval.equalsIgnoreCase("CONNECT_DATA")) {
i = tcpstring.indexOf(getval);
i = tcpstring.indexOf("=", i);
i = tcpstring.indexOf("(", i);
i = tcpstring.indexOf("=", i) + 1;
j = tcpstring.indexOf("))", i);
if ((i != -1) && (j != -1))
retstr = tcpstring.substring(i, j);
}
else {
i = tcpstring.indexOf(getval);
i = tcpstring.indexOf("=", i) + 1;
j = tcpstring.indexOf(")", i);
if ((i != -1) && (j != -1))
retstr = tcpstring.substring(i, j);
}
return retstr;
}
/*
* private String buildThinString (String tcpstring)
* @param tcpstring: Unparsed TCP string returned by getFirstTcpString()
*
* Parses TCP string and returns a full concatenated string e.g. <HOST>:<PORT>:<SID>
*/
private String buildThinString (String tcpstring)
throws Exception {
String retstr = new String ("");
retstr = parseTcpString (tcpstring, "HOST") + ":";
retstr = retstr + parseTcpString (tcpstring, "PORT") + ":";
retstr = retstr + parseTcpString (tcpstring, "CONNECT_DATA");
return retstr;
}
/*
* private String makeSqlConnection (String connstr, String drivertype, String username, String pwd, boolean
autocommit)
* @param connstr: Connection string. Oracle alias for oci drivers. <HOST>:<PORT>:<SID> for thin drive
r
* drivertype: Driver type to use "oci" or "thin"
* username: Oracle database username
* pwd: Oracle password
* autocommit: true to enable AutoCommit, false to disable AutoCommit
* @return String: "SUCCESS" Or SQLException error text
*
* Connects to the database using given url
*/
private String makeSqlConnection (String connstr, String drivertype,
String username, String pwd, boolean autocommit)
throws SQLException {
try {
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
} catch (SQLException e1) {return "java.sql.SQLException: " + e1.getMessage();}
String connurl = new String (buildConnURL(connstr, drivertype));
if (connurl == null)
return "java.sql.SQLException: No suitable oracle driver found";
try {
conn = DriverManager.getConnection (connurl, username, pwd);
conn.setAutoCommit (autocommit);
return "SUCCESS";
} catch (SQLException e2) {
connClose ();
return "java.sql.SQLException: " + e2.getMessage();
}
}
/*
* private String buildConnURL (String connstr, String drivertype)
* @param connstr: Connection string. Oracle alias for oci drivers. <HOST>:<PORT>:<SID> for thin drive
r
* drivertype: Driver type. "oci" or "thin"
* @return String: Full Connection URL OR null if error occurs
*
* Builds a full Connection URL and returns using a suitable available Oracle driver
*/
private String buildConnURL (String connstr, String drivertype)
throws SQLException {
String strurl = new String ("jdbc:oracle:" + drivertype);
String url = new String ();
if (drivertype.equalsIgnoreCase("THIN")) {
try {
url = strurl + ":@" + connstr;
Driver driver = DriverManager.getDriver(url);
return url;
} catch (SQLException e1) {return null;}
}
else {
try {
/* Try oci8 driver first */
url = strurl + "8:@" + connstr;
Driver driver = DriverManager.getDriver(url);
return url;
} catch (SQLException e2) {
try {
/* Now try oci7 driver */
url = strurl + "7:@" + connstr;
Driver driver = DriverManager.getDriver(url);
return url;
} catch (SQLException e3) {return null;}
}
}
}
/*
* private String enumerateAddressList (String connstr)
* @param connstr: Full connection string to parse
* @return String: End-Of-Line delimited list of all addresses including CONNECT_DATA
* (ADDRESS=(.....))(CONNECT_DATA=(...))
*
* Parses a connection string and returns all defined ADDRESSES
*/
private String enumerateAddressList (String connstr)
throws Exception {
String retstr = new String ("");
String strtmp = new String ("");
int i = 0;
int j = 0;
int k = 0;
int l = 0;
while (true) {
i = connstr.indexOf("(ADDRESS=", j);
if (i == -1)
break;
j = connstr.indexOf("))", i);
j = connstr.indexOf("(", j);
if ((i != -1) && (j != -1)) {
strtmp = connstr.substring(i, j);
if (strtmp.indexOf("(PROTOCOL=BEQ)") != -1)
strtmp = strtmp.substring(0, strtmp.indexOf("'))") + 3);
else
strtmp = strtmp.substring(0, strtmp.indexOf("))") + 2);
retstr = retstr + strtmp;
strtmp = "";
}
k = connstr.indexOf("(CONNECT_DATA=", j);
l = connstr.indexOf("))", k) + 2;
if ((k != -1) && (l != -1))
retstr = retstr + connstr.substring(k, l) + eol;
}
if (retstr.endsWith(eol))
retstr = retstr.substring(0, retstr.length() - 1);
return retstr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -