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)
答案 0 :(得分:1)
您的网址不正确。
应该是
getConnection("jdbc:oracle:thin:@localhost:portnum:CUSTDB", "scott", "tiger");
示例:
"jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger"
其中xe是数据库名称。
希望这有帮助。
答案 1 :(得分:0)
它在堆栈跟踪中:&#34;端口号的编号格式无效&#34;
您应该输入有效的端口号而不是CUSTDB
答案 2 :(得分: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
是的,请提供更多有关您工作的信息并构建一个正确的问题而不是仅发布错误堆栈!
答案 3 :(得分:0)
我注意到指定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
您是否注意到@
符号前的双斜线?只需删除它们,一切都会好的。