如何使用JDBC PreparedStatement将行插入MS Access?

时间:2014-09-07 09:32:02

标签: jdbc odbc ms-access-2013

未在Access DB中插入值。我的DSN名称是'DB',表名是'Employee'。 Access DB正在连接(由修改时间指示),但没有成功的插入操作。 NetBeans提供错误:“数据库位置未正确设置”。我在数据源(ODBC)中使用我的数据库设置了DSN。

try (Connection c = DriverManager.getConnection("jdbc:odbc:DB")) {
    PreparedStatement pss = c.prepareStatement("INSERT INTO Employee VALUES (?,?,?,?)");
    pss.setString(1, fn);
    pss.setString(2, ln);
    pss.setString(3, mn);
    pss.setString(4, add);
    pss.executeUpdate();
    c.commit();
}
} catch (ClassNotFoundException | SQLException e) {
    System.out.println(e);
}

这是完整的代码:

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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author javaEntu
 */
public class DB extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @
    Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String fn = request.getParameter("fn");
            String ln = request.getParameter("ln");
            String mn = request.getParameter("mn");
            String ps = request.getParameter("ps");
            String add = request.getParameter("add");
            String gen = request.getParameter("gen");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SV</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SV at " + request.getContextPath() + request.getRequestURL()     + "</h1>");
            out.println("</body>");
            out.println("</html>");
            out.println("<B>Your details: </B>");
            out.println("<br>First name: " + fn);
            out.println("<br>Last name: " + ln);
            out.println("<br>Contact number: " + mn);
            out.println("<br>Password: " + ps);
            out.println("<br>Address: " + add);
            out.println("<br>Gender: " + gen);
            out.println("<br>Thank you!");

            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                try (Connection c = DriverManager.getConnection("jdbc:odbc:DB")) {

                    PreparedStatement pss = c.prepareStatement("INSERT INTO Employee VALUES     (?,?,?,?)");
                    pss.setString(1, fn);
                    pss.setString(2, ln);
                    pss.setString(3, mn);
                    pss.setString(4, add);
                    pss.executeUpdate();
                    c.commit();
                }
            } catch (ClassNotFoundException | SQLException e) {
                System.out.println(e);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您应该在c.close()之后添加c.commit()

 try (Connection c = DriverManager.getConnection("jdbc:odbc:DB")) {
     PreparedStatement pss = c.prepareStatement("INSERT INTO Employee VALUES     (?,?,?,?)");
     pss.setString(1, fn);
     pss.setString(2, ln);
     pss.setString(3, mn);
     pss.setString(4, add);
     pss.executeUpdate();
     c.commit();
     c.close();
 }

答案 1 :(得分:0)

您的代码看起来不错。您可以添加日志语句以显示连接的外观(它看起来像sun.jdbc.odbc.JdbcOdbcConnection@15be5b6)。另外executeUpdate()返回插入的行数。在我的northwind MS Access数据库中,它可以工作,并为类似的INSERT返回1。将它记录在某处。

我认为你的问题在于数据库连接。查找任何ODBC编辑器并检查您的DSN是否有效。您还可以将连接字符串更改为:jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\Nwind.mdb

可能是DSN配置为32位而Java是64位还是反之?

另请参阅:the java db location is not set correctly

上的类似Netbeans错误消息