-1

I am trying to connect pl*sql developer tool using Java, where I want to run SQL queries. I know how to run sql queries in Oracle database using Java, but not able to run using pl*sql developer. My Oracle database is installed on another server.

Below is my code for database connectivity using Java.

import java.sql.*;
import java.util.*;


public class DatabaseConnectivity {

private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:@localhost:1521/XE";
private static final String DEFAULT_USERNAME = "appuser";
private static final String DEFAULT_PASSWORD = "vivek123";


public static void main(String[] args)
{
    long begTime = System.currentTimeMillis();

    String driver = ((args.length > 0) ? args[0] : DEFAULT_DRIVER);
    String url = ((args.length > 1) ? args[1] : DEFAULT_URL);
    String username = ((args.length > 2) ? args[2] : DEFAULT_USERNAME);
    String password = ((args.length > 3) ? args[3] : DEFAULT_PASSWORD);

    Connection connection = null;

    try
    {
        connection = createConnection(driver, url, username, password);
        DatabaseMetaData meta = connection.getMetaData();
        System.out.println(meta.getDatabaseProductName());
        System.out.println(meta.getDatabaseProductVersion()); 

        String sqlQuery = "select * from student";
        connection.commit();
        
        System.out.println(query(connection, sqlQuery, Collections.EMPTY_LIST));
    }
    catch (Exception e)
    {
        rollback(connection);
        e.printStackTrace();
    }
    finally
    {
        close(connection);
        long endTime = System.currentTimeMillis();
     //   System.out.println("wall time: " + (endTime - begTime) + " ms");
    }
}
public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
{
    Class.forName(driver);

    if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0))
    {
        return DriverManager.getConnection(url);
    }
    else
    {
        return DriverManager.getConnection(url, username, password);
    }
}
public static void close(Connection connection)
{
    try
    {
        if (connection != null)
        {
            connection.close();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

public static void close(Statement st)
{
    try
    {
        if (st != null)
        {
            st.close();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

public static void close(ResultSet rs)
{
    try
    {
        if (rs != null)
        {
            rs.close();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

public static void rollback(Connection connection)
{
    try
    {
        if (connection != null)
        {
            connection.rollback();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

public static List<Map<String, Object>> map(ResultSet rs) throws SQLException
{
    List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

    try
    {
        if (rs != null)
        {
            ResultSetMetaData meta = rs.getMetaData();
            int numColumns = meta.getColumnCount();
            while (rs.next())
            {
                Map<String, Object> row = new HashMap<String, Object>();
                for (int i = 1; i <= numColumns; ++i)
                {
                    String name = meta.getColumnName(i);
                    Object value = rs.getObject(i);
                    row.put(name, value);
                }
                results.add(row);
            }
        }
    }
    finally
    {
        close(rs);
    }

    return results;
}

public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException
{
    List<Map<String, Object>> results = null;

    PreparedStatement ps = null;
    ResultSet rs = null;

    try
    {
        ps = connection.prepareStatement(sql);

        int i = 0;
        for (Object parameter : parameters)
        {
            ps.setObject(++i, parameter);
        }

        rs = ps.executeQuery();
        results = map(rs);
    }
    finally
    {
        close(rs);
        close(ps);
    }

    return results;
}

public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException
{
    int numRowsUpdated = 0;

    PreparedStatement ps = null;

    try
    {
        ps = connection.prepareStatement(sql);

        int i = 0;
        for (Object parameter : parameters)
        {
            ps.setObject(++i, parameter);
        }

        numRowsUpdated = ps.executeUpdate();
    }
    finally    
    {    
        close(ps);    
    }    

    return numRowsUpdated;    
}
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Vivek
  • 103
  • 1
  • 7
  • Sql developer is a client tool. I don’t get well why you want to use it to query a oracle database, instead of doing it directly from your java code. What’s your purpose? – James Jun 27 '18 at 12:53
  • What do you mean "connect plsql developer tool using java" ? Isn't plsql developer just an autonomous tool to be used on its own ? (Oh and thumbs up of course for disclosing a password of yours here ... ) – Erwin Smout Jun 27 '18 at 12:54
  • I dont have permission to that database server so I cant go to that server and run my java code. – Vivek Jun 27 '18 at 13:06
  • I have to write one generic code which can use for many databases but execute from only single machine... – Vivek Jun 27 '18 at 13:08
  • If I want to run sql file using plsql developer tool then how can i connect using java – Vivek Jun 27 '18 at 13:09
  • and about password...Its a demo program and demo database password – Vivek Jun 27 '18 at 13:09
  • Sorry dude, but what you are trying to do makes completly no sense. If I were you I would change the strategy. – James Jun 27 '18 at 16:27

3 Answers3

0

You have mentioned "oracle database in on another server" then why there is localhost in Driver Url?

  • If the database is on another server then in Driver url you have to mention the server's IP instead of localhost. It doesn't matter where your code is running. If the code is connecting to the database on another server then server's IP has to be mentioned instead of localhost. – toTheRescue Jun 28 '18 at 12:03
0

Basically you want to run query on an remote oracle database. For that you just need to pass database connection details like ip address/hostname, port , DB name and user/pwd.

Still not sure what do you mean by 'connecting plsql developer from java code'? Plsql developer also uses JDBC code to connect to any oracle DB just like you would do from your JDBC code.

  • I dont have direct access to that database server. If i want to run any one query then I have to run this query using plsql developer tool from another machine. – Vivek Jun 27 '18 at 14:02
  • then you just install plsql developer tool on the machine you have access to. Then just add the database connection you want to connect to. it's that simple. Why do you want to write java code for that purpose? – Sunil Parihar Jun 29 '18 at 09:07
0

You should use same credentials in java code which you use in pl*sql developer. Java code can be generic for all databases if you are using JDBC.

I dont think there is any backend api's provided for pl*sql which can be called thru java code.