Search in sources :

Example 36 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class OracleSQLCallableStatementProxyTest method init.

@Before
public void init() {
    doInHibernate(this::sessionFactory, session -> {
        session.doWork(connection -> {
            Statement statement = null;
            try {
                statement = connection.createStatement();
                statement.executeUpdate("CREATE OR REPLACE FUNCTION fn_person ( " + "   personId IN NUMBER) " + "    RETURN SYS_REFCURSOR " + "IS " + "    persons SYS_REFCURSOR; " + "BEGIN " + "   OPEN persons FOR " + "        SELECT " + "            p.id AS \"p.id\", " + "            p.name AS \"p.name\", " + "            p.nickName AS \"p.nickName\" " + "       FROM person p " + "       WHERE p.id = personId; " + "   RETURN persons; " + "END;");
            } catch (SQLException ignore) {
            } finally {
                if (statement != null) {
                    statement.close();
                }
            }
        });
    });
    doInHibernate(this::sessionFactory, session -> {
        Person person1 = new Person();
        person1.setId(1L);
        person1.setName("John Doe");
        person1.setNickName("JD");
        session.persist(person1);
    });
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Before(org.junit.Before)

Example 37 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class CustomSQLTest method init.

@Before
public void init() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("ALTER TABLE person ADD COLUMN valid boolean");
                statement.executeUpdate("ALTER TABLE Person_phones ADD COLUMN valid boolean");
            }
        });
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Before(org.junit.Before)

Example 38 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class MySQLStoredProcedureTest method destroy.

@After
public void destroy() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_count_phones");
            } catch (SQLException ignore) {
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_phones");
            } catch (SQLException ignore) {
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("DROP FUNCTION IF EXISTS fn_count_phones");
            } catch (SQLException ignore) {
            }
        });
    });
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Session(org.hibernate.Session) After(org.junit.After)

Example 39 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class OracleCustomSQLWithStoredProcedureTest method init.

@Before
public void init() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("ALTER TABLE person ADD valid NUMBER(1) DEFAULT 0 NOT NULL");
                statement.executeUpdate("CREATE OR REPLACE PROCEDURE sp_delete_person ( " + "   personId IN NUMBER ) " + "AS  " + "BEGIN " + "    UPDATE person SET valid = 0 WHERE id = personId; " + "END;");
            }
        });
    });
}
Also used : Statement(java.sql.Statement) Session(org.hibernate.Session) Before(org.junit.Before)

Example 40 with Statement

use of java.sql.Statement in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method init.

@Before
public void init() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("CREATE OR REPLACE PROCEDURE sp_count_phones (  " + "   personId IN NUMBER,  " + "   phoneCount OUT NUMBER )  " + "AS  " + "BEGIN  " + "    SELECT COUNT(*) INTO phoneCount  " + "    FROM phone  " + "    WHERE person_id = personId; " + "END;");
                statement.executeUpdate("CREATE OR REPLACE PROCEDURE sp_person_phones ( " + "   personId IN NUMBER, " + "   personPhones OUT SYS_REFCURSOR ) " + "AS  " + "BEGIN " + "    OPEN personPhones FOR " + "    SELECT *" + "    FROM phone " + "    WHERE person_id = personId; " + "END;");
                statement.executeUpdate("CREATE OR REPLACE FUNCTION fn_count_phones ( " + "    personId IN NUMBER ) " + "    RETURN NUMBER " + "IS " + "    phoneCount NUMBER; " + "BEGIN " + "    SELECT COUNT(*) INTO phoneCount " + "    FROM phone " + "    WHERE person_id = personId; " + "    RETURN( phoneCount ); " + "END;");
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Person person1 = new Person("John Doe");
        person1.setNickName("JD");
        person1.setAddress("Earth");
        person1.setCreatedOn(Timestamp.from(LocalDateTime.of(2000, 1, 1, 0, 0, 0).toInstant(ZoneOffset.UTC)));
        person1.getAddresses().put(AddressType.HOME, "Home address");
        person1.getAddresses().put(AddressType.OFFICE, "Office address");
        entityManager.persist(person1);
        Phone phone1 = new Phone("123-456-7890");
        phone1.setId(1L);
        phone1.setType(PhoneType.MOBILE);
        person1.addPhone(phone1);
        Phone phone2 = new Phone("098_765-4321");
        phone2.setId(2L);
        phone2.setType(PhoneType.LAND_LINE);
        person1.addPhone(phone2);
    });
}
Also used : Statement(java.sql.Statement) Phone(org.hibernate.userguide.model.Phone) Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Before(org.junit.Before)

Aggregations

Statement (java.sql.Statement)3054 Connection (java.sql.Connection)1634 ResultSet (java.sql.ResultSet)1631 SQLException (java.sql.SQLException)1529 PreparedStatement (java.sql.PreparedStatement)1329 Test (org.junit.Test)570 ArrayList (java.util.ArrayList)323 CallableStatement (java.sql.CallableStatement)135 ResultSetMetaData (java.sql.ResultSetMetaData)127 IOException (java.io.IOException)121 Properties (java.util.Properties)114 HashMap (java.util.HashMap)83 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 StringPlus (mom.trd.opentheso.bdd.tools.StringPlus)63 DataSource (javax.sql.DataSource)62 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)61 Vector (java.util.Vector)61 DatabaseMetaData (java.sql.DatabaseMetaData)53 KeyValue (org.vcell.util.document.KeyValue)49