使用NetBeans在MS Access数据库中不会发生插入删除更新

时间:2017-03-25 10:10:55

标签: java ms-access jdbc netbeans ucanaccess

在学习了一些Servlets之后,我尝试制作一个简单的学生信息系统。 index.html代码是

<html>
    <body bgcolor="AliceBlue">
        <h1 style="text-align: center">Student Information System</h1>
        <form method="get" action="MIS">
         First Name <input type="text" name="FName"/><br><br>
         Last Name <input type="text" name="LName"/><br><br>
         ID <input type="number" name="ID"/><br><br>
         Age <input type="number" name="Age"><br><br>
         <input type="submit" name="Insert" value="Insert">
         <input type="submit" name="Delete" value="Delete">
         <input type="submit" name="Update" value="Update">
        </form>
    </body>
</html>

在这里,我使用三个按钮插入,更新和删除来对数据库执行相应的操作。

这是MIS.java代码

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Gaurav
 */
@WebServlet(urlPatterns = {"/MIS"})
public class MIS extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MIS</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet MIS at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
       PrintWriter pr = response.getWriter();



       try
       {
           Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
           Connection con=DriverManager.getConnection("jdbc:ucanaccess://F://Subjects//WT Netbeans//WT P5//src//studentmis.accdb","","");
           String FirstName = request.getParameter("FName");
           String LastName = request.getParameter("LName");
           String Insert = request.getParameter("Insert");
           String Update = request.getParameter("Update");
           String Delete = request.getParameter("Delete");
           int ID = Integer.parseInt(request.getParameter("ID"));
           int Age = Integer.parseInt(request.getParameter("Age"));
           String SqlInsert="INSERT INTO student_info(Firstname,Lastname,ID,Age)VALUES(?,?,?,?)";
           String SqlUpdate="UPDATE student_info SET Lastname=? ID=? Age=? WHERE Firstname=?";
           String SqlDelete="DELETE FROM student_info WHERE Firstname=?";
           if(Insert!=null)
           {
               PreparedStatement InsertStatement = con.prepareStatement(SqlInsert);
               InsertStatement.setString(1,FirstName);
               InsertStatement.setString(2,LastName);
               InsertStatement.setInt(3,ID);
               InsertStatement.setInt(4,Age);
               int RowsInserted = InsertStatement.executeUpdate();
               if(RowsInserted > 0)
                pr.println("New User Added Successfully");

           }

           else if(Update!=null)
           {
               PreparedStatement UpdateStatement = con.prepareStatement(SqlUpdate);
               UpdateStatement.setString(1,FirstName);
               UpdateStatement.setString(2,LastName);
               UpdateStatement.setInt(3,ID);
               UpdateStatement.setInt(4,Age);
               int RowsUpdated = UpdateStatement.executeUpdate();
               if(RowsUpdated > 0)
                pr.println("User Updated Successfully");

           }

           else if(Delete!=null)
           {
               PreparedStatement DeleteStatement = con.prepareStatement(SqlDelete);
               DeleteStatement.setString(1,FirstName);
               int RowsDeleted = DeleteStatement.executeUpdate();
               if(RowsDeleted > 0)
                pr.println("User Updated Successfully");
           }

       } 


       catch (ClassNotFoundException | SQLException ex) 
       {
           pr.println(ex);
       }

        //processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

运行项目后, 如果我按下插入按钮(输入信息后),则会出现错误

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 user lacks privilege or object not found: FIRSTNAME

如果我按下删除按钮(输入信息后),则会显示错误

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 user lacks privilege or object not found: FIRSTNAME

如果按下更新按钮(输入信息后),则会显示错误

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 user lacks privilege or object not found: LASTNAME

这是我的MS Access数据库studentmis.accdb包含的内容:

Database description

请帮我找到此代码中的问题。如果是数据库身份验证问题,那么我保证我没有为此数据库创建用户名和密码。 此外,我保证我已导入所有必需的.jar文件以使用Ucanaccess驱动程序。

