Solved

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

Posted on 2004-04-17
7
264 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now