Question

I wrote a servlet file SignUp.java and a helper file Updater.java. SignUp gathers the user info from a form filled by the user. Then it sends the info to Updater so that Updater updates the table in the database using the info. But when I fill the form and click submit, nothing changes. The weird thing is that when I wrote a test file and used it to supply the Updater with some dummy values, the table was updated with those dummy values. -
SignUp.java

package com.onlineshopping.web;

import com.onlineshopping.model.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SignUp extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException
{
    String[] month = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

    InfoHolder userInfo = new InfoHolder();
    userInfo.name = req.getParameter("name");
    userInfo.email = req.getParameter("email");
    userInfo.date = Integer.parseInt(req.getParameter("date"));
    userInfo.month = Arrays.asList(month).indexOf(req.getParameter("month")) + 1;
    userInfo.year = Integer.parseInt(req.getParameter("year"));
    userInfo.pwd = req.getParameter("pwd");
    userInfo.subscribe = "n";
    try
    {
        userInfo.subscribe = req.getParameter("subscribe");
    }
    catch(Exception e)
    {}

    Updater u = new Updater();
    u.update(userInfo);
}
}

InfoHolder just holds the information.

Updater.java

package com.onlineshopping.model;

import java.io.*;
import java.sql.*;
import java.util.*;
public class Updater
{
private static final String DRIVER = "com.mysql.jdbc.Driver";

private static final String CONNECTION = "jdbc:mysql://localhost/onlineshopping";

public void update(InfoHolder userInfo)
{

    try
    {
        Class.forName(DRIVER);

        Properties p = new Properties();
        p.put("user","****");
        p.put("password","****");

        Connection c = DriverManager.getConnection(CONNECTION,p);

        Statement s = c.createStatement();

        ResultSet rs = s.executeQuery("SELECT MAX(userid)+1 FROM user_info");
        rs.next();
        int id = rs.getInt(1);

        PreparedStatement ps = c.prepareStatement("INSERT INTO user_info VALUES (?,?,?,?,?,?,?)");
        ps.setInt(1,id);
        ps.setString(2,userInfo.name);
        ps.setString(3,userInfo.email);
        ps.setInt(4,userInfo.date);
        ps.setInt(5,userInfo.month);
        ps.setInt(6,userInfo.year);
        ps.setString(7,userInfo.subscribe);
        ps.executeUpdate();

        c.close();
    }

    catch(Exception e)
    {
        try
        {
            PrintWriter out = new PrintWriter(new FileWriter("err", true));
            out.println("Error: "+ e);
            out.close();
        }
        catch(IOException e)
        {}
    }
}
}


test.java

import com.onlineshopping.model.*;

public class test
{

public static void main(String[] args)
{
    Updater u = new Updater();

    InfoHolder xyz = new InfoHolder();
    xyz.name = "check";
    xyz.email = "checkmail";
    xyz.date = 1;
    xyz.month = 1;
    xyz.year = 2;
    xyz.pwd = "pw";
    xyz.subscribe = "n";

    u.update(xyz);
}
}

The Updater class file keeps throwing - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. But when I run the test class file from the terminal the same Updater class runs smoothly.

Is there a problem with my CLASSPATH variable:
It has this value - .:/usr/share/java/mysql.jar:/usr/share/java/mysql-connector-java-5.1.16.jar:/usr/lib/tomcat6/lib/servlet-api.jar:classes.
Please Help.

Was it helpful?

Solution

Try to copy mysql-connector-java-5.1.16.jar into /usr/lib/tomcat6/lib/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top