-3
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)
4

4 回答 4

3

我注意到用 a 指定 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    

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

于 2018-03-28T06:08:15.973 回答
1

您的网址不正确。

它应该是

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

例子:

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

其中 xe 是数据库名称。

希望这可以帮助。

于 2014-03-31T16:24:32.947 回答
0
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

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

于 2014-03-31T16:33:04.687 回答
0

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

于 2014-03-31T15:50:39.577 回答