用于将值插入注册表的 Java Servlet 代码:
package mypackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.PreparedStatement;
public class Register extends HttpServlet {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/test";
static final String USER = "root";
static final String PASS = "";
Connection conn = null;
Statement stmt = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("userName");
String pass=request.getParameter("userPass");
String email=request.getParameter("userEmail");
String ccountry=request.getParameter("userCountry");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt=conn.createStatement();
// String sql="insert into test values(?,?,?,?)";
java.sql.PreparedStatement ps=conn.prepareStatement("insert into registraion values(?,?,?,?)");
ps.setString(1,name);
ps.setString(2,pass);
ps.setString(3,email);
ps.setString(4,ccountry);
int i=ps.executeUpdate();
if(i>0)
out.print("You are successfully registered...");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
在我的表中有 5 个字段,如下所示。
id(primary key ,auto generate)
name
pass
email
country
但它没有将值插入注册表。我需要在注册表中插入值。
请帮我。