remotely connecting oracle database with java

Life with George :

I'm trying to figure out how to connect to oracle database remotely, in order to fetch information/data from it. I don't know the steps on how to go about it. I would also like to use datasource to connect with the oracle DB. I'm completely new to this, and if its not too much to ask can I get step by steps on how to do this. I'm using liberty server.

All I have done is read through the internet for something that answers my query but i just cant seem to find what I'm looking for. Below is what I have and I'm trying to see how to achieve my goal from what I have.

In this scenario I want to use datasource and connect remotely with 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();
            }
        }
    }
}
njr :

Your example code is a standalone Java program, although you also tagged the question with websphere-liberty. There are different ways of obtaining a data source from a standalone Java program vs when running in an application server (the latter).

Here is how to achieve it in Liberty.

Edit the server configuration (server.xml) file to enable one of the jdbc features,

<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>

Refer to example configuration on this knowledge center page for more information on data source configuration.

From a web or ejb component (servlet is used here), use resource injection as follows (this does not require the jndi-1.0 feature),

@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();
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307184&siteId=1