好了,现在解决了其他错误。现在,当我尝试单击更新按钮时,我收到此errornet.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 unexpected token: ID。已修改MIS.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Gaurav
 */
@WebServlet(urlPatterns = {"/MIS"})
public class MIS extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MIS</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet MIS at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
       PrintWriter pr = response.getWriter();



       try
       {
           Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
           Connection con=DriverManager.getConnection("jdbc:ucanaccess://F://Subjects//WT Netbeans//WT P5//src//studentmis.accdb");
           String FirstName = request.getParameter("FName");
           String LastName = request.getParameter("LName");
           String Insert = request.getParameter("Insert");
           String Update = request.getParameter("Update");
           String Delete = request.getParameter("Delete");
           int ID = Integer.parseInt(request.getParameter("ID"));
           int Age = Integer.parseInt(request.getParameter("Age"));
           String SqlInsert="INSERT INTO student_info(Firstname,Lastname,ID,Age)VALUES(?,?,?,?)";
           String SqlUpdate="UPDATE student_info SET Lastname=? ID=? Age=? WHERE Firstname=?";
           String SqlDelete="DELETE FROM student_info WHERE Firstname=?";
           if(Insert!=null)
           {
               PreparedStatement InsertStatement = con.prepareStatement(SqlInsert);
               InsertStatement.setString(1,FirstName);
               InsertStatement.setString(2,LastName);
               InsertStatement.setInt(3,ID);
               InsertStatement.setInt(4,Age);
               int RowsInserted = InsertStatement.executeUpdate();
               if(RowsInserted > 0)
                pr.println("New User Added Successfully");

           }

           else if(Update!=null)
           {
               PreparedStatement UpdateStatement = con.prepareStatement(SqlUpdate);
               UpdateStatement.setString(1, LastName);
               UpdateStatement.setInt(2,ID);
               UpdateStatement.setInt(3,Age);
               UpdateStatement.setString(4,FirstName);
               int RowsUpdated = UpdateStatement.executeUpdate();
               if(RowsUpdated > 0)
                pr.println("User Updated Successfully");

           }

           else if(Delete!=null)
           {
               PreparedStatement DeleteStatement = con.prepareStatement(SqlDelete);
               DeleteStatement.setString(1,FirstName);
               int RowsDeleted = DeleteStatement.executeUpdate();
               if(RowsDeleted > 0)
                pr.println("User Deleted Successfully");
           }

       } 


       catch (ClassNotFoundException | SQLException ex) 
       {
           pr.println(ex);
       }

        //processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

1 个答案:

答案 0 :(得分:1)

您收到此错误的原因是您的字段名称不正确,您的字段名称为:

|  Field1   |   Field1   |  Field3   |   Field4   |

而不是:

|  Firstname |   Lastname   |  ID   |   Age   |

实际上Firstname, Lastname, ID, Age是值,而不是列的名称,因此请更正字段的名称,并且每件事都可以。

error

修改

  

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 4.0.1意外   令牌:ID

这是逻辑,因为您将属性设置在错误的位置:

UPDATE student_info SET Lastname=? ID=? Age=? WHERE Firstname=?
//--------------------------1----^--2-^--3--^----------4------^

UpdateStatement.setString(1,FirstName);//<<-----this should be Lastname
UpdateStatement.setString(2,LastName);//<<-----this should be ID
UpdateStatement.setInt(3,ID);//<<-----this should be Age
UpdateStatement.setInt(4,Age);//<<-----this should be Firstname

所以改为使用:

UpdateStatement.setString(1, LastName);
UpdateStatement.setInt(2, ID);
UpdateStatement.setInt(3, Age);
UpdateStatement.setString(4, FirstName);

良好做法

请勿在开头UpdateStatement

中使用大写字母命名变量

修改

你错过了yor字段之间的逗号,UPDATE的正确语法是:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

所以在您的查询中:

UPDATE student_info SET Lastname=? ID=? Age=? WHERE Firstname=?
//--------------------------------^----^--miss comma

改为使用:

UPDATE student_info SET Lastname=?, ID=?, Age=? WHERE Firstname=?