0

我编写了一个 servlet 文件SignUp.java和一个帮助文件Updater.java。SignUp 从用户填写的表单中收集用户信息。然后它将信息发送到 Updater,以便 Updater 使用该信息更新数据库中的表。但是当我填写表格并单击提交时,没有任何变化。奇怪的是,当我编写一个测试文件并用它为更新程序提供一些虚拟值时,表格被这些虚拟值更新了。-
注册.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 只是保存信息。

更新程序.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)
        {}
    }
}
}


测试.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);
}
}

更新程序类文件不断抛出 - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。但是当我从终端运行测试类文件时,相同的 Updater 类运行顺利。我的CLASSPATH变量

是否有问题: 它有这个值 - .:/usr/share/java/mysql.jar:/usr/share/java/mysql-connector-java-5.1.16.jar:/usr/lib /tomcat6/lib/servlet-api.jar:类。 请帮忙。

4

1 回答 1

0

尝试将 mysql-connector-java-5.1.16.jar 复制到 /usr/lib/tomcat6/lib/

于 2013-02-21T08:36:33.257 回答