0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TomeeでDB接続

Posted at

JDBCで接続

    public String connect(){
        
        final String URL = "jdbc:oracle:thin:@localhost:1521/XEPDB1";
        final String USER = "hr";
        final String PASS = "hr";
        final String SQL = "select * from sample_table";

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbCon.class.getName()).log(Level.SEVERE, null, ex);
        }

        try (Connection conn = DriverManager.getConnection(URL,USER,PASS);
                Statement smt = conn.createStatement();
                ResultSet rs = smt.executeQuery(SQL)){
            
            while (rs.next()) {
                System.out.println(
                    "ID:" +rs.getString("ID") + 
                    "VALUE:" +rs.getString("Value"));
            }           

        } catch (Exception ex) {
            Logger.getLogger(DbCon.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }

DataPoolを使って接続

1.「<CATALINA_HOME>\lib」配下にjdbcドライバを配置する。

2.「tomee.xml」を修正する。

tomee.xml
  <Resource id="Oracle Database" type="DataSource">
    JdbcDriver  oracle.jdbc.OracleDriver
    JdbcUrl jdbc:oracle:thin:@localhost:1521/XEPDB1
    UserName    hr
    Password    hr
  </Resource>
    @Resource(name = "Oracle Database")
    private DataSource dataSource;
    
    public String connect(){
        final String SQL = "select * from 'sample_table'";
        
        try(final Connection connection = dataSource.getConnection();
            Statement smt = connection.createStatement();
            ResultSet rs = smt.executeQuery(SQL)){
            
            while (rs.next()) {
                System.out.println(
                    "ID:" +rs.getString("ID") + 
                    "VALUE:" +rs.getString("Value"));
            } 
            
        } catch (SQLException e) {
                    e.printStackTrace();
        }
        return "";
    }

なぜか怒られるので、原因を確認中
java.sql.SQLSyntaxErrorException: unexpected token: sample_table
Caused by: org.hsqldb.HsqlException: unexpected token: sample_table

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?