我写了一个servlet文件 SignUp.java 和一个帮助文件 Updater.java 。 SignUp从用户填写的表单中收集用户信息。然后它将信息发送到Updater,以便Updater使用info更新数据库中的表。但是当我填写表单并单击“提交”时,没有任何变化。奇怪的是,当我编写测试文件并使用它为Updater提供一些虚拟值时,表格会使用这些虚拟值进行更新。 -
的 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只保存信息。
的 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);
}
}
Updater类文件不断抛出 - java.lang.ClassNotFoundException:com.mysql.jdbc.Driver。但是当我从终端运行测试类文件时,相同的Updater类运行顺利。<登记/>
我的 CLASSPATH 变量是否有问题:
它有这个价值 -
:在/ usr /共享/爪哇/的mysql.jar:/usr/share/java/mysql-connector-java-5.1.16.jar:/usr/lib/tomcat6/lib/servlet-api.jar:类<登记/>
请帮助。
答案 0 :(得分:0)
尝试将mysql-connector-java-5.1.16.jar复制到/ usr / lib / tomcat6 / lib /