用于将值插入注册表的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
但它没有将值插入注册表。我需要在注册表上插入值。
请帮帮我。
答案 0 :(得分:0)
我相信insert语句中的表名是错误的。
java.sql.PreparedStatement ps=conn.prepareStatement("insert into registraion values(?,?,?,?)");
将其更改为以下工作。:
java.sql.PreparedStatement ps=conn.prepareStatement("insert into registration values(?,?,?,?)");