Step 1: Create a New Java Project in Eclipse
Open Eclipse.
Go to File → New → Java Project.
Enter Project Name: e.g., MySQLJDBCProject.
Click Finish.
Step 2: Add MySQL JDBC Driver to Your Project
Right-click on your project → Build Path → Configure Build Path.
Go to the Libraries tab.
Click Add External JARs….
Browse and select the MySQL Connector JAR you downloaded (mysql-connector-j-xxx.jar).
Click Apply and Close.
Step 3: Create Database Table (If not done)
CREATE DATABASE sreedattha_db;
USE student_db;
CREATE TABLE students (
id INT PRIMARY KEY,
student_name VARCHAR(100),
age INT,
email VARCHAR(100)
);
INSERT INTO students (id, student_name, age, email) VALUES
('101','Alice Johnson', 20, 'alice@example.com'),
('102','Bob Smith', 22, 'bob@example.com');
Step 4: Create a Java Class to Connect to MySQL
Right-click src folder → New → package → New → Class.
Name it MySQLConnect and check public static void main.
Paste the following code:
14. Write a java program to connect a database by using JDBC.
package mysqldb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class MySQLConnect {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/sreedattha_db"; // replace with
your DB
String user = "root"; // your MySQL username
String password = "Welcome123$"; // your MySQL password
try {
// Load and register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Connect to the database
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to MySQL successfully!");
// Close the connection
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Connection failed!");
e.printStackTrace();
}
}
}
Output
15. Write a java program to retrieve data from database using JDBC..
package mysqldb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class MySQLConnect {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/sreedattha_db"; // replace with your DB name
String user = "root"; // your MySQL username
String password = "Welcome123$"; // your MySQL password
String QUERY1 = "SELECT * FROM sreedattha_db.students;";
try {
// Load and register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Connect to the database
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to MySQL successfully!");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY1);
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", student name: " + rs.getString("student_name"));
System.out.print(", age: " + rs.getString("age"));
System.out.println(", email: " + rs.getString("email"));
}
// Close the connection
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Connection failed!");
e.printStackTrace();
}
}
}
Mysql Database
Output
15. Write a java program to store data from database using JDBC..
package mysqldb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class MySQLStoreData {
public static void main(String[] args) {
// JDBC connection parameters
String URL = "jdbc:mysql://localhost:3306/sreedattha_db"; // replace with your DB name
String User = "root"; // your MySQL username
String Password = "Welcome123$"; // your MySQL password
Scanner scanner = new Scanner(System.in);
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Connect to the database
Connection conn = DriverManager.getConnection(URL, User, Password);
// SQL insert query
String sql = "INSERT INTO students (id,student_name, age, email) VALUES (?, ?, ?, ?)";
// Prepare statement
PreparedStatement statement = conn.prepareStatement(sql);
// Get data from user
System.out.print("Enter ID: ");
int id = scanner.nextInt();
System.out.print("Enter student name: ");
String name = scanner.nextLine();
scanner.nextLine(); // consume newline
System.out.print("Enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter email: ");
String email = scanner.nextLine();
// Set parameters
statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, age);
statement.setString(4, email);
// Execute insert
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Student inserted successfully!");
}
// Close connection
statement.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Database connection or query failed.");
e.printStackTrace();
} finally {
scanner.close();
} } }
Output
Entered input
Db output data