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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22766133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:46:12  来源:igfitidea点击:

java.sql.SQLException: Io exception: Invalid number format for port number

javajdbcojdbc

提问by user3481751

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);    
         }
    }
}

error--

错误 -

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)

回答by disklosr

It's in the stack trace: "Invalid number format for port number" You should put a valid port number instead of CUSTDB

它在堆栈跟踪中:“端口号的数字格式无效”您应该输入有效的端口号而不是 CUSTDB

回答by Sri777

Your URL is incorrect.

您的网址不正确。

It should be

它应该是

getConnection("jdbc:oracle:thin:@localhost:portnum:CUSTDB", "scott", "tiger");

Example:

例子:

"jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger"

where xe is the database name.

其中 xe 是数据库名称。

Hope this helps.

希望这可以帮助。

回答by Ritikesh

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);

This is incorrect too! You need to use field names to get values instead of numbers like 1,2,3 etc. And yeah, do check with your port number to get around with the port exception! Check this for a demo on JDBC: http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

这也是不对的!您需要使用字段名称来获取值而不是 1、2、3 等数字。是的,请检查您的端口号以解决端口异常!在 JDBC 上查看演示:http: //www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

And yeah, please be more informative about your work and frame a proper question instead of just posting the Error stack!

是的,请对您的工作提供更多信息并提出适当的问题,而不仅仅是发布错误堆栈!

回答by Mikayil Abdullayev

What I've noticed is specifying SID instead of Service Name with a /does not work. So the following is not correct:

我注意到的是用 a 指定 SID 而不是服务名称是/行不通的。所以以下是不正确的:

jdbc:oracle:thin:@my.host.com:1521/DB_SID

It's going to throw listener doesn't know of service ...error. When you use SID, you should put a colon instead of a slash.

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

And this one will complain about the invalid port number format

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

jdbc:oracle:thin://@my.host.com:1521:DB_SID    

Did you notice the double slashes before the @sign? Just remove them and everything will be ok.

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