简体   繁体   中英

connect to database using JDBC in java

I'm trying to connect to an Oracle database using JDBC Driver and I'm handle with an error: "java.sql.SQLException: Invalid Oracle URL specified".

My code is the following:

import java.sql.*;
public class L9
{
    public static void main(String args[])
    {
         try
         {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              Connection con = DriverManager.getConnection("jdbc:oracle:thin;@localhost:1521:xe","user","password");
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery("select * from table");
              while(rs.next())
                    System.out.println(rs.getInt(1) + " "+rs.getString(2)+ " "+rs.getString(3));
              con.close();
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
    }
}

Anyone knows what is the problem?

It should be

jdbc:oracle:thin:@localhost:1521:xe

instead of

jdbc:oracle:thin;@localhost:1521:xe

(note the : after the "thin")

It is better to use a long format connection URL where you have the ability to pass connection descriptors. Easy Connection URL (jdbc:oracle:thin:@//localhost:1521/myorcldbservicename) ) does establish the connection but does not provide any High Availability capabilities.

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM