This is one of the most commonly asked questions from JDBC fundamentals, and knowing all the steps of JDBC connection is important.
import java.sql.*;
class JDBCTest {
public static void main (String args []) throws Exception
{
//Load driver class
Class.forName (“oracle.jdbc.driver.OracleDriver”);
//Create connection
Connection conn = DriverManager.getConnection
(“jdbc:oracle:thin:@hostname:1526:testdb”, “scott”, “tiger”);
// @machineName:port:SID, userid, password
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(“select ‘Hi’ from dual”);
while (rs.next())
System.out.println (rs.getString(1)); // Print col 1 => Hi
stmt.close();
}
}