Results 1 to 11 of 11
  1. #1
    Join Date
    May 2011
    Posts
    5

    Unanswered: 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:

    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:


    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!

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Try using the getConnection method that uses properties. And enter only a user property.

    Andy

  3. #3
    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

  4. #4
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5

  5. #5
    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

  6. #6
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    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

  7. #7
    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?

  8. #8
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Then it appears as if you have to have a password--which is good practice anyway.

    Andy

  9. #9
    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 !!**

  10. #10
    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);

  11. #11
    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 !!**

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •