java.sql.SQLException:Io异常:端口号的数字格式无效

use*_*751 -2 java jdbc ojdbc

package jdbcconnection;

import java.sql.*;

public class Jdbc2{

    public static void main(String[] args) throws Throwable {

        //Resgister the driver through 

         Class.forName("oracle.jdbc.driver.OracleDriver");
         System.out.println("registered driver successfully");
         //Create the connection and assign to connection reference
         Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:CUSTDB", "scott", "tiger");
         System.out.println("connection successsfully");
         //create a statement through connection reference and assign to statement reference
         Statement stmt=con.createStatement();
         System.out.println("statement object created successfully");
         //call the executequery method through statement reference and pass the query as argument.
         ResultSet rs=stmt.executeQuery("select * from emp");

         System.out.println("query is executed");

         while(rs.next()){
             int i=rs.getInt(1);
             String str=rs.getString(2);
             String str1=rs.getString(3);
             int i1=rs.getInt(4);
             System.out.println(i+"\t"+str+"\t"+str1+"\t"+i1);    
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误 -

Exception in thread "main" java.sql.SQLException: Io exception: Invalid number format for port number
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:439)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at jdbcconnection.Jdbc2.main(Jdbc2.java:13)
Run Code Online (Sandbox Code Playgroud)

Mik*_*yev 5

我注意到指定 SID 而不是带有 a 的服务名称/不起作用。所以下面的说法是不正确的:

jdbc:oracle:thin:@my.host.com:1521/DB_SID
Run Code Online (Sandbox Code Playgroud)

它会抛出listener doesn't know of service ...错误。当您使用 SID 时,您应该使用冒号而不是斜杠。

这个会抱怨端口号格式无效

jdbc:oracle:thin://@my.host.com:1521:DB_SID    
Run Code Online (Sandbox Code Playgroud)

您注意到标志前的双斜杠了吗@?只要删除它们,一切都会好起来的。