关于插入数据库

时间:2012-09-17 08:44:19

标签: jsp jdbc

iam尝试从寄存器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>

}

1 个答案:

答案 0 :(得分:3)

你永远不会执行你的陈述:

ps.executeUpdate();

您不提交连接:

con.commit();

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

注意:我真的建议避免评论部分代码。依赖insert语句中列的默认顺序也是不好的做法。该陈述应该看起来像

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

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