Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5932059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:46:55+00:00 2026-05-22T14:46:55+00:00

Okay, this is kinda infuriating. . . I can connect to the oracle database

  • 0

Okay, this is kinda infuriating. . . I can connect to the oracle database on a server in another city but I can’t connect to the oracle database on my own computer? What am I missing? Below is my code. . . I have even tried with both ports 1521 (and 8080 for my Tomcat server).

try
{
      Connection conn;

      // Load the JDBC driver
      String driverName = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driverName);

      // Create a connection to the database            
      String url = "jdbc:oracle:thin:@localhost"; 

      conn = DriverManager.getConnection(url,"root","password");     

 }   
 catch (IOException e) 
 {
    System.out.println("Caught I/O Exception);
    e.printStackTrace();
    throw e;
 } 
 catch (SQLException e) 
 {
    System.out.println("Caught SQL Exception);
    e.printStackTrace();
    throw e;
 }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:46:56+00:00Added an answer on May 22, 2026 at 2:46 pm

    The URL you have is incorrect. Try this:

    String url = "jdbc:oracle:thin:@host:1521:database";
    

    You have to use the right username and password and substitute the values for host and database that apply to your case. Don’t just use that string verbatim.

    Is Oracle running? Can you connect using SQL*Plus? If SQL*Plus can’t connect, Java won’t be able to, either.

    And the JDBC driver JAR that matches your JDK and database versions needs to be in the CLASSPATH. You should be able to find the driver JAR in the distro you downloaded.

    Don’t set it using any CLASSPATH environment variables; use the java -cp command line argument.

    Modify this code to try it out. I know that it works.

    package persistence;
    
    import java.sql.*;
    import java.util.*;
    
    /**
     * util.DatabaseUtils
     * User: Michael
     * Date: Aug 17, 2010
     * Time: 7:58:02 PM
     */
    public class DatabaseUtils
    {
        private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
        private static final String DEFAULT_URL = "jdbc:oracle:thin:@host:1521:database";
        private static final String DEFAULT_USERNAME = "username";
        private static final String DEFAULT_PASSWORD = "password";
    /*
        private static final String DEFAULT_DRIVER = "org.postgresql.Driver";
        private static final String DEFAULT_URL = "jdbc:postgresql://localhost:5432/party";
        private static final String DEFAULT_USERNAME = "pgsuper";
        private static final String DEFAULT_PASSWORD = "pgsuper";
    */
    /*
        private static final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
        private static final String DEFAULT_URL = "jdbc:mysql://localhost:3306/party";
        private static final String DEFAULT_USERNAME = "party";
        private static final String DEFAULT_PASSWORD = "party";
    */
    
        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 PERSON_ID, FIRST_NAME, LAST_NAME FROM PERSON ORDER BY LAST_NAME";
                System.out.println("before insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
    
                connection.setAutoCommit(false);
                String sqlUpdate = "INSERT INTO PERSON(FIRST_NAME, LAST_NAME) VALUES(?,?)";
                List parameters = Arrays.asList( "Foo", "Bar" );
                int numRowsUpdated = update(connection, sqlUpdate, parameters);
                connection.commit();
    
                System.out.println("# rows inserted: " + numRowsUpdated);
                System.out.println("after insert: " + 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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm kinda stuck on really silly problem, but still can't figure out. I'm
Okay this may be a simple question but I have yet to come with
okay so this is probably a soft pitch question for sombody, but I want
Okay this one may be a bit out from left field, but I'm going
Okay so my code looks kinda like this // Returns the note of a
Okay. This is propably very basic and noobie, but I have about 20+ stores
Pointless Dribble Okay This is another weird one from me, i want to thank
Hello kind computing people, Okay this is super broad, but I thought I'd give
Okay, so this question could hardly be more basic, but... Why does this CSS
Okay I know this is really stupid.But how to i write SELECT Column1 FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.