[go: up one dir, main page]

0% found this document useful (0 votes)
45 views5 pages

JDBC 1

This document defines classes and interfaces for performing CRUD operations on employee data from a MySQL database using JDBC. It includes an Emp POJO class to represent employee objects, an EmpDao interface defining data access methods, an EmpDaoImpl class implementing those methods by querying the database and mapping results to Emp objects, and classes for database connection handling. The main method retrieves all employees and prints them.

Uploaded by

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

JDBC 1

This document defines classes and interfaces for performing CRUD operations on employee data from a MySQL database using JDBC. It includes an Emp POJO class to represent employee objects, an EmpDao interface defining data access methods, an EmpDaoImpl class implementing those methods by querying the database and mapping results to Emp objects, and classes for database connection handling. The main method retrieves all employees and prints them.

Uploaded by

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

package com.jdbcdemo.

pojo;
public class Emp {
private int id;
private String name;
private int age;
private int salary;
public Emp() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Id : " + id + ", Name : " + name + ", Age : " + age +
", Salary : " + salary;
}
}
--------------------------------------------------------------------------------------------------------------------------
package com.jdbcdemo.dao;
import java.util.List;
import com.jdbcdemo.pojo.Emp;
public interface EmpDao {
List <Emp> getAllProducts();
Emp searchEmp(int eId);
boolean addNewEmp(Emp emp);
boolean updateEmp(Emp emp);
boolean deleteEmp(int eId);
}
--------------------------------------------------------------------------------------------------------------------------
package com.jdbcdemo.daoimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jdbcdemo.connection.DbConnection;
import com.jdbcdemo.dao.EmpDao;
import com.jdbcdemo.pojo.Emp;
import com.mysql.cj.xdevapi.PreparableStatement;
public class EmpDaoImpl implements EmpDao {
@Override
public List<Emp> getAllProducts() {
List<Emp> lst = new ArrayList<>();
try (Connection con = DbConnection.getDatabaseConnection ()){
PreparedStatement est =
con.prepareStatement("SELECT * FROM emp");
ResultSet rs = est.executeQuery();
while(rs.next()) {
Emp emp = new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setAge(rs.getInt("age"));
emp.setSalary(rs.getInt("salary"));
lst.add(emp);
}
return lst;
} catch (SQLException | NullPointerException e) {
e.printStackTrace();
lst.clear();
return lst;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
package com.jdbcdemo.connection;
public interface DbDetails {
String CONSTR = "jdbc:mysql://localhost:3306/webJ?useSSL=false";
String DBDRIVER = "com.mysql.cj.jdbc.Driver";
String USERNAME = "root";
String PASSWORD = "1234567";
}
--------------------------------------------------------------------------------------------------------------------------
package com.jdbcdemo.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getDatabaseConnection() {
try {
Class.forName (DbDetails.DBDRIVER );
Connection con =
DriverManager.getConnection (DbDetails.CONSTR ,
DbDetails.USERNAME , DbDetails.PASSWORD );
return con;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
package com.jdbcdemo.main;
import java.util.List;
import com.jdbcdemo.dao.EmpDao;
import com.jdbcdemo.daoimpl.EmpDaoImpl;
import com.jdbcdemo.pojo.Emp;
public class AppMain {
public static void main(String[] args) {
EmpDao daoImpl = new EmpDaoImpl();
List<Emp> lst = daoImpl.getAllProducts();
if(lst.size() > 0) {
System.out .println("All Employees are : ");
lst.forEach(System.out ::println);
}
else
System.out .println("No Employees found");
}
}

You might also like