2021-08-05

This commit is contained in:
2021-08-05 11:55:51 +09:00
parent 438bf4bd5c
commit 5f17645bda
11 changed files with 315 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
package kr.pe.elex.examples.db;
import com.elex_project.abraxas.Console;
import java.sql.*;
public class Derby {
public static void main(String... args){
try {
Connection connection = DriverManager.getConnection("jdbc:derby:dbtest/derby;create=true");
try {
connection.createStatement()
.execute("CREATE TABLE person(name VARCHAR(16), age INT)");
} catch (SQLException e){
if (e.getSQLState().equals("X0Y32")){
System.out.println("Already exists.");
}
}
PreparedStatement statement = connection
.prepareStatement("INSERT INTO person (name, age) VALUES (?,?)");
statement.setString(1, "Steve");
statement.setInt(2, 37);
statement.executeUpdate();
ResultSet result = connection.createStatement()
.executeQuery("SELECT * FROM person");
while (result.next()) {
Console.writeLine("{} is {}-year-old.",
result.getString("name"), result.getInt("age"));
}
result.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,39 @@
package kr.pe.elex.examples.db;
import com.elex_project.abraxas.Console;
import java.sql.*;
public class H2 {
public static void main(String... args) {
try {
Connection connection = DriverManager.getConnection("jdbc:h2:./dbtest/h2");
connection.createStatement()
.execute("CREATE TABLE IF NOT EXISTS person(name TEXT, age INT);");
PreparedStatement statement = connection
.prepareStatement("INSERT INTO person (name, age) VALUES (?,?);");
statement.setString(1, "Charlie");
statement.setInt(2, 14);
statement.executeUpdate();
ResultSet result = connection.createStatement()
.executeQuery("SELECT * FROM person;");
while (result.next()) {
Console.writeLine("{} is {}-year-old.",
result.getString("name"), result.getInt("age"));
}
result.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,36 @@
package kr.pe.elex.examples.db;
import com.elex_project.abraxas.Console;
import java.sql.*;
public class SQLite {
public static void main(String... args){
try {
Connection connection = DriverManager.getConnection("jdbc:sqlite:dbtest/hibernate.sqlite");
connection.createStatement()
.execute("CREATE TABLE IF NOT EXISTS person(id INT, name TEXT, age INT);");
PreparedStatement statement = connection
.prepareStatement("INSERT INTO person (name, age) VALUES (?,?);");
statement.setString(1, "Steve");
statement.setInt(2, 37);
statement.executeUpdate();
ResultSet result = connection.createStatement()
.executeQuery("SELECT * FROM person;");
while (result.next()) {
Console.writeLine("{} is {}-year-old.",
result.getString("name"), result.getInt("age"));
}
result.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,34 @@
package kr.pe.elex.examples.hibernate;
import org.hibernate.Session;
public abstract class BaseCrudRepository<T> extends BaseRepository {
public T create(T o) {
Session session = getSession();
session.beginTransaction();
session.save(o);
session.getTransaction().commit();
session.close();
return o;
}
public T update(T person) {
Session session = getSession();
session.beginTransaction();
session.update(person);
session.getTransaction().commit();
session.close();
return person;
}
public void delete(T person) {
Session session = getSession();
session.beginTransaction();
session.delete(person);
session.getTransaction().commit();
session.close();
}
}

View File

@@ -0,0 +1,30 @@
package kr.pe.elex.examples.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
abstract class BaseRepository {
private static final Configuration configuration;
private static SessionFactory factory;
static {
try {
configuration = new Configuration().configure();
factory = configuration.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
protected Session getSession(){
Session session = factory.openSession();
if (!session.isConnected()){
factory = configuration.buildSessionFactory();
session = getSession();
}
return session;
}
}

View File

@@ -0,0 +1,26 @@
package kr.pe.elex.examples.hibernate;
import java.util.List;
public class ORM {
public static void main(String... args) {
PersonRepository repository = new PersonRepository();
Person p;
p = repository.create(new Person("Charlie", 17));
System.out.println(p);
p = repository.create(new Person("Steve", 37));
System.out.println(p);
p = repository.create(new Person("Agatha", 19));
System.out.println(p);
List<Person> list = repository.retrieveAll();
for (Person person : list){
System.out.println(person);
}
repository.retrieveById(3)
.ifPresent(System.out::println);
}
}

View File

@@ -0,0 +1,26 @@
package kr.pe.elex.examples.hibernate;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long id;
@Column(name = "name")
public String name;
@Column(name = "age")
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}

View File

@@ -0,0 +1,43 @@
package kr.pe.elex.examples.hibernate;
import org.hibernate.Session;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import java.util.List;
import java.util.Optional;
public class PersonRepository extends BaseCrudRepository<Person> {
public PersonRepository() {
super();
}
public List<Person> retrieveAll2() {
Session session = getSession();
CriteriaQuery<Person> query = session.getCriteriaBuilder()
.createQuery(Person.class);
TypedQuery<Person> q = session.createQuery(query.select(query.from(Person.class)));
session.close();
return q.getResultList();
}
public List<Person> retrieveAll() {
Session session = getSession();
List<Person> list = session
.createQuery("FROM Person p", Person.class).list();
session.close();
return list;
}
public Optional<Person> retrieveById(long id) {
Session session = getSession();
Person person = (Person) session
.createQuery("FROM Person p where p.id = :id")
.setParameter("id", id)
.setMaxResults(1)
.uniqueResult();
session.close();
return Optional.ofNullable(person);
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:./dbtest/hibernate.h2</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.connection.show_sql">true</property>
<property name="hibernate.connection.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Table Class -->
<mapping class = "kr.pe.elex.examples.hibernate.Person"/>
</session-factory>
</hibernate-configuration>