Solved

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

Posted on 2004-04-17
7
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

630 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