Creating, reading, updating, and deleting data in a database is a common task in many applications, and JDBC (Java Database Connectivity) is a Java API that allows you to connect to a database and perform these operations. In this blog post, we will walk through the steps of setting up a simple CRUD (create, read, update, delete) operation using JDBC.
1. Connect to the database
The first step is to establish a connection to the database. You can do this by loading the JDBC driver and creating a connection object.
Java
try {Â Â Â Â Class.forName("com.mysql.jdbc.Driver");Â Â Â Â Connection con = DriverManager.getConnection(Â Â Â Â Â Â Â Â "password");Â Â Â Â System.out.println("Connection established.");}catch (Exception e) {Â Â Â Â e.printStackTrace();} |
2. Create a new record
Once you have a connection to the database, you can use the connection object to create a new record in the database. To do this, you will need to use an SQL INSERT statement and execute it using the connection object.
Java
try {Â Â Â String sql = "INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)";Â Â Â PreparedStatement statement = con.prepareStatement(sql);Â Â Â statement.setString(1, "value1");Â Â Â statement.setString(2, "value2");Â Â Â statement.setInt(3, 123);Â Â Â statement.executeUpdate();Â Â Â System.out.println("Record created.");} catch (SQLException e) {Â Â Â e.printStackTrace();} |
3. Read a record
To read a record from the database, you will need to use an SQL SELECT statement and execute it using the connection object. The result of the query will be a ResultSet object that you can use to access the data in the record.
Java
try {Â Â Â String sql = "SELECT column1, column2, column3 FROM table_name WHERE id = ?";Â Â Â PreparedStatement statement = con.prepareStatement(sql);Â Â Â statement.setInt(1, 1);Â Â Â ResultSet result = statement.executeQuery();Â Â Â if (result.next()) {Â Â Â Â Â Â Â String column1 = result.getString("column1");Â Â Â Â Â Â Â String column2 = result.getString("column2");Â Â Â Â Â Â Â int column3 = result.getInt("column3");Â Â Â Â Â Â Â System.out.println("Column 1: " + column1);Â Â Â Â Â Â Â System.out.println("Column 2: " + column2);Â Â Â Â Â Â Â System.out.println("Column 3: " + column3);Â Â Â }} catch (SQLException e) {Â Â Â e.printStackTrace();} |
4. Update a record
To update a record in the database, you will need to use an SQL UPDATE statement and execute it using the connection object.
Java
try {Â Â Â String sql = "UPDATE table_name SET column1 = ?, column2 = ?, column3 = ? WHERE id = ?";Â Â Â PreparedStatement statement = con.prepareStatement(sql);Â Â Â statement.setString(1, "new_value1");Â Â Â statement.setString(2, "new_value2");Â Â Â statement.setInt(3, 456);Â Â Â statement.setInt(4, 1);Â Â Â statement.executeUpdate();Â Â Â System.out.println("Record updated.");} catch (SQLException e) {Â Â Â e.printStackTrace();} |
5. Delete a record
To delete a record from the database, you will need to use an SQL DELETE statement and execute it using the connection object.
Java
try {Â Â Â String sql = "DELETE FROM table_name WHERE id = ?";Â Â Â PreparedStatement statement = con.prepareStatement(sql);Â Â Â statement.setInt(1, 1);Â Â Â statement.executeUpdate();Â Â Â System.out.println("Record deleted.");} catch (SQLException e) {Â Â Â e.printStackTrace();} |
Summary
CRUD operations in Java can be easily performed using JDBC. With a few simple steps, you can connect to a database, create new records, read existing records, update records, and delete records. This allows you to easily manage your data and maintain the integrity of your application.
