我试图捕获从表单输入的数据。一旦我点击提交按钮,但我遇到了困难。下面是我的简单代码。我试图在注册页面上工作,该页面捕获有关某人的所有信息。一些数据有一个下拉菜单和单选按钮。我想捕获并存储数据库中的所有内容(mysql)。
我的servlet代码:
package com.Registration.Controllers;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddUserSevelet
*/
@WebServlet("/AddUserSevelet")
public class AddUserSevelet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddUserSevelet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter write = response.getWriter();
String username = request.getParameter("uname");
String password = request.getParameter("pass");
String country = request.getParameter("country");
AddUser register =new AddUser();
if (register.adduser(username, password, country)){
write.println("You Have Been Registered Successfully");
} else {
write.print("ERROR WHILE LOADING");
}
}
}
my adduser class
package com.Registration.Controllers;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class AddUser {
public boolean adduser(String userName,String password,String country){
boolean isRegistered = false;
try{
Connection con = DB_Connection.getConnection();
PreparedStatement psmt = con.prepareStatement("insert into user VALUES(default,?,?,?,?)");
psmt.setString(1, userName);
psmt.setString(2, password);
psmt.setString(3, country);
int success = psmt.executeUpdate();
if(success > 0){
isRegistered = true;
}
}
catch(Exception e){
e.printStackTrace();
}
return isRegistered;
}
}
我的HTML代码:
<body>
<form action="AddUserSevelet" method="post">
UserName: <input type="text" name="uname"/><br/><hr/>
password: <input type="password" name="pass"/><br/><hr/>
Country: <input type="text" name="country"/><br/><hr/>
<input type="radio" name="color" value="red" checked>red
<br>
<input type="radio" name="color" value="blue" checked>blue
<br>
<input type="radio" name="color" value="white">white
<br/><hr/>
<br/><hr/>
<select name ="cars">
<option value="BMW"> BMW 320i</option>
<option value="BENZ"> Compressor</option>
<option value="Toyota"> Allion</option>
<option value="Audi"> A5</option>
<option value="Jeep"> Cheroki</option>
<option value="Volvo"> Volvo g5</option>
</select>
<input type="submit" value="Register"/><br/><hr/>
</body>
我的问题是如何从下拉菜单和单选按钮捕获数据,如果我将更改我的servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter write = response.getWriter();
String username = request.getParameter("uname");
String password = request.getParameter("pass");
String country = request.getParameter("country");
String cars = request.getParameter("cars");
String color = request.getParameter("color");
AddUser register =new AddUser();
if (register.adduser(username, password, country, cars , color)){
write.println("You Have Been Registered Successfully");
} else {
write.print("ERROR WHILE LOADING");
}
我的班级代码:
Connection con = DB_Connection.getConnection();
PreparedStatement psmt = con.prepareStatement("insert into 'user'(?,?,?,?,?) VALUES(default,?,?,?,?,?,?)");
psmt.setString(1, userName);
psmt.setString(2, password);
psmt.setString(3, country);
psmt.setString(4, cars);
psmt.setString(5, color);
我得到错误:
加载时出现错误,并且在控制台中出现
java.sql.SQLEXCEPTION:No VALUE specified for parameter 6
请帮助我解决如何使用servlet捕获数据并插入或编辑数据的方法或解决方案。我是一个刚尝试学习的新手。谢谢!