Java JDBC
Java Database Connectivity (JDBC) is an API that allows Java programs to interact with databases. JDBC is a standard API developed by Sun Microsystems that defines how a client may access a database. With JDBC, you can execute SQL statements, retrieve results, and interact with databases.
Syntax
To use JDBC in a Java program, follow these steps:
- Load the database driver class.
- Establish a connection to the database.
- Create a statement object.
- Execute a query or update statement.
- Process the results if necessary.
- Close the connection and statement objects.
Example
Let's say you have an employee database with a table called "employees". Follow the steps below to connect to the database and retrieve all employees:
public class EmployeeDAO {
private Connection conn = null;
private Statement stmt = null;
public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<>();
try {
// Step 1: Load the database driver class
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Establish a connection to the database
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "user", "password");
// Step 3: Create a statement object
stmt = conn.createStatement();
// Step 4: Execute a query statement
String sql = "SELECT * FROM employees";
ResultSet rs = stmt.executeQuery(sql);
// Step 5: Process the results if necessary
while (rs.next()) {
Employee emp = new Employee(
rs.getInt("id"),
rs.getString("name"),
rs.getInt("age"),
rs.getString("email")
);
employees.add(emp);
}
// Step 6: Close the connection and statement objects
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return employees;
}
}
Output
The output of the above example is a list of all the employees retrieved from the database.
Explanation
In the example above, we used JDBC to connect to a MySQL database and retrieve all employees from the "employees" table. We first load the MySQL driver class, establish a connection to the database, and create a statement object. Then, we execute a query statement to retrieve all employees, process the results, and finally, close the connection and statement objects.
Use
JDBC can be used in Java programs to interact with any standard relational database. You can use JDBC to insert, update, or delete records from the database, in addition to querying the database for records. You can also manage transactions and connections.
Important Points
- Make sure you load the correct database driver class for the database you're working with.
- Always close the connection and statement objects to free up resources.
- Always sanitize user inputs to prevent SQL injection attacks.
- Be aware of character encoding issues when working with non-ASCII data.
Summary
In this tutorial, we discussed how to use JDBC in a Java program to interact with a database. We covered the syntax, example, output, explanation, use, important points, and summary of JDBC in Java. With this knowledge, you can now write Java programs that interact with databases and perform basic CRUD operations.