使用 java 远程连接 oracle 数据库
remotely connecting oracle database with java
我正在尝试弄清楚如何远程连接到 oracle 数据库,以便从中获取 information/data。我不知道如何去做的步骤。我还想使用数据源连接 oracle 数据库。我对此完全陌生,如果要求不高的话,我可以逐步了解如何执行此操作。我正在使用自由服务器。
我所做的只是通过互联网阅读可以回答我查询的内容,但我似乎无法找到我正在寻找的内容。以下是我所拥有的,我正在尝试了解如何从我所拥有的东西中实现我的目标。
在这种情况下,我想使用数据源并与 oracle DB 远程连接。
package com.dBconnect;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DatabaseUtility {
private static DataSource dataSource;
static Connection conn;
public static void main(String ars[]) {
try {
conn = dataSource.getConnection();
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
您的示例代码是一个独立的 Java 程序,尽管您还使用 websphere-liberty 标记了问题。从独立的 Java 程序与在应用程序服务器中 运行 时(后者)获取数据源有不同的方法。
以下是如何在 Liberty 中实现它。
编辑服务器配置 (server.xml) 文件以启用 jdbc 功能之一,
<featureManager>
<feature>jdbc-4.2</feature>
<feature>jndi-1.0</feature> <!-- for JNDI lookup of the data source -->
<feature>servlet-4.0</feature> <!-- or other features that you want to use -->
</featureManager>
<dataSource id="myDataSource" jndiName="jdbc/myOracleDataSource">
<jdbcDriver libraryRef="OracleLib"/>
<properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/SAMPLEDB" user="user1" password="password1"/>
</dataSource>
<library id="OracleLib">
<file name="C:/Oracle/lib/ojdbc8.jar"/>
</library>
有关数据源配置的详细信息,请参阅 this knowledge center page 上的示例配置。
从 web 或 ejb 组件(这里使用 servlet),使用资源注入如下(这不需要 jndi-1.0 特性),
@WebServlet("/*")
public class ExampleServlet extends javax.servlet.http.HttpServlet {
@Resource(lookup = "jdbc/myOracleDataSource")
private DataSource dataSource;
public void init() throws ServletException {
// Here is another way of accessing the data source - via JNDI lookup.
// This requires the jndi-1.0 feature
DataSource anotherDataSource = InitialContext.doLookup("jdbc/myOracleDataSource");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
conn = dataSource.getConnection();
System.out.println("connection established");
response.getWriter().println("connection established");
} catch (Exception e) {
e.printStackTrace();
response.getWriter().println("failed to establish connection: " + e);
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我正在尝试弄清楚如何远程连接到 oracle 数据库,以便从中获取 information/data。我不知道如何去做的步骤。我还想使用数据源连接 oracle 数据库。我对此完全陌生,如果要求不高的话,我可以逐步了解如何执行此操作。我正在使用自由服务器。
我所做的只是通过互联网阅读可以回答我查询的内容,但我似乎无法找到我正在寻找的内容。以下是我所拥有的,我正在尝试了解如何从我所拥有的东西中实现我的目标。
在这种情况下,我想使用数据源并与 oracle DB 远程连接。
package com.dBconnect;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DatabaseUtility {
private static DataSource dataSource;
static Connection conn;
public static void main(String ars[]) {
try {
conn = dataSource.getConnection();
System.out.println("connection established");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
您的示例代码是一个独立的 Java 程序,尽管您还使用 websphere-liberty 标记了问题。从独立的 Java 程序与在应用程序服务器中 运行 时(后者)获取数据源有不同的方法。
以下是如何在 Liberty 中实现它。
编辑服务器配置 (server.xml) 文件以启用 jdbc 功能之一,
<featureManager>
<feature>jdbc-4.2</feature>
<feature>jndi-1.0</feature> <!-- for JNDI lookup of the data source -->
<feature>servlet-4.0</feature> <!-- or other features that you want to use -->
</featureManager>
<dataSource id="myDataSource" jndiName="jdbc/myOracleDataSource">
<jdbcDriver libraryRef="OracleLib"/>
<properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/SAMPLEDB" user="user1" password="password1"/>
</dataSource>
<library id="OracleLib">
<file name="C:/Oracle/lib/ojdbc8.jar"/>
</library>
有关数据源配置的详细信息,请参阅 this knowledge center page 上的示例配置。
从 web 或 ejb 组件(这里使用 servlet),使用资源注入如下(这不需要 jndi-1.0 特性),
@WebServlet("/*")
public class ExampleServlet extends javax.servlet.http.HttpServlet {
@Resource(lookup = "jdbc/myOracleDataSource")
private DataSource dataSource;
public void init() throws ServletException {
// Here is another way of accessing the data source - via JNDI lookup.
// This requires the jndi-1.0 feature
DataSource anotherDataSource = InitialContext.doLookup("jdbc/myOracleDataSource");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
conn = dataSource.getConnection();
System.out.println("connection established");
response.getWriter().println("connection established");
} catch (Exception e) {
e.printStackTrace();
response.getWriter().println("failed to establish connection: " + e);
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}