ORM(Object Relational Mapping) implementation in java

               A programming technique that interacts the relational database for developers. That is called ORM(Object Relational Mapping).

Actually, it is like a bridge between front end and back end code. Here, front end is your code. Back end is your relational database.

 Let us create a Customer database for this project.

Java implementation:

Part1: Customer Class creation

public class Customer {

    private int c_id;

    private String c_name;

    private String c_email;

    // let us code the constructor

    public Customer() {}

    public Customer(int c_id, String c_name, String c_email) {

        this.c_id = c_id;

        this.c_name = c_name;

        this.c_email = c_email;

    }

    // code for input

    public int getId() { return c_id; }

    public void setId(int c_id) { this.c_id = c_id; }

    public String getName() { return c_name; }

    public void setName(String c_name) { this.c_name = c_name; }

    public String getEmail() { return c_email; }

    public void setEmail(String c_email) { this.c_email = c_email; }

    @Override

    public String toString() {

        return "Customer{c_id=" + c_id + ", c_name=" + c_name + ", c_email=" + c_email + "}";

    }

}

Part 2: CustomerDao class code is written here

import java.sql.*;

public class CustomerDao {

    private Connection connection1;

    public CustomerDao(Connection connection1) {

        this.connection1 = connection1;

    }

    public void add(Customer cust) throws SQLException {

        String query = "INSERT INTO Customer (c_id, c_name, c_email) VALUES (?, ?, ?)";

        try (PreparedStatement st1 = connection1.prepareStatement(query)) {

            st1.setInt(1, cust.getId());

            st1.setString(2, cust.getName());

            st1.setString(3, cust.getEmail());

            st1.executeUpdate();

        }

    }

    public Customer findById(int c_id) throws SQLException {

        String query = "SELECT * FROM Customer WHERE c_id = ?";

        try (PreparedStatement st1 = connection1.prepareStatement(query)) {

            st1.setInt(1, c_id);

            ResultSet rs = st1.executeQuery();

            if (rs.next()) {

                return new Customer(

                    rs.getInt("c_id"),

                    rs.getString("c_name"),

                    rs.getString("c_email")

                );

            }

        }

        return null;

    }

Part 3: ORMain class

import java.sql.*;

public class ORMain {

    public static void main(String[] args) {

        try {

            // Step 1: Let us Connect to database (using SQLite)

            Connection con1 = DriverManager.getConnection("jdbc:sqlite:sampledb.db");

            // Step 2: if the table doesn’t exist, it creates it.

            try (Statement st1 = con1.createStatement()) {

                String sql = "CREATE TABLE IF NOT EXISTS users (c_id INTEGER PRIMARY KEY, c_name TEXT, c_email TEXT)";

                st1.execute(sql);

            }

            // Step 3: insert a data and retrieve it

            CustomerDao da1 = new CustomerDao(con1);

            Customer cust = new Customer(4, "Rajeeva", "raje_va@abc.com");

            da1.add(cust);

            Customer retr1 = da1.findById(3);

            System.out.println("The data is Retrieved here: " + retr1);

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

}

}

That’s all. this is the mini ORM code in java.  Hope ,it will  give you the information. Keep coding!!!

No comments:

Post a Comment