当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Exception in inserting a record into a table using SQL

问题描述:

 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) {

}

}

}

I have kept a field loan_id in the table 'loans' as a primary key which has to be automatically incremented. And then I wrote the above query for the insertion of a new record into the table. I am getting these values from another JSP .But my table is not getting updated. plz do solve this..

网友答案:

try with statement

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+")");
网友答案:

These aren't variables you're inserting, they're strings

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

Your values are all strings. You probably want the variable name. Your problem seems to be that you're using single quotes instead of double quotes, which separate String from variables. It should be something like this:

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

You should use a prepared statement instead though for added security

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");
分享给朋友:
您可能感兴趣的文章:
随机阅读: