Java 使用NetBeans的MS Access数据库中未发生插入-删除更新
在学习了一些servlet之后,我尝试制作一个简单的学生信息系统。Java 使用NetBeans的MS Access数据库中未发生插入-删除更新,java,ms-access,jdbc,netbeans,ucanaccess,Java,Ms Access,Jdbc,Netbeans,Ucanaccess,在学习了一些servlet之后,我尝试制作一个简单的学生信息系统。 index.html代码是 <html> <body bgcolor="AliceBlue"> <h1 style="text-align: center">Student Information System</h1> <form method="get" action="MIS"> First Name &l
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>
}
项目运行后,,
如果我按下“插入”按钮(输入信息后),则会出现错误
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
如果我按下删除按钮(输入信息后),则会出现错误
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
如果我按下更新按钮(输入信息后),它会给出错误信息
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
包含的内容:
请帮助我找到此代码中的问题。如果是数据库身份验证问题,那么我保证我没有为此数据库创建用户名和密码。
另外,我保证已经导入了使用Ucanaccess驱动程序所需的所有.jar
文件
好了,现在其他错误都解决了。现在,当我尝试单击更新按钮时,我得到了这个errornet.ucanaccess.jdbc.UcanaccessSQLException:ucaesc:::4.0.1意外标记: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>
}
出现此错误是因为字段名称不正确,字段名称为:
| Field1 | Field1 | Field3 | Field4 |
而不是:
| Firstname | Lastname | ID | Age |
事实上,Firstname,Lastname,ID,Age
是值,而不是列的名称,所以更正字段的名称,一切都会好起来的
编辑 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
用于良好实践 不要在Begging
UpdateStatement
编辑
如果缺少yor字段之间的逗号,则更新的正确语法为:
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=?
谢谢。我真是太傻了。但现在有一个不同的错误。插入和删除正常,但更新无效。当我尝试按下更新按钮时,它给出了一个错误
net.ucanaccess.jdbc.UcanaccessSQLException:ucaesc:::4.0.1意外标记:ID
。请告诉我可能是什么问题@YCF\u Lcheck my edit@gauravgillalkar你混合了你的属性,因此你得到了这个错误很幸运你建议的编辑不起作用。仍然是完全相同的错误net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc:::4.0.1意外标记:ID
@YCF\u Lare您确定@gauravgillalkar可能您更改了if Insert块,如果不发布您更改的代码,请再次检查是的,我更改了正确的块。但我不能在这里发表评论的代码。它说评论太长了。我将改为修改问题@YCF\u L