使用SQL将记录插入表中的异常

时间:2013-11-21 10:55:44

标签: java sqlexception jdbc-odbc

 public class grantLoan extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Connection con;
     Statement st;

    public grantLoan() {
        super();

    }

    public Connection getCon()
    {
          try
          {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/microfinance", "root", "");
          }
          catch (Exception e) {

            e.printStackTrace();
        }


          return con;

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         response.setContentType("text/html");
            try
            {

                String category = request.getParameter("category");
                System.out.println(category);
                String addr = request.getParameter("addres");
                System.out.println(addr);
                Integer income = request.getIntHeader("sal");
                System.out.println(income);
                Integer amount = request.getIntHeader("amount");
                System.out.println(amount);
                String tenure = request.getParameter("tenure");
                System.out.println(tenure);
                String assets = request.getParameter("surity");
                System.out.println(assets);
                String type_of_payment = request.getParameter("paymentType");
                System.out.println(type_of_payment);

                HttpSession session = request.getSession();
                int accno = (Integer)session.getAttribute("acno");


               con=getCon();

                st =  con.createStatement();

              PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into loans(Account_no,category,Present_address,Required_Amount,Duration,Assets,income,Payment_type) values('?','?','?','?','?','?','?','?')");
              pst.setInt(2, accno);
              pst.setString(3, category);
              pst.setString(4, addr);
              pst.setInt(5, amount);
              pst.setString(6, tenure);
              pst.setString(7, assets);
              pst.setInt(8, income);
              pst.setString(9, type_of_payment);

             int i= pst.executeUpdate(); 
              if(i==1)
              {
                System.out.println("loans table updated successfully");
                response.sendRedirect("UserHome.jsp");

               }
            }
            catch(Exception exception) { 

            }


    }
     }

我在表'贷款'中保留了一个字段loan_id作为主键,必须自动递增。然后我写了上面的查询,以便在表中插入新记录。我从另一个JSP获取这些值。但是我的表没有得到更新。 PLZ确实解决了这个问题。

2 个答案:

答案 0 :(得分:0)

这些不是您要插入的变量,而是字符串

int i= st.executeUpdate("insert into loans(Account_no,category,Present_address,Required_Amount,Duration,Assets,income,Payment_type) values('+accno+','+category+','+addr+','+amount+','+tenure+','+assets+','+income+','+type_of_payment+')");

你的价值观都是字符串。您可能想要变量名称。您的问题似乎是您使用单引号而不是双引号,它将String与变量分开。它应该是这样的:

+ "values(" + accno + ", " + category + ", " + addr + ", "...

为了增加安全性,您应该使用预准备语句

PreparedStatment pst = connection.prepareStatement("insert into loans(Account_no,category,Present_address,Required_Amount,Duration,Assets,income,Payment_type) values(?, ?, ?, ?, ?, ?, ?, ?)");

pst.setInt(1, accno);
pst.setString(2, category);    // should be setXxxx depending on the type of data
pst.setString(3, address);
pst.setString(4, amount);
pst.setString(5, tenure);
pst.setString(6, assets);
pst.setInt(7, income);
pst.setString(8, type_of_payment);

int i = pst.executeUpdate();

if (i == 1) response.sendRedirect("UserHome.jsp");

- OR -

boolean updated = pst.execute();

if (updated) response.sendRedirect("UserHome.jsp");

答案 1 :(得分:0)

尝试使用声明

int i= st.executeUpdate("insert into loans(Account_no,category,Present_address,Required_Amount,Duration,Assets,income,Payment_type) values("+accno+","+category+","+addr+","+amount+","+tenure+","+assets+","+income+","+type_of_payment+")");