首页 > 解决方案 > 我无法将新值插入数据库

问题描述

我正在创建一个约会页面。我使用 java 和 MySQL 作为我的数据库。我在向数据库添加新约会时遇到了麻烦。我可以更新或删除我之前在数据库中添加的数据,但我在添加新约会时遇到了麻烦。

SEVERE: Servlet.service() for servlet [model.AppointmentsAPI] in context with path [/PracticalTest_Doctor_Management] threw exception
java.lang.NullPointerException
at model.AppointmentAPI.doPut(AppointmentAPI.java:73)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:663)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

这是我得到的错误“PracticalTest_Doctor_Management”是我的项目名称。第 73 行是 AppointmentAPI.java 中的 doPut 方法。下面我附上了代码。

对于数据库连接:

DB_Connection.java

package Util;
import java.sql.Connection;
import java.sql.DriverManager;

    public class DB_Connection {
        
        
        public Connection connect(){
            Connection con = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/healthmgt", "root", "");
                
                // For testing print success message
                            System.out.println("Successfully Connected");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return con;
    
                    }
            
    
    }

我正在使用的插入代码:

约会.java

public String insertAppointment(String date, String venue, String docName, String patientId) {
        String output = "";
        try {
            DB_Connection obj_DB_Connection= new DB_Connection();
            Connection con = obj_DB_Connection.connect();
            if (con == null) {
                return "Error while connecting to the database for inserting appoinment details.";
            }

            String query = " insert into appointment (`app_Id`,`app_Date`,`app_Venue`,`app_Doctor_Id`,`app_Patient_Id`) values (?,?,?,?,?)";
            PreparedStatement preparedStatement = con.prepareStatement(query);

            preparedStatement.setInt(1, 0);
            preparedStatement.setString(2, date);
            preparedStatement.setString(3, venue);
            preparedStatement.setString(4, docName);
            preparedStatement.setString(5, patientId);

            preparedStatement.execute();

            String newAppointment = readAppoinment();
            output = "{\"status\":\"success\", \"data\": \"" + newAppointment + "\"}";

        } catch (Exception e) {

            output = "{\"status\":\"error\", \"data\": \"Error while creating appointment.\"}";
            System.err.println(e.getMessage());
        }
        return output;
    }

约会API.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String output = appointmentObj.insertAppointment(request.getParameter("appointmentDate"),
            request.getParameter("appointmentVenue"), request.getParameter("assignDoctor"),
            request.getParameter("assignPatient"));

    response.getWriter().write(output);

}

protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Map paras = getParasMap(request);

        String output = appointmentObj.updateAppoinment(paras.get("hidapp_IdSave").toString(),
                paras.get("appointmentDate").toString(), 
                paras.get("appointmentVenue").toString(),
                paras.get("assignDoctor").toString(), 
                paras.get("assignPatient").toString());

        // There is a Error when updating an appointment. Use only date when updating
        // otherwise it prints the "Data truncation" error in
        // the console (yyyy/mm//dd) format

        response.getWriter().write(output);
    }

请查看我在下面附上的图片链接,我将其作为链接上传,因为我无法将其作为图片上传。下图显示了错误描述。

有人可以帮我弄这个吗 !!!

标签: javamysqlapiinsert

解决方案


doPut()如果您没有实现它会尝试转发doPost()或查看即将到来的请求,则会在方法中发生异常

  @Override
  protected void doPut(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
      doPost(req, resp);
  }

推荐阅读