Please Note : Sys user wont work will throw error connect as sysdba, so created a new user srirams and checking my jdbc connection through that.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleJdbcTest {
static String userid="srirams", password = "xxx";
static String url = "jdbc:oracle:thin:@localhost:1521:XE";
static Connection con = null;
public static void main(String[] args) throws Exception {
Connection con = getOracleJDBCConnection();
if(con!= null){
System.out.println("Got Connection.");
DatabaseMetaData meta = con.getMetaData();
System.out.println("Driver Name : "+meta.getDriverName());
System.out.println("Driver Version : "+meta.getDriverVersion());
}else{
System.out.println("Could not Get Connection");
}
}
public static Connection getOracleJDBCConnection(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, userid, password);
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
}
===================CODE Ends=================
Result:
=============================================
Got Connection.
Driver Name : Oracle JDBC driver
Driver Version : 10.1.0.2.0
No comments:
Post a Comment