お世話になります。
jsp初心者です。
jspで会員登録フォームを入力し、仮にユーザーIDをaaaとし、そのデータをサーブレットで取得し、
データベースに登録するという流れにしたいのですが、
HTTPステータス 500 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'aaa' in 'field list'
というエラーが出てしまい、うまくいきません。
調べたところ、aaaというカラムは存在しませんという意味になるらしいんですが、
aaaというデータを登録したいだけなのになぜカラムの話が出てくるのだろうかと、うまく理解できていません。
回答よろしくお願いいたします。
java
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5<html> 6<head> 7<title>会員登録フォーム</title> 8</head> 9<body> 10 11<H1>会員登録フォーム</H1> 12<P>ユーザーID</P> 13<FORM ACTION="../InsertDb" method="get"> 14<INPUT type="text" name="user"> 15<P>パスワード</P> 16<INPUT type="password" name="pass"> 17<P>年齢</P> 18<INPUT type="text" name="age"> 19<INPUT type="reset"> 20<INPUT type="submit"> 21</FORM> 22</body> 23</html>
java
1package insert; 2 3import java.io.IOException; 4import java.sql.SQLException; 5import java.sql.Statement; 6 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import com.mysql.jdbc.Connection; 14 15import connect.MySql; 16 17 18@WebServlet("/InsertDb") 19public class InsertDb extends HttpServlet 20{ 21 private static final long serialVersionUID = 1L; 22 23 protected void doGet(HttpServletRequest request, 24 HttpServletResponse response) throws ServletException, IOException 25 { 26 String user = request.getParameter("user"); 27 String pass = request.getParameter("pass"); 28 String ages = request.getParameter("age"); 29 30 String sql = "INSERT INTO PERSONAL(username,password,age) VALUES(" + user + "," + pass + "," + ages + ")"; 31 32 Connection con = null; 33 Statement smt = null; 34 35 try 36 { 37 // MySQLに接続 38 con = MySql.getConnection(); 39 smt = (Statement) con.createStatement(); 40 int num = smt.executeUpdate(sql); 41 } 42 catch(SQLException e) 43 { 44 throw new ServletException(e); 45 } 46 finally 47 { 48 if(smt != null) 49 { 50 try 51 { 52 smt.close(); 53 } 54 catch(SQLException ignore) 55 {} 56 } 57 if(con != null) 58 { 59 try 60 { 61 con.close(); 62 } 63 catch(SQLException ignore) 64 {} 65 } 66 } 67 request.getRequestDispatcher("/db/test.jsp") 68 .forward(request, response); 69 70 } 71 72}
java
1package connect; 2 3import java.sql.DriverManager; 4 5import com.mysql.jdbc.Connection; 6 7public class MySql 8{ 9 public static Connection getConnection() 10 { 11 try 12 { 13 Class.forName("com.mysql.jdbc.Driver"); 14 Connection con = (Connection) DriverManager.getConnection( 15 "jdbc:mysql://localhost:3306/REGISTER", "root", ""); 16 return con; 17 } 18 catch(Exception e) 19 { 20 throw new IllegalStateException(e); 21 } 22 } 23 24 public static void main(String[] args) throws Exception 25 { 26 Connection con = getConnection(); 27 System.out.println("con="+con); 28 con.close(); 29 } 30} 31
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/15 09:10
2016/08/15 09:41