Skip to content
May 3, 2012 / doganay

ORACLE JDBC CONNECTION TEST

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Hi,

save this code as testconn.java

import java.sql.*;

public class testconn {
public static void main(String[] args) {
if ( args.length != 4 ) {
printLine(“Usage: TestConnection <driver> <url> <user> <pwd>”);
}
else {
String driver = args[0];
String url = args[1];
String user = args[2];
String pwd = args[3];

boolean bConnected = checkConnection(driver, url, user, pwd);
if ( bConnected ) {
printLine(“Connection successful.”);
}
else {
printLine(“Connection failed.”);
}
}
}
public static boolean checkConnection(String driver, String url,
String user, String pwd) {
Connection connection = null;
boolean bConnected = false;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
bConnected = true;
} catch (Exception e) {
e.printStackTrace();
}
finally {
if ( connection != null ) {
try {
connection.close();
} catch (SQLException se) {}
connection = null;
}
return bConnected;
}
}
public static void printLine(String msg) {

System.out.println(msg);
}
}
#end of code

javac -cp .;C:\oracle\product\11.1.0\client_1\jdbc\lib\ojdbc5.jar;C:\oracle\product\11.1.0\client_1\jlib\orai18n.jar testconn.java
java -cp .;C:\oracle\product\11.1.0\client_1\jdbc\lib\ojdbc5.jar;C:\oracle\product\11.1.0\client_1\jlib\orai18n.jar testconn oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@HOST:PORT:DB_NAME USERNAME PASSWORD

-change semicolons(;) to columns(:) if you use unix/linux.

–change oracle home paths as you need..

Thx:

http://testdrive.mapinfo.com/techsupp/miprod.nsf/kbase_by_product/B309A6631406A514852569770060E795

http://r-ozturk.blogspot.com/2012/04/oracle-connection-test-with-jdbc-thin.html

http://www.oracle.com/technetwork/java/compile-136656.html

try and pray:)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: