Search in sources :

Example 41 with DataSource

use of javax.sql.DataSource in project mybatis-3 by mybatis.

the class ScriptRunnerTest method testLoggingFullScipt.

@Test
public void testLoggingFullScipt() throws Exception {
    DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
    Connection conn = ds.getConnection();
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setAutoCommit(true);
    runner.setStopOnError(false);
    runner.setErrorLogWriter(null);
    runner.setSendFullScript(true);
    StringWriter sw = new StringWriter();
    PrintWriter logWriter = new PrintWriter(sw);
    runner.setLogWriter(logWriter);
    Reader reader = new StringReader("select userid from account where userid = 'j2ee';");
    runner.runScript(reader);
    assertEquals("select userid from account where userid = 'j2ee';" + System.getProperty("line.separator") + System.getProperty("line.separator") + "USERID\t" + System.getProperty("line.separator") + "j2ee\t" + System.getProperty("line.separator"), sw.toString());
}
Also used : StringWriter(java.io.StringWriter) Connection(java.sql.Connection) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) DataSource(javax.sql.DataSource) UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) PrintWriter(java.io.PrintWriter) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 42 with DataSource

use of javax.sql.DataSource in project mybatis-3 by mybatis.

the class ScriptRunnerTest method commentAferStatementDelimiterShouldNotCauseRunnerFail.

@Test
public void commentAferStatementDelimiterShouldNotCauseRunnerFail() throws Exception {
    DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
    Connection conn = ds.getConnection();
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.setErrorLogWriter(null);
    runner.setLogWriter(null);
    runJPetStoreScripts(runner);
    String resource = "org/apache/ibatis/jdbc/ScriptCommentAfterEOLTerminator.sql";
    Reader reader = Resources.getResourceAsReader(resource);
    try {
        runner.runScript(reader);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Connection(java.sql.Connection) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) SQLException(java.sql.SQLException) PooledDataSource(org.apache.ibatis.datasource.pooled.PooledDataSource) DataSource(javax.sql.DataSource) UnpooledDataSource(org.apache.ibatis.datasource.unpooled.UnpooledDataSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 43 with DataSource

use of javax.sql.DataSource in project mybatis-3 by mybatis.

the class SqlRunnerTest method shouldInsert.

@Test
public void shouldInsert() throws Exception {
    DataSource ds = createUnpooledDataSource(BLOG_PROPERTIES);
    runScript(ds, BLOG_DDL);
    Connection connection = ds.getConnection();
    SqlRunner exec = new SqlRunner(connection);
    exec.setUseGeneratedKeySupport(true);
    int id = exec.insert("INSERT INTO author (username, password, email, bio) VALUES (?,?,?,?)", "someone", "******", "someone@apache.org", Null.LONGVARCHAR);
    Map<String, Object> row = exec.selectOne("SELECT * FROM author WHERE username = ?", "someone");
    connection.rollback();
    connection.close();
    assertTrue(SqlRunner.NO_GENERATED_KEY != id);
    assertEquals("someone", row.get("USERNAME"));
}
Also used : Connection(java.sql.Connection) DataSource(javax.sql.DataSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 44 with DataSource

use of javax.sql.DataSource in project mybatis-3 by mybatis.

the class SqlRunnerTest method shouldDemonstrateDDLThroughRunMethod.

@Test
public void shouldDemonstrateDDLThroughRunMethod() throws Exception {
    DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
    Connection connection = ds.getConnection();
    SqlRunner exec = new SqlRunner(connection);
    exec.run("CREATE TABLE BLAH(ID INTEGER)");
    exec.run("insert into BLAH values (1)");
    List<Map<String, Object>> rows = exec.selectAll("SELECT * FROM BLAH");
    exec.run("DROP TABLE BLAH");
    connection.close();
    assertEquals(1, rows.size());
}
Also used : Connection(java.sql.Connection) Map(java.util.Map) DataSource(javax.sql.DataSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 45 with DataSource

use of javax.sql.DataSource in project mybatis-3 by mybatis.

the class SqlRunnerTest method shouldSelectOne.

@Test
public void shouldSelectOne() throws Exception {
    DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
    runScript(ds, JPETSTORE_DDL);
    runScript(ds, JPETSTORE_DATA);
    Connection connection = ds.getConnection();
    SqlRunner exec = new SqlRunner(connection);
    Map<String, Object> row = exec.selectOne("SELECT * FROM PRODUCT WHERE PRODUCTID = ?", "FI-SW-01");
    connection.close();
    assertEquals("FI-SW-01", row.get("PRODUCTID"));
}
Also used : Connection(java.sql.Connection) DataSource(javax.sql.DataSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21