将数据库连接到Eclipse

时间:2014-09-20 17:36:26

标签: java database eclipse

我正在尝试用Java创建一个使用oracle数据库存储信息的应用程序。我正在使用Eclipse,我将数据库连接到Eclipse。我现在可以在Data Source Explorer中看到数据库连接。现在我如何在我的应用程序中使用数据库中的表?你能提供教程的链接吗?

1 个答案:

答案 0 :(得分:0)

查看Data Tools Platform usage

的示例

首先,你建立一个连接:

 IConnectionProfile profile = ProfileManager.getInstance().getProfileByName("myprofile");
  IStatus status = profile.connect();
  if (status.getCode() == IStatus.OK)) {
     // success
  } else {
     // failure :(
     if (status.getException() != null) {
        status.getException().printStackTrace();
     }
  } 

然后,使用托管池获取JDBC连接:

  public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
     IManagedConnection managedConnection = ((IConnectionProfile)profile).
        getManagedConnection ("java.sql.Connection");
     if (managedConnection != null) {
        return (java.sql.Connection) managedConnection.getConnection().getRawConnection();
     }
     return null;
  }

此代码当然会访问存储在基于Eclipse的应用程序中的连接配置文件,并且不会在IDE中看到定义的内容。确保不要混淆IDE和应用程序。