简体   繁体   中英

Error While Inserting Data to Database from Java Program

I am trying to develop a inventory management system as part of my mini project.

While I try to Insert a data to my Bill_Master Database it returning an error

java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-01858: a non-numeric character was found where a numeric was expected

       bqty=Integer.parseInt(iqty.getText());
       bamount=Float.parseFloat(famnt.getText());
       bdsc=Integer.parseInt(dsc.getText());
        bnet=Float.parseFloat(netamnt.getText());
         billid=Integer.parseInt(billn.getText());
         code=Integer.parseInt(icode.getText());
         bqty=Integer.parseInt(iqty.getText());
         rate=getRate(code);
         iamount=rate*bqty;
         amt.setText(Float.toString(iamount)); 
         total=total+iamount;


       try 
       {
           billdetailid++;  
     stmt.executeUpdate("insert into Bill_Master values('"+billid+"','"+date+"','"+cname+"','"+total+"','"+bdsc+"','"+total+"','"+uid+"')");//Error Causing Line. 

Values are (1,'27-oct-2013','n/a',900.00,0.0,900.00,'Desk')

Table Structure

  1. Bill_Id (Primary Key INT ):-Stores Bill Number

  2. Bill_Date (Date): Stores Date Of Bill

  3. Customer_Name ( VARCHAR(50)): Customer Name

  4. Total_amt (NUMBER(6)) :Total Bill Amount

  5. Cash_Disc (Number(2)):Discount

  6. Grand_Total(Number(6)):Grand Total

  7. UID(VARCHAR(10)) Stores Who Generated the bill.(EMPLOYEE ID) Connection Type :ODBC

Please help to solve this issue.

You are putting single quotes around each of your values including bill_Id which is defined as an int. the SQL database is reading this as a string and complaining. Also (as was already pointed out) PreparedStatements make this a lot easier and more secure.

尝试这个:

stmt.executeUpdate("insert into Bill_Master values('"+billid+"',to_date('"+date+"', 'dd-MON-yyyy'),'"+cname+"','"+total+"','"+bdsc+"','"+total+"','"+uid+"')");

Firstly that is one horrible way of writing SQL queries in java !!!

I am guessing you have just started learning. Please check out PreparedStatements

Data Type related bugs will become easier to debug.

Also that is not the way you write continuous string appends. Check out StringBuilder and String Buffer

I found the exact problem. The reason was I am trying to insert the Label instead of the text in the label. correct statement is

stmt.executeUpdate("insert into Bill_Master values('"+billid+"','"+date.getText()+"','"+cname.getText()+"','"+total+"','"+bdsc+"','"+total+"','"+uid+"')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM