JDBC (Java Database Connectivity) is an API (Application Programming Interface) that allows Java applications to interact with relational databases. It provides a standard way for Java programs to access databases, regardless of the specific database vendor. The JDBC architecture consists of two main components: the JDBC API and the JDBC driver.
Table of Contents
JDBC API
The JDBC API provides a set of classes and interfaces that allow Java applications to connect to and interact with a database. Let’s look at each of the key interfaces in more detail
Driver Manager
The DriverManager class is used to establish a connection to a database. It provides a method called getConnection() that takes the URL of the database, the username, and the password as parameters. Here’s an example of how to use DriverManager to connect to a MySQL database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "myusername";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection successful!");
} catch (SQLException e) {
System.out.println("Connection failed.");
e.printStackTrace();
}
}
}
Connection
The Connection interface represents a connection to a database and provides methods for managing transactions, creating statements, and more. Here’s an example of how to use Connection to create a statement and execute a query:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "myusername";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection successful!");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
while(resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("Name: " + name + ", Age: " + age);
}
} catch (SQLException e) {
System.out.println("Connection failed.");
e.printStackTrace();
}
}
}
Statement
The Statement interface provides methods for executing SQL statements and returning results. Here’s an example of how to use Statement to insert data into a table:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "myusername";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection successful!");
Statement statement = connection.createStatement();
int rowsInserted = statement.executeUpdate("INSERT INTO mytable (name, age) VALUES ('John', 30)");
if (rowsInserted > 0) {
System.out.println("A new row has been inserted.");
}
} catch (SQLException e) {
System.out.println("Connection failed.");
e.printStackTrace();
}
}
}
ResultSet
The ResultSet interface provides methods for retrieving data from a database query. Here’s an example of how to use ResultSet to retrieve data from a table:
import java.sql.Connection;
import java.sql.DriverManager;
import java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "myusername";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection successful!");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
while(resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("Name: " + name + ", Age: " + age);
}
} catch (SQLException e) {
System.out.println("Connection failed.");
e.printStackTrace();
}
}
}
Conclusion
JDBC is a powerful API that allows Java applications to interact with relational databases through the JDBC API and JDBC drivers. By using these components, developers can easily connect to databases and retrieve and manipulate data from Java programs.