0
私はJSPフォームからデータベース(XAMPP、MySQL)にレコードを登録(挿入)しようとしています。詳細は以下のとおりです。私は、ローカルホスト上で実行され、詳細を入力するときJSP(Eclipse)をデータベース(XAMPP、MySQL)に挿入する
register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "RegisterController" method = "post">
<input type = "text" name = "email" placeholder = "Your e-mail"><br><br>
<input type = "text" name = "firstname" placeholder = "First name"><br><br>
<input type = "text" name = "lastname" placeholder = "Last name"><br><br>
<input type = "text" name = "address" placeholder = "Home address"><br><br>
<input type = "text" name = "phone" placeholder = "Your phone number"><br><br>
<input type = "password" name = "password"><br><br>
<input type = "submit" name = "submit" value = "Register">
</form>
</body>
</html>
RegisterController.java
package cms.com;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
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 RegisterController
*/
public class RegisterController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterController() {
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
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
String email = request.getParameter("email");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
String password = request.getParameter("password");
String sql = "insert into users(email,firstname,lastname,address,phone,password) values(?,?,?,?,?,?)";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,email);
pst.setString(2,firstname);
pst.setString(3,lastname);
pst.setString(4,address);
pst.setString(5, phone);
pst.setString(6, password);
pst.executeUpdate();
PrintWriter out = response.getWriter();
out.println("You have successfully registered!");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
だから、それが唯一の空のページを表示し、テーブルには何も更新されていませんでした。デバッグでは、実行時と同様にエラーも表示されません。私はEclipse LunaをDynamic Module 2.5とApache Tomcat 7.0で使用しています。