Solved

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

Posted on 2004-04-17
7
272 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

734 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