无法写入DB

时间:2015-12-20 15:02:25

标签: java mysql sql jsp jdbc

我编写了一个Java Web应用程序,我想将数据写入数据库,但是当我在文本字段中填写信息并单击提交时,我没有在DB中写入任何内容?什么是问题。

到目前为止,这是我的代码:

<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body onload="displayResult()">
        <%!
            public class User {
                String URL = "jdbc:mysql://localhost:3306/studetnInfo";
                String USERNAME = "root";
                String PASSWORD = "root";

                Connection myConnection = null;
                Statement statement=null;
                PreparedStatement insertUsers = null;
                ResultSet resultSet = null;

                public User(){

                    try{

                        myConnection = DriverManager.getConnection(URL, USERNAME, PASSWORD);             

                        insertUsers = myConnection.prepareStatement(
                        "INSERT INTO user(name, email) VALUES (?, ?, ?)");

                    } catch(SQLException e){
                        e.printStackTrace();
                    }
                }

                public int setUsers (String userName, String userEmail){

                    int result=0;
                    try{
                        insertUsers.setString(1, userName);
                        insertUsers.setString(2, userEmail);
                        result = insertUsers.executeUpdate();

                    }catch(SQLException e){
                        e.printStackTrace();
                    }
                    return result;
                }
            }
        %>
        <%
            int result=0;

            // If Submit buttin is clicked.
            if (request.getParameter("submit") != null){

                String firstName = new String();
                String emailAddress = new String();

                if (request.getParameter("first") != null){
                    firstName = request.getParameter("first");
                }
                if (request.getParameter("email") != null){
                     emailAddress = request.getParameter("email");
                }

                User user = new User();
                result = user.setUsers(firstName, emailAddress);
            }
        %>
        <form name="myform" action="index.jsp" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td>Enter First Name: </td>
                        <td><input type="text" name="first" value="" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Enter E-mail Address: </td>
                        <td><input type="text" name="email" value="" size="50" /></td>
                    </tr>
                </tbody>
            </table>
            <input type="hidden" name="hidden" value="<%= result %>" />
            <input type="reset" value="Clear" name="clear" />
            <input type="submit" value="Submit" name="submit" />
        </form>
            <script lang="JavaScript">
            <!--
                function displayResult()
                {
                    if(doccument.myform.hidden.value == 1){
                        alert("Data Inserted");
                    }else{
                        alret("Data Not Inserted");
                    }
                }
            //-->
            </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

因为您没有向INSERT脚本发送任何值。