The class given below connects to a oracle database using JDBC from a Java standalone class. You can reuse the same class. Make sure that you change the properties like the oracle connection url, database connection username and password.
/*
Connect Oracle Database using JDBC @ Javagenious.com
*/
public class JavaGeniousConnectOracle {
public static void main(String[] args)
throws ClassNotFoundException, SQLException
{
DriverManager.registerDriver
(new oracle.jdbc.driver.OracleDriver());
String url = "jdbc:oracle:thin:@//server.local:1521/prod";
jdbc:oracle:thin:@//host:port/service
Connection conn =
DriverManager.getConnection(url,"scott", "tiger");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset =
stmt.executeQuery("select * from SYS.V_$VERSION");
while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
System.out.println ("Connected to the Database and Closed Successfully.");
}
}