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:
- 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.javaClass.forName("oracle.jdbc.driver.OracleDriver"); // Example for Oracle database
- 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.javaConnection connection = DriverManager.getConnection(url, username, password);String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // Example for Oracle database
String username = "yourUsername";
String password = "yourPassword";Replace the URL, username, and password with your specific database details.
- Execute SQL Queries: Once the connection is established, you can create a
StatementorPreparedStatementto execute SQL queries.javaStatement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM your_table");
Replace
your_tablewith the actual table name. - Process the Results: Process the results of your SQL queries, if any, using the
ResultSet.javawhile (resultSet.next()) {
// Process each row of the result set
// Example: String value = resultSet.getString("column_name");
}
- Close Resources: Always close the
ResultSet,Statement, andConnectionobjects to release resources.javaresultSet.close();
statement.close();
connection.close();
Here’s a complete example:
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.