使用Java程序创建Oracle模式

时间:2013-02-19 13:59:51

标签: java sql oracle oracle11g

我使用Toad数据库建模器创建了Oracle Scheme。我通常使用SQL开发人员使用SQL语句导入新方案。现在我想创建自动执行此过程的Java程序。到目前为止,我创建了这个Java代码,它打开了与Oracle的连接:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import org.junit.BeforeClass;
import org.junit.Test;

public class OracleCreateScheme
{

    public OracleCreateScheme()
    {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {
        // rcarver - setup the jndi context and the datasource
        try
        {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES,
                    "org.apache.naming");
            InitialContext ic = new InitialContext();

            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            // Construct DataSource
            OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
            ds.setURL("jdbc:oracle:thin:@host:port:db");
            ds.setUser("admin");
            ds.setPassword("qwerty");

            ic.bind("java:/comp/env/jdbc/oracle", ds);
        }
        catch (NamingException ex)
        {
            //Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Test
    public void createOracleScheme() throws SQLException, NamingException
    {

        Context initContext = new InitialContext();
        Context webContext = (Context) initContext.lookup("java:/comp/env");

        DataSource ds = (DataSource) webContext.lookup("jdbc/Oracle");

        String SqlStatement = null;

        if (ds == null)
        {
            throw new SQLException();
        }


        Connection conn = ds.getConnection();
        if (conn == null)
        {
            throw new SQLException();
        }

        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try
        {
            conn.setAutoCommit(false);
            boolean committed = false;
            try
            {

                ps = conn.prepareStatement(SqlStatement);
                resultSet = ps.executeQuery();

                conn.commit();
                committed = true;
            }
            finally
            {
                if (!committed)
                {
                    conn.rollback();
                }
            }
        }
        finally
        {
            ps.close();
            conn.close();
        }

    }
}

您能告诉我如何修改代码以便将SQL文件执行到Oracle中吗? 还有哪些现成的Java程序可以用于同一目的吗?

1 个答案:

答案 0 :(得分:3)

我建议使用现有的东西而不是编写自己的实现。这项任务有许多强大的工具:

Maven SQL插件  [mojo.codehaus.org/sql-maven-plugin/examples/execute.html]

DBUnit Fileloader  [www.dbunit.org/howto.html#fileloader]

或春天(如果你被迫使用它:-))  [static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html]

检查本文:  [stackoverflow.com/questions/7565420/create-and-clean-db-setup-only-once-for-testing-all-daos]

希望这会有所帮助......