URL string format for connecting to Oracle database with JDBC

前端 未结 7 1440
予麋鹿
予麋鹿 2020-12-02 09:44

I\'m a newbie to Java-related web development, and I can\'t seem to get a simple program with JDBC working. I\'m using off-the-shelf Oracle 10g XE and the Eclipse EE IDE. Fr

相关标签:
7条回答
  • 2020-12-02 10:28
    String host = <host name>
    String port = <port>
    String service = <service name>
    String dbName = <db schema>+"."+service
    String url = "jdbc:oracle:thin:@"+host+":"+"port"+"/"+dbName
    
    0 讨论(0)
  • 2020-12-02 10:28
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());         
    connection = DriverManager.getConnection("jdbc:oracle:thin:@machinename:portnum:schemaname","userid","password");
    
    0 讨论(0)
  • 2020-12-02 10:33

    if you are using oracle 10g expree Edition then:
    1. for loading class use DriverManager.registerDriver (new oracle.jdbc.OracleDriver()); 2. for connecting to database use Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password@localhost:1521:xe");

    0 讨论(0)
  • 2020-12-02 10:38

    I'm not a Java developer so unfortunatly I can't comment on your code directly however I found this in an Oracle FAQ regarding the form of a connection string

    jdbc:oracle:<drivertype>:<username/password>@<database>
    

    From the Oracle JDBC FAQ

    http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#05_03

    Hope that helps

    0 讨论(0)
  • 2020-12-02 10:45

    There are two ways to set this up. If you have an SID, use this (older) format:

    jdbc:oracle:thin:@[HOST][:PORT]:SID
    

    If you have an Oracle service name, use this (newer) format:

    jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE
    

    Source: this OraFAQ page

    The call to getConnection() is correct.

    Also, as duffymo said, make sure the actual driver code is present by including ojdbc6.jar in the classpath, where the number corresponds to the Java version you're using.

    0 讨论(0)
  • 2020-12-02 10:46

    The correct format for url can be one of the following formats:

    jdbc:oracle:thin:@<hostName>:<portNumber>:<sid>;  (if you have sid)
    jdbc:oracle:thin:@//<hostName>:<portNumber>/serviceName; (if you have oracle service name)
    

    And don't put any space there. Try to use 1521 as port number. sid (database name) must be the same as the one which is in environment variables (if you are using windows).

    0 讨论(0)
提交回复
热议问题