简体   繁体   English

在NetBeans中将HTML与MSAcess连接

[英]Connecting HTML with MSAcess in NetBeans

I have a HTML page where new users are added to the Database,I have done MS-Access connection through JDBC-ODBC and Servlets .I have no errors but the values aren't getting saved in the Database. 我有一个HTML页面,新用户已添加到数据库中,我已经通过JDBC-ODBCServlets完成了MS-Access连接。我没有错误,但是值没有保存在数据库中。

*******HTML FILE***** ******* HTML文件*****

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <center><h1>login page</h1></center>
            <form action="Serve" method="get">
             Username: <input type="text" name="username"><br>
             Password: <input type="text" name="password"><br>
             <button> Login</button>
            </form>
        </div>
    </body>
</html>

***JDBC-ODBC FILE**** *** JDBC-ODBC文件****

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class base {
    Connection con;
    Statement st;
    ResultSet rs;
    String s;

    public base() throws SQLException {
        connect();
    }

    private void connect() throws SQLException {
        try {
            //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con =DriverManager.getConnection("jdbc:odbc:db","","");
            st=con.createStatement();
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
    }
}

*****SERVLETS*** ***** SERVLETS ***

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
@WebServlet(name = "Serve", urlPatterns = {"/Serve"})
public class Serve extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            base s = new base();
            s.s="insert into Table1 values("+request.getParameter("name")+","+request.getParameter("pass")+")";
             s.st.executeQuery(s.s);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }


    }

    // <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 {
        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>

}

The problem is that you get the wrong parameters from the request Object. 问题是您从请求对象中获取了错误的参数。

Try this: 尝试这个:

s.s="insert into Table1 values("+request.getParameter("username")+","+request.getParameter("password")+")";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM