Solved

Trouble connecting to the database (IBM DB2 from a servlet)

Posted on 2004-04-17
7
268 Views
Last Modified: 2010-07-27
Hi everyone,

I am currently doing a small web application which is a HTML form which allows the user to search and add to a database.

Everything seemed to be working fine until i reinstalled DB2.
Now when i run the program, it gets caught on the sql exception whilst trying to connect:

Here is the code:

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection(URL, "administrator", "password");
        }
        catch(ClassNotFoundException cfne)
        {
            System.err.println("Failed to load JDBC/ODBC driver.");
            jdbcDriverError = true;
        }
        catch(SQLException sqle)
        {
            System.err.println("Unable to connect! - " +
                                "Check the connection string");   <-----------THIS SHOWS IN THE LOG FILE

            sqlError = true;
        }
        return;
    }

can anyone please help me
0
Comment
Question by:lamb0
  • 3
  • 2
7 Comments
 
LVL 1

Expert Comment

by:pcazella
ID: 10851413
Why did you reinstall DB2...did you upgrade or where you having other issues with the former install.

Did you also install the latest ODBC / JDBC drivers for DB2 on the web server?

0
 

Author Comment

by:lamb0
ID: 10855319
>>Why did you reinstall DB2...
I wanted to add a different user and that was the only way i knew how

>>Did you install the latest ODBC / JDBC drivers for DB2 on the web server?
NO, i didnt know i had to.
Can you please post a link for the drivers I'm running windows 2000 ,db2 v.7 and tomcat 5.0
I have had a look on google but i dont want to install the wrong one.

Thanks
0
 
LVL 1

Expert Comment

by:pcazella
ID: 10856807
If you are currently not getting the "Failed to load JDBC/ODBC driver" error message which means that the driver is loading, but you are not connecting to the database.  If your username and password are correct, then it should work but it's not...hence the question.

Okay, so I googled the driver you are currently using...sun.jdbc.odbc.JdbcOdbcDriver and it seems that it is a bridge driver or one for experimentaly use only.

http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/bridge.doc.html

I then found a page from the DB2 information pages which may help solve your issue:
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp?topic=/com.ibm.db2.udb.doc/ad/cjvjcccn.htm

If you installed teh JDBC drivers as part of the DB2 set up, then try changing your driver call to:
Class.forName("com.ibm.db2.jcc.DB2Driver");

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:lamb0
ID: 10857164
I tried changing my driver call line like you suggested and that seems to have gotten me past the sql error and is now giving me a jdbc error, i dont know what driver i installed (or if i even installed one ?)

This method comes directly after the one i posted earlier:

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                                        throws ServletException, IOException
    {
        String id, surname, givenName, abn, streetAdd, suburb, state, postcode, email, tel, mob;

        output = res.getWriter();

        if(sqlError)
            output.println("<h2>Fatal error using DSN to connect to database. " +
                            "Please advise your leader.</h2>");
        else
        {
            if(jdbcDriverError)
                output.println("<h2>Fatal error loading Java database driver. " +
                                "Please advise your leader.</h2>");  <<---- GETS STUCK HERE NOW
            else
            {
              id = req.getParameter("clientIDField");
              surname = req.getParameter("surnameField");
              givenName = req.getParameter("givenNameField");
              abn = req.getParameter("abnField");
              streetAdd = req.getParameter("streetField");
              suburb = req.getParameter("suburbField");
              state = req.getParameter("stateField");
              postcode = req.getParameter("postcodeField");
              mob = req.getParameter("mobileField");
              tel = req.getParameter("teleField");
              email = req.getParameter("emailField");

              res.setContentType("text/html");

              if (submit.equals("add"))
              {
                if (id.equals("") ||
                    givenName.equals("") ||
                    surname.equals("") ||
                    streetAdd.equals("") ||
                    suburb.equals("") ||
                    postcode.equals("") ||
                    state.equals(""))
                {
                  output.println("<h3>Please click the back button and fill in" +
                                 " all required fields.</h3>");
                  output.close();
                  return;
                }

                client = new ClientEntity(id, surname, givenName, abn, streetAdd, suburb,
                                          state, postcode, mob, tel, email);

                boolean success = query.insertIntoDB(client.toString());
   
                if (success)
                  output.print("<h2><font face = \"Arial\">Thank you, " + givenName +
                               ", for registering.</font></h2>");

                output.close();
              }
              else if(submit.equals("search"))
              {
                try
                {
                  client = new ClientEntity(id, surname, givenName, abn, streetAdd, suburb,
                          state, postcode, mob, tel, email);

                  query.searchDB(client);
                }
                catch(SQLException sqle)
                {
                  sqle.printStackTrace();
                }
              }
            }
        }

Does this help?
0
 

Author Comment

by:lamb0
ID: 10936685
ok i know what was wrong now.
I didnt create the data source in control panel.

ive done this now and it works fine.
 If anyone else is having the same problems then follow these steps.....

Start>Settings>Control Panel>Administrative Tools>Data Sources (ODBC)>System DSN>Add>(scroll down to your db type)>Finish>Then click on the dsn you created and click configure. Now you can add a user name and password to access this database.

GOod Luck
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 11975836
Submitted to PAQ with points refunded (125)

DarthMod
Community Support Moderator
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question