Skip to content

Day 15: JDBC - Database Connectivity

Java Database Connectivity (JDBC): A Comprehensive Guide

Introduction to JDBC

JDBC is a Java-based API that allows Java applications to interact with relational databases like MySQL, Oracle, PostgreSQL, and more. It provides a standard interface for connecting to databases, executing SQL queries, and handling query results.
Let's begin by importing the necessary Java packages for JDBC:
javaCopy code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Connecting to a Database

To connect to a database, you need to establish a connection using the Connection object. Here's how you can do it:
javaCopy code
public static Connection getConnection() throws SQLException {
String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase";
String username = "your_username";
String password = "your_password";
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
return connection;
}

Replace "jdbc:mysql://localhost:3306/mydatabase", "your_username", and "your_password" with your database URL, username, and password.

Executing SQL Queries

Once you have a connection, you can execute SQL queries. For instance, let's select all records from a table:
javaCopy code
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public static void main(String[] args) {
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
String sql = "SELECT * FROM mytable";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// Process each row of the result set here
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

Handling ResultSets

The ResultSet object contains the query result. You can iterate through it to retrieve data:
javaCopy code
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// Process data
}

Complete Login and Registration with database
package JavaCompleteCourse;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class day15 {
private static final String DB_URL = "jdbc:mysql://your_database_url:your_port/your_database_name";
private static final String DB_USER = "your_database_username";
private static final String DB_PASSWORD = "your_database_password";

private JFrame frame;
private JTextField usernameField;
private JPasswordField passwordField;
private JPanel loginPanel;
private JPanel welcomePanel;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
new day15();
} catch (Exception e) {
e.printStackTrace();
}
});
}

public day15() {
frame = new JFrame("Login Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);

loginPanel = createLoginPanel();
welcomePanel = createWelcomePanel();

frame.setContentPane(loginPanel);

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.