If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Can't access DB2 DataBase through Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-24-11, 16:07
billys13 billys13 is offline
Registered User
 
Join Date: May 2011
Posts: 5
Can't access DB2 DataBase through Java

Hello to everyone. This is my first post so I apologize if it's in the wrong section or if my question has already been answered.

I've installed DB2 Express Edition on my computer (running Windows 7) and created my database. When I installed DB2, I chose to use my local account as administrator (so I use the username I have in Windows and don't have a password, since I've chosen not to have one in my Windows account).

My problem is that I can't access my database with Java (I use JCreator LE 5.00 as my IDE). It seems that it can't login with the username I provide. Here is my code:


Code:
String url = "jdbc:db2://localhost:50000/DBName";

		Connection con;

		try
		{
			Class.forName("com.ibm.db2.jcc.DB2Driver");
		}
		catch (java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(url, "userName", "");
		}

		catch (SQLException e) {
			System.err.println("SQLException "+e.getMessage());
		}

The problem is in the bold line. The output says:

Quote:
SQLException [jcc] [t4] [10107] [11233] [3.50.152] The length of the password, 0, is not allowed. ErrorCode =- 4461, SQLSTATE = 42815

When I try not to insert my username and password, so the bold line becomes like this:


Code:
con = DriverManager.getConnection(url);

I get the following message:


Quote:
SQLException [jcc] [t4] [10205] [11234] [3.50.152] Null userid is not supported. ErrorCode =- 4461, SQLSTATE = 42815



So, from what I understand, I have to insert a password, but I don't have any, since I don't use a password in my Windows account.


Could anyone help me? Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 05-25-11, 09:47
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try using the getConnection method that uses properties. And enter only a user property.

Andy
Reply With Quote
  #3 (permalink)  
Old 05-25-11, 12:30
billys13 billys13 is offline
Registered User
 
Join Date: May 2011
Posts: 5
So, copying from Oracle's site, I should use this method right?

Code:
DriverManager.getConnection(String url, Properties info);
Because I am new in Java and I don't know what to write in the Properties field, could you give me an example?

Thanks for the answer!


Quote:
Originally Posted by ARWinner View Post
Try using the getConnection method that uses properties. And enter only a user property.

Andy
Reply With Quote
  #4 (permalink)  
Old 05-25-11, 13:27
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Reply With Quote
  #5 (permalink)  
Old 05-26-11, 19:47
billys13 billys13 is offline
Registered User
 
Join Date: May 2011
Posts: 5
The only properties I find in this page are to set the username and password (instead od doing that directly in the getConnection method), which gives me the same result as before. Thanks though for the help!


I finally put a password in my Windows account (which I wanted to avoid) and now it seems to be working with this password.

It would be interesting though if there is a way to solve that, so if anyone knows something more, please share
Reply With Quote
  #6 (permalink)  
Old 05-27-11, 09:09
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You were supposed to figure out that the Properties is a collection and that you do not have to add all the items in the example. I had hoped that you would try this:

Code:
Properties properties = new Properties(); // Create Properties object
properties.put("user", "db2adm");         // Set user ID for connection
String url = "jdbc:db2:toronto";
                                          // Set URL for data source
Connection con = DriverManager.getConnection(url, properties); 
                                          // Create connection
I do not know if it will work or not but it is worth a try.

Andy
Reply With Quote
  #7 (permalink)  
Old 05-27-11, 13:40
billys13 billys13 is offline
Registered User
 
Join Date: May 2011
Posts: 5
Yeap I've tried that too, but when I set only the username as a property and not the password it gives me a message saying that null password is not supported. Is there maybe a character that I could use as a password that would be interpreted as a null password?
Reply With Quote
  #8 (permalink)  
Old 05-27-11, 22:24
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Then it appears as if you have to have a password--which is good practice anyway.

Andy
Reply With Quote
  #9 (permalink)  
Old 05-30-11, 00:56
singhipst singhipst is offline
Registered User
 
Join Date: Jul 2006
Location: Bangalore
Posts: 57
I used below code and it worked for me.
You can try this

Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DB2Conn {

	public String checkDB2Conn(String serverName, int portNumber, String dbName, String userName, String password ){
		Connection conn	= null;
		String url="jdbc:db2://"+serverName+":"+portNumber+"/"+dbName;		
		try {
			Class.forName ("com.ibm.db2.jcc.DB2Driver");
			conn = DriverManager.getConnection(url, userName, password);
	        Statement stmt = conn.createStatement();
	        ResultSet rset = stmt.executeQuery("SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1");	        
	        while (rset.next()){
	        	//System.out.println (rset.getString(1));
	        }
	        stmt.close();
	        conn.close();
		}catch (SQLException se) {			
			return se.toString();
		}catch (Exception e) {			
			e.printStackTrace();
			return "Connection Failed";
		}
		return "Connection Succeed";
	}
}
__________________
Ritesh Kumar Singh
IBM Certified DB2 DBA for LUW
**Knowledge Is Theft If Not Shared !!**
Reply With Quote
  #10 (permalink)  
Old 06-01-11, 04:30
billys13 billys13 is offline
Registered User
 
Join Date: May 2011
Posts: 5
Hey singhipst. Do you have a password in the account you use to login?


I always got an error when I use the following instruction, when I had a null password.


Code:
conn = DriverManager.getConnection(url, userName, password);
Reply With Quote
  #11 (permalink)  
Old 07-19-11, 00:31
singhipst singhipst is offline
Registered User
 
Join Date: Jul 2006
Location: Bangalore
Posts: 57
Yes, We should have the password which is set on OS for the account, Since DB2 Uses OS authentication.

While login into OS and then connecting to DB2 CLP, so without giving password also this will connect due to default OS authentication. But while using JAVA you need to provide the login credentials.
__________________
Ritesh Kumar Singh
IBM Certified DB2 DBA for LUW
**Knowledge Is Theft If Not Shared !!**
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On