java SQLNonTransientException 的可能解决方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16673316/
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
Possible solutions for a SQLNonTransientException
提问by user2406060
I am very new to database connections with Java and am having a difficult time connecting to the database I was provided. So far I've learned that installing the correct DB2 drivers may have been an issue and putting them in the CLASSPATH
for the program. I have done this and I still am not able to establish a connection. Please Help!
我对使用 Java 的数据库连接非常陌生,并且很难连接到我提供的数据库。到目前为止,我了解到安装正确的 DB2 驱动程序可能是一个问题,并将它们放入CLASSPATH
程序中。我已经这样做了,但我仍然无法建立连接。请帮忙!
Here is a print out of my error report in Eclipse:
这是我在 Eclipse 中的错误报告的打印件:
com.ibm.db2.jcc.am.SqlNonTransientConnectionException: [jcc][t4][10380][11951][4.13.127] Required property "URLname" is unknown host. ERRORCODE=-4222, SQLSTATE=08001 at com.ibm.db2.jcc.am.id.a(id.java:667) at com.ibm.db2.jcc.am.id.a(id.java:60) at com.ibm.db2.jcc.am.id.a(id.java:103) at com.ibm.db2.jcc.t4.a.(a.java:231) at com.ibm.db2.jcc.t4.b.a(b.java:1901) at com.ibm.db2.jcc.am.kb.a(kb.java:700) at com.ibm.db2.jcc.am.kb.(kb.java:653) at com.ibm.db2.jcc.t4.b.(b.java:332) at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:231) at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:197) at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:472) at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:113) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at ServerAcessDemo.main(ServerAcessDemo.java:23)
com.ibm.db2.jcc.am.SqlNonTransientConnectionException: [jcc][t4][10380][11951][4.13.127] 必需的属性“URLname”是未知主机。ERRORCODE=-4222,SQLSTATE=08001 在 com.ibm.db2.jcc.am.id.a(id.java:667) 在 com.ibm.db2.jcc.am.id.a(id.java:60)在 com.ibm.db2.jcc.am.id.a(id.java:103) 在 com.ibm.db2.jcc.t4.a.(a.java:231) 在 com.ibm.db2.jcc。 t4.ba(b.java:1901) 在 com.ibm.db2.jcc.am.kb.a(kb.java:700) 在 com.ibm.db2.jcc.am.kb.(kb.java:653) ) 在 com.ibm.db2.jcc.t4.b.(b.java:332) 在 com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:231) 在 com.ibm.db2.jcc.DB2SimpleDataSource .getConnection(DB2SimpleDataSource.java:197) 在 com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:472) 在 com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:113) 在 java。 sql.DriverManager。
Here is the code I have developed so far:
这是我迄今为止开发的代码:
import java.sql.*;
public class ServerAcessDemo{
// jdbc driver name and database URL
static final String JDBC_DRIVER = "com.ibm.db2.jcc.DB2Driver";
static final String DB_URL = "jdbc:db2://URLname/portNumber";
// Database credentials
static final String USER = "userID";
static final String PASSWORD = "password";
public static void main(String[] args){
Connection conn = null;
try{
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
}
catch(Exception e){
// handle errors for Class.forName
e.printStackTrace();
}
finally{
// finally block used to close resources
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
}
回答by Piotr Kochański
I am not sure if this is as simple as that, but you set:
我不确定这是否就这么简单,但是您设置了:
static final String DB_URL = "jdbc:db2://URLname/portNumber";
and the error is Required property "URLname" is unknown host
, so it seems that you have supplied wrong DB_URL value, it should be something like jdbc:db2://localhost:50000/your_database_name
, assuming your DB runs locally on 50000 port, you need to provide right database name.
错误是Required property "URLname" is unknown host
,因此您似乎提供了错误的 DB_URL 值,它应该类似于jdbc:db2://localhost:50000/your_database_name
,假设您的数据库在 50000 端口本地运行,您需要提供正确的数据库名称。