-1

我正在尝试从注册 jsp 页面的数据库 swingmail 中插入 Dealer_Register 表,但是这些值没有被插入,你能告诉我的程序中是否有任何异常出现,请告诉原因

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testing;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Admin
 */

public class regdbservlet extends HttpServlet {
 /*  private static String algorithm = "DESede";
        private static Key key = null;
        private static Cipher cipher = null;

 private static byte[] encrypt(String input)throws Exception {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] inputBytes = input.getBytes();
            return cipher.doFinal(inputBytes);
        }
*/
    /** 
     * 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 res)
    throws ServletException, IOException {
       /* key = KeyGenerator.getInstance(algorithm).generateKey();
        cipher = Cipher.getInstance(algorithm);

        response.setContentType("text/html;charset=UTF-8");*/
        PrintWriter out = res.getWriter();
        String firstname=request.getParameter("firstname");
        String surname=request.getParameter("surname");
        String email=request.getParameter("email");
        String zipcode=request.getParameter("zipcode");
        String userId=request.getParameter("userId");
        String dealer_password=request.getParameter("dealer_password");
        String town=request.getParameter("town");
        String country=request.getParameter("country");
        //String input = dealer_password;
         //byte[] encryptionBytes = encrypt(input);
        //String passw=new String(encryptionBytes);
        String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=swingmail";
        String user = "sa";
        String pwd = "sa";

        Connection con=null;
       // Statement st=null;
        //ResultSet rs=null;
        try {
              Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
              con =  DriverManager.getConnection(url,user,pwd);
              //String sql = "INSERT INTO memory VALUES ('" + surname + "','" + firstname + "'," + userId + ","+ zipcode +",'"+ email +"','"+ dealer_password +"','"+ town +"','"+ country +"')" ;
              String sql = "insert into  Dealer_Register values (?,?,?,?,?,?,?,?)";
              PreparedStatement ps =con.prepareStatement(sql);
              ps.setString(1, surname);
              ps.setString(2, firstname);
              ps.setString(3, userId);
              ps.setString(4, zipcode);
              ps.setString(5, email);
              ps.setString(6, dealer_password);
              ps.setString(7, town);
              ps.setString(8, country);
              out.println("<html>");
              out.println("<head>");
              out.println("<title>Servlet regdbservlet</title>");
              out.println("</head>");
              out.println("<body>");
              out.println("<h1>Servlet regdbservlet at " + request.getContextPath () + "</h1>");
              out.println("</body>");
              out.println("</html>");

        }
         catch(Exception  cnfe){
            cnfe.printStackTrace();
        }
        finally {
            out.close();
        }
    }
        // <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>

}
4

1 回答 1

3

你永远不会执行你的语句:

ps.executeUpdate();

您也不提交连接:

con.commit();

并且连接真的应该在 finally 块中关闭。您应该始终关闭您打开的连接。

注意:我真的建议避免注释代码部分。在插入语句中依赖列的默认顺序也是不好的做法。该声明应如下所示

insert into Dealer_Register (id, foo, bar, baz, ...) values (?,?,?,?,?,?,?,?)

最后,请尊重 Java 命名约定。Java 中的类以大写字母开头。

于 2012-09-17T08:46:45.360 回答