How to do Database Connection Using JDBC thin Driver

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();

}

}

To establish a database connection using JDBC thin driver in Core Java, you typically follow these steps:

  1. Load the JDBC driver: Use Class.forName() to dynamically load the JDBC driver for your database. For a thin driver, this is usually provided by the database vendor.
    java
    Class.forName("oracle.jdbc.driver.OracleDriver"); // Example for Oracle database
  2. Create a Connection: Use the DriverManager.getConnection() method to establish a connection to the database. You need to provide the URL of your database, username, and password.
    java
    String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // Example for Oracle database
    String username = "yourUsername";
    String password = "yourPassword";
    Connection connection = DriverManager.getConnection(url, username, password);

    Replace the URL, username, and password with your specific database details.

  3. Execute SQL Queries: Once the connection is established, you can create a Statement or PreparedStatement to execute SQL queries.
    java
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM your_table");

    Replace your_table with the actual table name.

  4. Process the Results: Process the results of your SQL queries, if any, using the ResultSet.
    java
    while (resultSet.next()) {
    // Process each row of the result set
    // Example: String value = resultSet.getString("column_name");
    }
  5. Close Resources: Always close the ResultSet, Statement, and Connection objects to release resources.
    java
    resultSet.close();
    statement.close();
    connection.close();

Here’s a complete example:

java

import java.sql.*;

public class JDBCExample {
public static void main(String[] args) {
try {
// Load the JDBC driver
Class.forName(“oracle.jdbc.driver.OracleDriver”);

// Create a connection
String url = “jdbc:oracle:thin:@localhost:1521:orcl”;
String username = “yourUsername”;
String password = “yourPassword”;
Connection connection = DriverManager.getConnection(url, username, password);

// Execute a SQL query
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(“SELECT * FROM your_table”);

// Process the results
while (resultSet.next()) {
// Process each row of the result set
// Example: String value = resultSet.getString(“column_name”);
}

// Close resources
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Replace the placeholders (yourUsername, yourPassword, jdbc:oracle:thin:@localhost:1521:orcl, your_table, etc.) with your actual database details and SQL queries.