jsp、サーブレットを使い、フォームに入力した内容をデータベースに追加したい
test_db.jspからtest_db.javaの表示までは上手く行くのですが、それ以降が結果反映されません。Eclipse ver.2019-03(4.11.0)を使っています。
ご回答よろしくお願いします。
test_db.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ユーザー登録</title>
</head>
<body>
<form action="test_db" method="post">
名前:<br>
<input type="text" name="name"><br>
性別:<br>
男<input type="radio" name="gender" value="0">
女<input type="radio" name="gender" value="1">
<input type="submit" value="登録">
</form>
</body>
</html>
test_db.java
package servlet;
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 FormSampleServlet
*/
@WebServlet("/test_db")
public class test_db extends HttpServlet {
private static final long serialVersionUID = 1L;
//インスタンス化 値受け渡し
//ConnectionClass obj = new ConnectionClass();
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
static final String URL = "jdbc:mysql://localhost/data_data";
static final String USERNAME = "rararaio";
static final String PASSWORD = "Vwxyz123";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//リクエストパラメータを取得
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
//リクエストパラメータをチェック
String errorMsg = "";
if(name == null || name.length() == 0) {
errorMsg += "名前が入力されていません<br>";
}
if(gender == null || gender.length() == 0) {
errorMsg += "性別が選択されていません<br>";
} else {
if(gender.equals("0")) {
gender = "男性";
} else if(gender.equals("1")) {
gender = "女性";
}
}
//表示するメッセージ
String msg = "'" + name + "','" + gender + "'";
if(errorMsg.length() != 0) {
msg = errorMsg;
}
//HTMLを出力
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\">");
out.println("<title>ユーザー登録結果</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>"+msg+"</p>");
out.println("</body>");
out.println("</html>");
}
}
test_db_connection.java
package com.developersio;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class test_db_connection {
static final String URL = "jdbc:mysql://localhost/data_data";
static final String USERNAME = "rararaio";
static final String PASSWORD = "Vwxyz123";
public static void main(String[] args) {
try (
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement statement = connection.createStatement();
) {
String sql = "INSERT INTO test VALUES (msg);";
// String sql = "INSERT INTO test VALUES ('matuda','man');";
int result = statement.executeUpdate(sql);
System.out.println("結果1:" + result);
// sql = "INSERT INTO music (name, title, year) VALUES ('Aril');";
sql = "INSERT INTO test VALUES ('tanaka','woman');";
result = statement.executeUpdate(sql);
System.out.println("結果2:" + result);
} catch (SQLException e) {
e.printStackTrace();
}
}
}