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 ([Link]).
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@[Link]'),
('102','Bob Smith', 22, 'bob@[Link]');
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 [Link];
import [Link];
import [Link];
import [Link].*;
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
[Link]("[Link]");
// Connect to the database
Connection conn = [Link](url, user, password);
[Link]("Connected to MySQL successfully!");
// Close the connection
[Link]();
} catch (ClassNotFoundException e) {
[Link]("MySQL JDBC Driver not found.");
[Link]();
} catch (SQLException e) {
[Link]("Connection failed!");
[Link]();
}
}
}
Output
15. Write a java program to retrieve data from database using JDBC..
package mysqldb;
import [Link];
import [Link];
import [Link];
import [Link].*;
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
[Link]("[Link]");
// Connect to the database
Connection conn = [Link](url, user, password);
[Link]("Connected to MySQL successfully!");
Statement stmt = [Link]();
ResultSet rs = [Link](QUERY1);
// Extract data from result set
while ([Link]()) {
// Retrieve by column name
[Link]("ID: " + [Link]("id"));
[Link](", student name: " + [Link]("student_name"));
[Link](", age: " + [Link]("age"));
[Link](", email: " + [Link]("email"));
}
// Close the connection
[Link]();
} catch (ClassNotFoundException e) {
[Link]("MySQL JDBC Driver not found.");
[Link]();
} catch (SQLException e) {
[Link]("Connection failed!");
[Link]();
}
}
}
Mysql Database
Output
15. Write a java program to store data from database using JDBC..
package mysqldb;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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([Link]);
try {
// Load the MySQL JDBC driver
[Link]("[Link]");
// Connect to the database
Connection conn = [Link](URL, User, Password);
// SQL insert query
String sql = "INSERT INTO students (id,student_name, age, email) VALUES (?, ?, ?, ?)";
// Prepare statement
PreparedStatement statement = [Link](sql);
// Get data from user
[Link]("Enter ID: ");
int id = [Link]();
[Link]("Enter student name: ");
String name = [Link]();
[Link](); // consume newline
[Link]("Enter age: ");
int age = [Link]();
[Link](); // consume newline
[Link]("Enter email: ");
String email = [Link]();
// Set parameters
[Link](1, id);
[Link](2, name);
[Link](3, age);
[Link](4, email);
// Execute insert
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("Student inserted successfully!");
}
// Close connection
[Link]();
[Link]();
} catch (ClassNotFoundException e) {
[Link]("MySQL JDBC Driver not found.");
[Link]();
} catch (SQLException e) {
[Link]("Database connection or query failed.");
[Link]();
} finally {
[Link]();
} } }
Output
Entered input
Db output data