从jsp页面插入表值

时间:2014-07-15 17:46:07

标签: mysql jsp insert

这是我的jsp页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "dbutil.*" %>
<%@ page import ="java.sql.*" %>


<!DOCTYPE html>
 <html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<form>
    <body>
        <table border="2" width="1" cellspacing="1" cellpadding="2">
            <thead>
                <tr>
                    <th>NEW USER  </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td> Email: </td>
                    <td><input type="email" name="t1" value=""  required/></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="t2" value="" required /></td>
                </tr>
                <tr>
                    <td> Name: </td>
                    <td><input type="text" name="t3" value="" required /></td>
                </tr>
                <tr>
                    <td>Mobile:</td>
                     <td><input type= "text" name="t4" value="" pattern ="\d{10}"    required /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit " name="b1" /></td>
                    <td></td>
                </tr>
            </tbody>
        </table>
        <%
            if (request.getParameter("b1") != null) {
                System.out.println("yo yo yo ");
                String email1 = request.getParameter("t1");
                String pass1 = request.getParameter("t2");
                String name1 = request.getParameter("t3");
                String mob1 = request.getParameter("t4");

                String sql = "insert into userinfo(email,pass,mobile,name) values('" +          email1
                        + "','" + pass1 + "','" + mob1 + "','" + name1 + "')";
                DBconnect x = new DBconnect();
                x.QueryExecuter(sql);

            }
        %>
    </body>
</form>


</html>

这不会在表名 userinfo 中插入值。 它是一个登录页面

当我运行文件时,它显示页面,在我们输入值后,它会将它插入到表中。 DBconnect是我为commen连接所做的课程。

package dbutil;

import java.sql.*;

public class DBconnect {


Connection  conn;


public DBconnect()
{
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn =     DriverManager.getConnection("jdbc:mysql://localhost:3306/employeemanagement","root","root")     ;

    }
    catch ( Exception e)
    {
     System.out.println(e);
    }
}
public void QueryExecuter (String sql){
try
{
    Statement stat;
        stat = conn.createStatement();
    stat.execute(sql);
    stat.close();
    conn.close();


 }

 catch(Exception e)
 {
     System.out.println(e);
 }
 }


public ResultSet QueryReturner(String sql)
{
    ResultSet rs = null;
    try
    {
        Statement stat = conn.createStatement();
        rs = stat.executeQuery(sql);


    }
    catch (Exception e)
    {
      System.out.println(e);
    }
    return rs;
  }
 }

2 个答案:

答案 0 :(得分:0)

更改

  

stat.execute(SQL);

  

stat.executeUpdate(SQL);

答案 1 :(得分:-1)

String sql = "insert into userinfo(email,pass,mobile,name) values('" + email1 + "','" + pass1 + "','" + mob1 + "','" + name1 + "');";

我认为你需要在SQL语句之后提供一个半冒号。它对我有用。