Skip to main content
added 195 characters in body
Source Link
import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestDao {
    // Usage of JdbcDao
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/db", "username", "password");

        // Create table
        jdbcDataAccess
                .createTable("create table if not exists student "
                        + "(id int primary key auto_increment, name varchar(45), age int)");


        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}
import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestDao {
    // Usage of JdbcDao
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/db", "username", "password");

        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}
import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestDao {
    // Usage of JdbcDao
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/db", "username", "password");

        // Create table
        jdbcDataAccess
                .createTable("create table if not exists student "
                        + "(id int primary key auto_increment, name varchar(45), age int)");


        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}
added 14 characters in body
Source Link

Following example of Spring JdbcTemplate, I often use this approach to write my persistence layer:

Advantage of this approach is that you don't have to always open connections, create statements, execute them, close them... You just create one DAO class and use it as shown above. You could also create separated class with static method which will return connection instead of creating one connection instance per JdbcDao instance. Also, in my example of usage, I passed lambda expression instead anonymous inner class of ResultSetProcessor. You could create separated class which will implement ResultSetProcessor interface instead of passing lambda. Why did I choose this approach? Although it's a bit verbose in some cases, it minimizes redundancy of code. Only thing you should create are POJO's (whichwith properties which reflect tables fromin database) and implementation classes for ResultSetProcessor interface. It's not perfect, but it's enough for basic needs.

Following example of JdbcTemplate, I often use this approach to write my persistence layer:

Advantage of this approach is that you don't have to always open connections, create statements, execute them, close them... You just create one DAO class and use it as shown above. You could also create separated class with static method which will return connection instead of creating one connection instance per JdbcDao instance. Also, in my example of usage, I passed lambda expression instead anonymous inner class of ResultSetProcessor. You could create separated class which will implement ResultSetProcessor interface instead of passing lambda. Why did I choose this approach? Although it's a bit verbose in some cases, it minimizes redundancy of code. Only thing you should create are POJO's (which reflect tables from database) and implementation classes for ResultSetProcessor interface.

Following example of Spring JdbcTemplate, I often use this approach to write my persistence layer:

Advantage of this approach is that you don't have to always open connections, create statements, execute them, close them... You just create one DAO class and use it as shown above. You could also create separated class with static method which will return connection instead of creating one connection instance per JdbcDao instance. Also, in my example of usage, I passed lambda expression instead anonymous inner class of ResultSetProcessor. You could create separated class which will implement ResultSetProcessor interface instead of passing lambda. Why did I choose this approach? Although it's a bit verbose in some cases, it minimizes redundancy of code. Only thing you should create are POJO's (with properties which reflect tables in database) and implementation classes for ResultSetProcessor interface. It's not perfect, but it's enough for basic needs.

added 801 characters in body
Source Link
import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestFactoryTestDao {
    // Usage of JdbcFactoryJdbcDao
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/test"db", "root""username", "1234""password");

        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}

Advantage of this approach is that you don't have to always open connections, create statements, execute them, close them... You just create one DAO class and use it as shown above. You could also create separated class with static method which will return connection instead of creating one connection instance per JdbcDao instance. Also, in my example of usage, I passed lambda expression instead anonymous inner class of ResultSetProcessor. You could create separated class which will implement ResultSetProcessor interface instead of passing lambda. Why did I choose this approach? Although it's a bit verbose in some cases, it minimizes redundancy of code. Only thing you should create are POJO's (which reflect tables from database) and implementation classes for ResultSetProcessor interface.

import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestFactory {
    // Usage of JdbcFactory
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/test", "root", "1234");

        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}
import java.util.List;

import org.jdbcfactory.JdbcDao;

public class TestDao {
    // Usage of JdbcDao
    public static void main(String[] args) {

        JdbcDao<Student> jdbcDataAccess = new JdbcDao<Student>(
                "jdbc:mysql://localhost:3306/db", "username", "password");

        // Insert new record
        jdbcDataAccess.saveOrUpdate(
                "insert into student (name, age) values (?, ?)", new Object[] {
                        "John Doe", 25 });

        // Update
        jdbcDataAccess.saveOrUpdate(
                "update student set name = ?, age = ? where id = ?",
                new Object[] { "Jane Doe", 21, 1 });

        // Delete
        jdbcDataAccess.delete("delete from student where id = ?",
                new Object[] { 1 });

        // Select all
        List<Student> list = jdbcDataAccess.getAll("SELECT * FROM student",
                rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                });

        // Select single
        List<Student> list2 = jdbcDataAccess.getAll(
                "select * from student where id = ?", rs -> {
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));

                    return student;
                }, new Object[] { 1 });

        list.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

        list2.forEach(student -> System.out.println(student.getId() + " :: "
                + student.getName() + " :: " + student.getAge()));

    }

    public static class Student {

        private int id;
        private String name;
        private int age;

        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;
        }

    }

}

Advantage of this approach is that you don't have to always open connections, create statements, execute them, close them... You just create one DAO class and use it as shown above. You could also create separated class with static method which will return connection instead of creating one connection instance per JdbcDao instance. Also, in my example of usage, I passed lambda expression instead anonymous inner class of ResultSetProcessor. You could create separated class which will implement ResultSetProcessor interface instead of passing lambda. Why did I choose this approach? Although it's a bit verbose in some cases, it minimizes redundancy of code. Only thing you should create are POJO's (which reflect tables from database) and implementation classes for ResultSetProcessor interface.

Source Link
Loading