[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-04-17
7
Medium Priority
?
285 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
6 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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