How do I login to 'xe' connection in Oracle 11g with java code?

13,983

If you want to connect as SYS you have to use sys as sysdba

So in your java code try like the following code

    String url = "jdbc:oracle:thin:@//localhost:1521:xe";
    String username = "sys as sysdba";
    String password = "123456";
Connection connection= DriverManager.getConnection(url, username, password);

Regards

Share:
13,983
sweet dreams
Author by

sweet dreams

Life can be very beautiful, only if we choose to make it so.

Updated on July 23, 2022

Comments

  • sweet dreams
    sweet dreams almost 2 years

    I want to make java code that does only this:

    Login to the 'xe' connection in Oracle 11g database. That's all. How can do it ?

    EDIT: yes, i was using JDBC, but unable to login to that connection. My url is jdbc:oracle:thin:@//localhost:1521/xe, username = sys and password = 123456 for the xe or sys connection. Then why can't i login to that connection ?

    EDIT:

    I am very sorry, I forgot to add that I see another error besides the 1st one, i.e.

    SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
    

    This is followed by-

    Exception in thread "main" java.lang.NullPointerException
    
  • sweet dreams
    sweet dreams over 11 years
    For- 1- yes, please refer to the edit in question. 2- why is it wrong ? 3- no. want to create the database/schema/user/other user or whichever uncommon name oracle decided to give it :(
  • sweet dreams
    sweet dreams over 11 years
    Works !I thought that since the username for connection 'xe' is sys and password is 123456, setting my username = sys only in java code would work. Btw, why does "sys as sysdba" work and not just "sys" ?
  • Jacob
    Jacob over 11 years
    Because sys is a privileged user and hence we need to use sys as sysdba. From doc To connect to Oracle Database as a privileged user over a nonsecure connection, you must be authenticated by a password file. When using password file authentication, the database uses a password file to keep track of database user names that have been granted the SYSDBA or SYSOPER system privilege. This form of authentication is discussed in "Using Password File Authentication".
  • Nenad Bulatović
    Nenad Bulatović over 9 years
    Shouldn't be there ; after String password = "123456" ?
  • Jacob
    Jacob over 9 years
    @NenadBulatovic Unquestionably yes, it was a typo and corrected. Thanks