在NetBeans中将HTML与MSAcess连接

时间:2015-10-14 08:25:06

标签: java html servlets jdbc netbeans

我有一个HTML页面,其中新用户已添加到数据库中,我已通过JDBC-ODBCServlets建立了MS-Access连接。我没有错误,但值不是&# 39;保存在数据库中。

******* 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 ****

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 ***

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>

}

1 个答案:

答案 0 :(得分:0)

问题是您从请求对象获取了错误的参数。

试试这个:

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