使用JSP和MySQL执行查询时会自动插入空值 [英] Null values automatically inserted when executing a query using JSP and MySQL

查看:114
本文介绍了使用JSP和MySQL执行查询时会自动插入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行下面的代码时,在每次插入之前,自动用空值填充一行,然后插入实际值.

When I execute the code below, before every insert, automatically a row is getting filled with Null values and then the actual values are being inserted.

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="style.css" type="text/css">
    <title>Connecting to a Data base</title>
</head>
<body>

     <%
     String connectionURL="jdbc:mysql://localhost:3306/jsp_test";
     Connection connection = null;
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     connection = DriverManager.getConnection(connectionURL, "root", "exeterblr");
     if(!connection.isClosed()) {
         out.println("Successfully connected to " + "MySQL server using TCP/IP...");
         }
     else
         {
         out.println("Cannot connect to DB");
         }
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        PreparedStatement pstatement=null;
        int updateTable=0;
        String queryString = "INSERT INTO sample_info(name,email,phone) VALUES (?, ?, ?)";
        pstatement=connection.prepareStatement(queryString);
        pstatement.setString(1, name);
        pstatement.setString(2, email);
        pstatement.setString(3, phone);

        updateTable=pstatement.executeUpdate();
      %>



    <form method="post" action="index.jsp">
        <table>
            <tr>
            <td>Name</td><td><input type="text" name="name"></td>
            </tr>
            <tr>
            <td>Email</td><td><input type="text" name="email"></td>
            </tr>
            <tr>
            <td>Phone</td><td> <input type="text" name="phone"></td>
            </tr>

        </table>
        <input type="submit" value="Submit">
    </form>


</body>

当我插入一个值时,数据库包含以下内容:

When I insert a value, the database contains the following:

Name Email Phone <br />
NULL NULL  NULL  <br />
ActualValue ActualValue Actual Value

推荐答案

您每次访问网页时都会插入(NULL,NULL,NULL),而不仅是在提交时.

You insert (NULL,NULL,NULL) each time you visit your webpage and not only on submit.

这篇关于使用JSP和MySQL执行查询时会自动插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