0% found this document useful (0 votes)
29 views3 pages

Practical Number 18

Practical 18 of ajp

Uploaded by

rautv3550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Practical Number 18

Practical 18 of ajp

Uploaded by

rautv3550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL NUMBER:- 18

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCDemo {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test_db";
private static final String JDBC_USER = "root"; // Replace with your DB user
private static final String JDBC_PASSWORD = "password"; // Replace with your DB
password
public static void insertData(String name, String email) {
String insertQuery = "INSERT INTO users (name, email) VALUES (?, ?)";
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER,
JDBC_PASSWORD);
PreparedStatement preparedStatement = conn.prepareStatement(insertQuery)) {
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}
} catch (SQLException e) {
System.out.println("Error occurred during data insertion: " + e.getMessage());
}
}
public static void retrieveData() {
String selectQuery = "SELECT * FROM users";
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER,
JDBC_PASSWORD);
PreparedStatement preparedStatement = conn.prepareStatement(selectQuery)) {
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
System.out.println("User ID: " + id);
System.out.println("User Name: " + name);
System.out.println("User Email: " + email);
System.out.println("--------------------------");
}
} catch (SQLException e) {
System.out.println("Error occurred during data retrieval: " + e.getMessage());
}
}
public static void main(String[] args) {
insertData("John Doe", "[email protected]");
insertData("Jane Smith", "[email protected]");
retrieveData();
}
}
DATABASE:-
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
2) import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateEmployeeTable {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test_db";
private static final String JDBC_USER = "root";
private static final String JDBC_PASSWORD = "password";
public static void createEmployeeTable() {
String createTableSQL = "CREATE TABLE IF NOT EXISTS employee ("
+ "emp_id INT NOT NULL, "
+ "emp_name VARCHAR(255) NOT NULL, "
+ "PRIMARY KEY (emp_id)"
+ ")";
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER,
JDBC_PASSWORD);
Statement statement = conn.createStatement()) {
statement.execute(createTableSQL);
System.out.println("Employee table created successfully!");
} catch (SQLException e) {
System.out.println("Error occurred while creating table: " + e.getMessage());
}
}
public static void main(String[] args) {
createEmployeeTable();
}
}

You might also like