我正在使用oracle 11g xe版本。所以我已经建立了类路径并继续执行该程序,但是遇到了这些错误:
Connecting to database...
java.sql.SQLRecoverableException: IO Error: Invalid number format for port number
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:478)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:547)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:225)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:556)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at jdbcdisplay.main(jdbcdisplay.java:24)
Caused by: oracle.net.ns.NetException: Invalid number format for port number
at oracle.net.resolver.AddrResolution.resolveSimple(AddrResolution.java:497)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:394)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:873)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:258)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1577)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:353)
... 7 more
GoodBye!!
现在我还没有使用任何IDE,也没有在windows中完成。我正在尝试通过linux终端进行连接,所有可用的帮助几乎都是针对Windows的。 我用于程序的连接字符串是:
String jdbcUrl="jdbc:oracle:thin:@localhost:1521/XE:orcl";
String user="Sourajyoti";
String password="*******";
conn=DriverManager.getConnection(jdbcUrl,user,password);
我使用的代码如下:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
class jdbcdisplay
{
public static void main(String[] args)
{
Connection conn=null;
try
{
String driver="oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
System.out.println("Connecting to database...");
String jdbcUrl="jdbc:oracle:thin:@localhost:1521/XE:orcl";
String user="Sourajyoti";
String password="******";
conn=DriverManager.getConnection(jdbcUrl,user,password);
Statement stmt=conn.createStatement();
String sql="SELECT ENAME,SALARY,HIREDATE FROM EMP";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next())
{
String name=rs.getString("ENAME");
double salary=rs.getDouble(2);
Date date=rs.getDate("HIREDATE");
System.out.print("Name: "+name);
System.out.print("Salary: "+salary);
System.out.println("Hiredate: "+date);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println("GoodBye!!");
}
}
答案 0 :(得分:1)
我在使用REST连接和检索Oracle数据库中的数据时遇到了这个问题。
jdbc:oracle:thin:Herong/TopSecret@localhost:1521:XE
jdbc:oracle:thin:Herong/TopSecret@:1521:XE
jdbc:oracle:thin:Herong/TopSecret@//localhost:1521/XE
jdbc:oracle:thin:Herong/TopSecret@//:1521/XE
jdbc:oracle:thin:Herong/TopSecret@//localhost/XE
jdbc:oracle:thin:Herong/TopSecret@///XE
这些都是连接URL的有效变体,但我最终必须完全删除该端口。
"type": "jdbc",
"name": "test",
"connection": {
"driver": "oracle.jdbc.OracleDriver",
"url": "jdbc:oracle:thin:@//QAORACLE11G.server.local/DatabaseName",
"user": "QA_DBUSER",
"password": "QA_DBPASSWORD"
},
答案 1 :(得分:0)
好的不同是我的连接字符串,我将其更改为:
String jdbcUrl="jdbc:oracle:thin:@localhost:1521:xe";
String user="Sourajyoti";
String password="******";