【问题标题】:JSP-form data is not entering mysql databaseJSP-form数据没有进入mysql数据库
【发布时间】:2016-12-28 07:50:02
【问题描述】:

我制作了一个 HTML 文件来设计一个注册页面。单击提交按钮时,表单值被发送到 JSP 页面。但是当我运行这个项目时,填写表格后,显示空白的JSP页面,甚至没有显示“无法输入数据”。并且值没有进入数据库。我正在使用 Netbeans IDE。 这是两个文件的代码。

addlibform.html:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add Librarian</title>
    <link rel="stylesheet"      href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="background.css">
    <link rel="stylesheet" type="text/css" href="main.css">
    <style>
        .label{
            font-size: 15px;
            color: black;
            position: absolute;
            margin-left: 500px;
            margin-top: 100px;
        }
        .input{
            position: absolute;
            margin-left: 650px;
            margin-top: 100px;
        }
    </style>
</head>
<body>

    <h1 style="position: absolute; margin-left: 550px;">Add Librarian</h1><br>
    <form method="post" action="addlib.jsp">
        <label for="username" class="label">Username:</label>

        <input type="text" name="userid" id="userid" required class="input"><br><br><br>

    <label for="password" class="label">Password:</label>

    <input type="password" name="password" id="password" class="input"><br><br><br>

        <label for="address" class="label">Address:</label>

        <textarea rows="2" cols="22" name="add" class="input"></textarea><br><br><br>

        <label for="city" class="label">City:</label>

        <input type="text" name="city" class="input"><br><br><br>

        <label for="contact" class="label">Contact No.:</label>

        <input type="text" name="contact" class="input"><br><br><br>

        <input type="submit" class="btn btn-default" value="Add Librarian" style="position: relative; margin-left: 600px; margin-top: 100px;">
        <input class="btn btn-default" value="Back" style="position: relative; margin-left: 600px; margin-top: 20px;">        
    </form>

</body>
</html>

addlib.jsp 文件:

<%@ page import ="java.sql.*" %>
<%@ page import ="java.lang.*" %>
<%
String username = request.getParameter("userid");
String pwd = request.getParameter("password");
String add = request.getParameter("address");
String city = request.getParameter("city");
String contact = request.getParameter("contact");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymgmt", "root", "root");
Statement st = c.createStatement();

int i=st.executeUpdate("INSERT INTO librarians VALUES('" + username + "','" + pwd + "','" + add + "','" + city + "','" + contact + ")");
if(i > 0)
    System.out.println("Librarian added successfully");
else
    System.out.println("Failed to insert data");
}
catch(Exception e)
{
  System.out.println(e);
}
%>  

请帮忙。在此先感谢:)

【问题讨论】:

    标签: html mysql jsp jdbc netbeans


    【解决方案1】:

    以下内容:

    "','" + contact + ")");
    

    在关闭括号之前无法关闭单引号。

    那是你的错误。

    但这无关紧要。您在这里的问题不是您犯的特定错误,而是您在盲目编码,看不到错误的事实。如果你继续盲目地编码,就会出现更多的错误,你仍然不知道哪里出了问题。 stackoverflow 的我们随时为您提供帮助,但在某个时间点之后,它开始变得乏味,对您和我们来说都是如此。

    因此,在您的代码中,如果抛出异常(正如我上面解释的那样,它抛出),异常的toString() 将输出到标准输出。您在控制台中看到了吗?如果您无条件地将“hello”输出到标准输出(例如,就在try{, 之前),您看到了吗?如果没有,那就是问题所在:错误被有效地抑制了,所以你无法知道哪里出了问题。

    如何尝试捕获异常,以便服务器为您捕获异常并在服务器日志和服务器日志中提供有意义的错误消息在实际网页上?

    【讨论】:

      猜你喜欢
      • 2013-10-12
      • 1970-01-01
      • 2012-08-15
      • 2021-08-31
      • 2017-08-10
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多