【问题标题】:java.sql.SQLException: Io exception: Invalid number format for port numberjava.sql.SQLException:Io 异常:端口号的数字格式无效
【发布时间】:2014-03-31 15:47:04
【问题描述】:
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);    
         }
    }
}

错误--

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)

【问题讨论】:

  • 我们很乐意提供帮助,但您需要在此处实际格式化问题。只是复制粘贴代码不会得到任何答案,只是投票被遗忘。我们不知道问题是什么(什么是破坏),或有关问题的任何信息。我们有代码,一些文本错误,没有其他可做的了。大多数人在尝试帮助/解决这个问题之前还需要更多的东西......不管它是什么。

标签: java jdbc ojdbc


【解决方案1】:

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

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

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

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

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

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

【讨论】:

    【解决方案2】:

    您的网址不正确。

    应该是

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

    例子:

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

    其中 xe 是数据库名称。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        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);
        

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

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

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-18
        • 2014-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多