从JSP页面插入数据库失败

时间:2018-12-26 14:11:12

标签: java mysql jsp jdbc

我有一个表单,可以在其中输入产品的一些信息(productCode,名称,价格...),然后在JSP页面中获取此信息,然后尝试将其插入到我的数据库中。我还为该数据库创建了一个演示“ admin”,因此我(以root用户身份)授予了该数据库上的所有特权,因此当我与该用户建立连接时。问题是,当我提交信息时,连接已建立,但不会将其插入数据库,因此失败。上一次我进行与此类似的操作(并且始终是对我自己而不是商业目的),我将Java代码放入JSP页面,现在我有了一个单独的Java类来处理所有这些事情。 我处理插入的jsp页面是这样的:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="Data.Database" %>

<html>
<head>
    <title>Προϊόντα Τεχνολογίας | Εισαγωγή Προϊόντος</title>
</head>
<%
    float Price = 0;
    float Vat = 0;

    String code = request.getParameter("barcode");
    String name = request.getParameter("productname");
    String price = request.getParameter("price");
    String vat = request.getParameter("vat");
    String _description = request.getParameter("description");

    if (code == null || code.isEmpty() || price == null || price.isEmpty() || vat == null || vat.isEmpty() || _description == null || _description.isEmpty())
        response.sendRedirect("missingdata.html");

    try {
        if (!vat.matches("[0-9]+") || !price.matches("[0-9]+"))
            response.sendRedirect("errornumber.html");
        else {
            Price = Float.parseFloat(price);
            Vat = Float.parseFloat(vat);
        }
    }catch (NullPointerException ex) {
        ex.printStackTrace();
    }

    Database db = new Database();
    System.out.println("price = " + Price);
    System.out.println("vat = " + Vat);
    String q = "INSERT INTO products (prodcode, prodname, price, vat, `_description`) VALUES " +
            "('" + code + "', '" + name + "', '" + Price + "', '" + Vat + "', '" + _description + "');";
    db.createQuery(q);
    db.insertDataToDatabase();
    db.closeDatabase();
%>
<body>
A new record inserted in Products Table<br>
<a href="index.html">Επιστροφή</a>
</body>
</html>

处理与数据库连接的Java类是这样的:

包装数据;

import java.sql.*;
import java.lang.String;

public class Database {
    private String SQLstring;
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public Database() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String myDatabase = "jdbc:mysql://localhost:3306/dbproducts";
            String user = "prodadmin";
            String pass = "78910";
            this.con = DriverManager.getConnection(myDatabase, user, pass);
            this.st = con.createStatement();

        }catch (ClassNotFoundException cne) {
            cne.getMessage();
        }catch (SQLException se){
            se.getErrorCode();
        }

        System.out.println("Connection established");
    }

    public void closeDatabase() {
        try {
            this.st.close();
            this.con.close();
            this.rs.close();
        }catch (SQLException se) {
            se.getErrorCode();
        }
    }

    public void createQuery(String q) {
        this.SQLstring = q;
        System.out.println(this.SQLstring);
    }

    public ResultSet getDataFromDatabase() {

        System.out.println("Fetching data...");

        try {
            this.rs = this.st.executeQuery(this.SQLstring);
        }catch (SQLException se) {
            se.printStackTrace();
        }

        System.out.println("Data fetched!");

        return this.rs;
    }

    public void insertDataToDatabase() {
        try {
            this.st.executeUpdate(this.SQLstring);
        }catch (SQLException se) {
            se.printStackTrace();
        }

        System.out.println("Data saved to database!");
    }
}

服务器记录的错误如下:

org.apache.jasper.JasperException: An exception occurred processing [/insert.jsp] at line [45]

42:     String q = "INSERT INTO products (prodcode, prodname, price, vat, `_description`) VALUES " +
43:             "('" + code + "', '" + name + "', '" + Price + "', '" + Vat + "', '" + _description + "');";
44:     db.createQuery(q);
45:     db.insertDataToDatabase();
46:     db.closeDatabase();
47: %>
48: <body>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:607)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:496)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NullPointerException
    Data.Database.insertDataToDatabase(Database.java:62)
    org.apache.jsp.insert_jsp._jspService(insert_jsp.java:156)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:458)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

对我来说,这表明数据库Java类有问题,但是如果我在本地用主类插入某些内容,它将运行良好。

最后但并非最不重要的一点是,我尝试在java类中建立main,并且它插入了相同的语句,所以我猜这两者之间的通信有问题。我一直在努力寻找解决方案,但是我敢肯定这会很愚蠢。每一点帮助都值得赞赏。

圣诞节快乐,祝大家新年快乐!

0 个答案:

没有答案