Sample Program 1
import java.sql.*;
public class JDBCConnectivity {
public static void main(String[] args) {
// Database URL, username, and password
String url = "jdbc:mysql://localhost:3306/testdb"; // Change 'testdb' to your database name
String user = "root"; // Change to your MySQL username
String password = "password"; // Change to your MySQL password
// JDBC variables
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish connection
conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database successfully!");
// Create a statement object
stmt = conn.createStatement();
// Create a table (if not exists)
String createTableSQL = "CREATE TABLE IF NOT EXISTS Users (id INT PRIMARY KEY
AUTO_INCREMENT, name VARCHAR(50), age INT)";
stmt.executeUpdate(createTableSQL);
System.out.println("Table created or already exists.");
// Insert a record
String insertSQL = "INSERT INTO Users (name, age) VALUES ('John Doe', 30)";
stmt.executeUpdate(insertSQL);
System.out.println("Record inserted successfully.");
// Retrieve and display data
String selectSQL = "SELECT * FROM Users";
rs = stmt.executeQuery(selectSQL);
System.out.println("User Data:");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Age: "
+ rs.getInt("age"));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
Sample Program 2
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) {
// Database credentials
String url = "jdbc:mysql://localhost:3306/your_database"; // Change 'your_database'
String user = "root"; // Change to your MySQL username
String password = "your_password"; // Change to your MySQL password
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Step 2: Establish the connection
conn = DriverManager.getConnection(url, user, password);
// Step 3: Create and execute SQL statement
stmt = conn.createStatement();
String query = "SELECT * FROM your_table"; // Change 'your_table'
ResultSet rs = stmt.executeQuery(query);
// Step 4: Process the result set
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
// Step 5: Close resources
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("JDBC Driver not found!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Database connection error!");
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Links for installation:
JDBC Connection
https://youtu.be/ykD4hJqaHjo?si=tONhryw2Yvp5Y10M
MYSQL Installation
https://www.youtube.com/watch?v=hiS_mWZmmI0
https://youtu.be/hiS_mWZmmI0?si=dvtObxsLfKVFYPAQ
Mysql connection with Eclipse
https://www.youtube.com/watch?v=3PRW9N5yk2I
https://www.youtube.com/watch?v=rGysLrq9_QI
Mysql with Netbeans
https://www.youtube.com/watch?v=USoyUJZpKFk
https://www.youtube.com/watch?v=VNoU590W750