0% found this document useful (0 votes)
10 views3 pages

Ajt PR1

The document outlines the basic steps to establish a database connection in Java using JDBC, including downloading the driver, adding it to the project, loading the driver, and executing SQL queries. It provides specific JDBC driver names for various databases such as MySQL, Oracle, and SQL Server. Additionally, a sample Java code is included to demonstrate how to connect to a database, handle exceptions, and close the connection.

Uploaded by

Piyush Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Ajt PR1

The document outlines the basic steps to establish a database connection in Java using JDBC, including downloading the driver, adding it to the project, loading the driver, and executing SQL queries. It provides specific JDBC driver names for various databases such as MySQL, Oracle, and SQL Server. Additionally, a sample Java code is included to demonstrate how to connect to a database, handle exceptions, and close the connection.

Uploaded by

Piyush Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Advanced Java Technology (CE0618)

(CE0522)

PRACTICAL – 1
---------------------------------------------------------------------------------------------------------
Aim: Write down basic steps to establish database connection from java. Also
write the connection code for different db
---------------------------------------------------------------------------------------------------------

Basic Steps to Establish a Database Connection


1. Download JDBC Driver: Obtain the JDBC driver for your database.
2. Add JDBC Driver to Project: Include the JDBC driver JAR file in your Java project’s
build path.
3. Load the JDBC Driver: Use Class.forName() to load the JDBC driver class.
4. Establish Connection: Use DriverManager.getConnection() to create a connection
to the database.
5. Perform Database Operations: Use the connection to execute SQL queries.
6. Close the Connection: Always close the connection to free up resources.

Connection Code for each DB connection:


RDBMS JDBC driver Name
MySQL com.mysql.jdbc.Driver
ORACLE oracle.jdbc.driver.OracleDriver
DB2 com.ibm.db2.jdbc.net.DB2Driver
Sybase com.sybase.jdbc.SybDriver
SQLite org.sqlite.JDBC
SQLServer com.microsoft.sqlserver.jdbc.SQLServerDriver

Code:
package practical1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

IU2241230105 CSE-E 1
Advanced Java Technology (CE0618)
(CE0522)

public class Practical1 {

// Database URL, username, and password


private static final String URL = "jdbc:derby://localhost:1527/Pr1";
private static final String USER = "root";
private static final String PASSWORD = "root";

public static void main(String[] args) {


Connection connection = null;
try {
// Establish the connection
connection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connection to MySQL database established
successfully!");
// You can now use the connection to perform database operations
} catch (SQLException e) {
System.out.println("Error connecting to the database: " + e.getMessage());
} finally {
// Close the connection
if (connection != null) {
try {
connection.close();
System.out.println("Connection closed.");
} catch (SQLException e) {
System.out.println("Error closing the connection: " + e.getMessage());
}
}
}
}

IU2241230105 CSE-E 2
Advanced Java Technology (CE0618)
(CE0522)

Output:

IU2241230105 CSE-E 3

You might also like