【问题标题】:Exception in inserting a record into a table using SQL使用 SQL 向表中插入记录时出现异常
【发布时间】:2013-11-21 10:55:44
【问题描述】:
 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) { 

            }


    }
     }

我在“loans”表中保留了一个字段 loan_id 作为必须自动递增的主键。然后我编写了上面的查询,用于在表中插入一条新记录。我从另一个 JSP 获取这些值。但是我的表没有得到更新。请解决这个问题..

【问题讨论】:

  • IN 第二个 catch 块添加一个 exception.printStackTrace() 那么它将给出什么......写下你的问题或让它更清楚......
  • 它没有抛出任何异常

标签: java sqlexception jdbc-odbc


【解决方案1】:

用语句试试

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+")");

【讨论】:

  • 您能否发布它给出的异常,并检查所有数据库数据类型以及每列的条件,因为它非常简单。
  • IDE 没有抛出任何异常。
  • 连接语句后检查控件是否正常工作,因为你的代码中没有密码
【解决方案2】:

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

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+')");

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

+ "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");

【讨论】:

  • 你试过什么方法?准备好的语句还是把单引号改成双引号?
  • 你还在收到 SQLException 吗?您确定使用正确的setXxxx() 吗?即setString() 用于字符串,setInt() 用于整数?并确保您的 Connection 变量为 connection
  • 实际问题是......执行在此 SQL 查询处停止。上面的代码是完美的。剩下的一切都很好。它甚至没有显示任何异常,但它没有导航到下一个页面,即 UserHome.jsp。
  • 尝试更改为 if (i != 1) response.sendRedirect("UserHome.jsp");executeUpdate() 仅在有结果集时返回 1,否则返回 2。甚至 if(i == 2) 只是为了确保。
  • 另外,使用当前代码,它是否甚至打印System.out.println("loans table updated successfully");?> 如果没有,那是if 的问题。见上面的评论
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多