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!
Here is a print out of my error report in 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)
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
}
}
Best Answer
I am not sure if this is as simple as that, but you set:
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 likejdbc:db2://localhost:50000/your_database_name
, assuming your DB runs locally on 50000 port, you need to provide right database name.