| | |
java app with INSERT statement...
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi,
I am currently doing a mini project using Eclipse with Java language.. i needed to submit some values into the DB, but i am having some trouble to do so... i would like to find out if i need to create variables for all the values i want to submit? and please do help me with the code below. btw, i am using date/time datatype for the date and time and autonum for invoice num... THanks
I am currently doing a mini project using Eclipse with Java language.. i needed to submit some values into the DB, but i am having some trouble to do so... i would like to find out if i need to create variables for all the values i want to submit? and please do help me with the code below. btw, i am using date/time datatype for the date and time and autonum for invoice num... THanks
Java Syntax (Toggle Plain Text)
public class Payment { private int InvoiceNo; private String Date; private String Time; private int TableNo; private String PayMode; private String ProductName; private int Qty; private String Size; private double UnitPrice; private double TotalPrice; private double SubTotal; private float GST; private float SurCharge; private float MemDisc; private float DelCharge; private double GrandTotal; private double Tendered; private double Changed; private String Name; private int CreditCardNo; private String CreditCardType; private String ExpDate; private int CVC; public String getName() { return Name; } public void setName(String name) { this.Name = name; } public int getCreditNo() { return CreditCardNo; } public void setCreditNo(int creditNo) { this.CreditCardNo = creditNo; } public String getExpiryDate() { return ExpDate; } public void setExpiryDate(String expiryDate) { ExpDate = expiryDate; } public String getCreditType() { return CreditCardType; } public void setCreditType(String creditType) { this.CreditCardType = creditType; } public int getCVC() { return CVC; } public void setCVC(int cvc) { CVC = cvc; } public float getGST() { return GST; } public void setGST(float gst) { GST = gst; } public Payment() { // TODO Auto-generated constructor stub } public boolean retrieveProduct(){ // declare local variables boolean success = false; ResultSet rs = null; DBController db = new DBController(); // step 1 of using DBcontroller, passing data source name setup during last practical db.setUp("SweetParadise"); // declare the SQL String dbQuery = "SELECT * FROM PAYMENT WHERE InvoiceNo =" + InvoiceNo; // step 2 of using DBcontroller, for retrieve SQL use readRequest method rs = db.readRequest(dbQuery); try{ if (rs.next()){ InvoiceNo = rs.getInt("InvoiceNo"); /*name = rs.getString("ProductName"); image = rs.getString("ProductImage"); unitPrice = rs.getDouble("ProductUnitPrice"); */ success = true; } } catch (Exception e) { e.printStackTrace(); } // step 3 of using DBcontroller db.terminate(); return success; } public boolean createPayment(){ // declare local variables boolean success = false; ResultSet rs = null; DBController db = new DBController(); // step 1 of using DBcontroller, passing data source name setup during last practical db.setUp("SweetParadise"); // declare the SQL String dbQuery = "INSERT INTO PAYMENT(InvoiceNo, Date, Time, TableNo, PayMode, "; dbQuery = dbQuery + "ProductName, Qty, Size, UnitPrice, TotalPrice, "; dbQuery = dbQuery + "SubTotal, GST, SurCharge, MemDisc, DelCharge, GrandTotal, "; dbQuery = dbQuery + "Tendered, Changed, Name, CreditCardNo, CreditCardType, ExpDate, CVC) "; dbQuery = dbQuery + "VALUES (" + InvoiceNo + ",'" + Date + "', " + Time + ", "; dbQuery = dbQuery + "'" + TableNo + "', '" + PayMode + "' , '" + ProductName + "', '" + Qty + "',"; dbQuery = dbQuery + "'" + Size + "', '" + UnitPrice + "', '" + TotalPrice + "', "; dbQuery = dbQuery + "'" + SubTotal + "', '" + GST + "', '" + SurCharge + "', '" + MemDisc + "', " ; dbQuery = dbQuery + "'" + DelCharge + "', '" + GrandTotal + "', '" + Tendered + "', '" + Changed + "', '" + Name + "',"; dbQuery = dbQuery + "'" + CreditCardNo + "', '" + CreditCardType + "', '" + ExpDate + "', '" + CVC + "')"; // step 2 of using DBcontroller, use updateRequestKey method if you want the value of // the key back (usually, if the key is autoNumber) rs = db.updateRequestKey(dbQuery); try{ if (rs.next()){ // this will return the ID which is a autoNumber primary key InvoiceNo = rs.getInt("InvoiceNo"); success = true; } } catch (Exception e) { e.printStackTrace(); } // step 3 of using DBcontroller db.terminate(); return success; } /*public static ArrayList<Product> getProductByCategory(int inCatID){ // declare local variables ArrayList<Product> pdtList = new ArrayList<Product>(); ResultSet rs = null; DBController db = new DBController(); // step 1 of using DBcontroller, passing data source name setup during last practical db.setUp("myDatabase"); String dbQuery = "SELECT * FROM PRODUCTS WHERE ProductCategoryID = " + inCatID; // step 2 of using DBcontroller, use updateRequest method rs = db.readRequest(dbQuery); try{ while (rs.next()){ int pdtID = rs.getInt("ProductID"); int pdtCatID = rs.getInt("ProductCategoryID"); String pdtName = rs.getString("ProductName"); String pdtImage = rs.getString("ProductImage"); double pdtUnitPrice = rs.getDouble("ProductUnitPrice"); Product pdt = new Product(pdtID, pdtCatID, pdtName, pdtImage, pdtUnitPrice); pdtList.add(pdt); } } catch (Exception e) { e.printStackTrace(); } // step 3 of using DBcontroller db.terminate(); return pdtList; } */ public static void main(String[] args) { Payment p1 = new Payment(); p1.setName("test"); p1.setCreditNo(3567843); System.out.println(p1.createPayment()); } }
•
•
•
•
Hi,
I am currently doing a mini project using Eclipse with Java language.. i needed to submit some values into the DB, but i am having some trouble to do so... i would like to find out if i need to create variables for all the values i want to submit? and please do help me with the code below. btw, i am using date/time datatype for the date and time and autonum for invoice num... THanks
Well if you want to dynamically add data into DB then you have to use variables. Its good programming practice to use variable ( ofcourse for essential ones only ).
Please mention the problem you are facing while inserting data to DB.
Thanks.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
1. I don't see anything wrong with the query code here.
2. If you want to send the data for just two variables mention just those variables in the insert into clause for e.g. if you want to insert just date and time write the query as follows : INSERT INTO tablename(date,time) values ('%datevalue%','%timevalue%'); this will work just as fine.
3. Instead of having two independent columns, one for date and the other for time you caould have a single datetime or timestamp column. Look into the specs of each to find out which one is more appropriate in your case.
4. Since InvoiceNum is an auto generated column, you need not pass data for it (I assume it is something like AUTO_INCREMENT in MySQL) you can omit this coulmn name and it's corresponding value from the query.
Alsoyou have not stated what error as such you are getting, mention that to get more specific help.
2. If you want to send the data for just two variables mention just those variables in the insert into clause for e.g. if you want to insert just date and time write the query as follows : INSERT INTO tablename(date,time) values ('%datevalue%','%timevalue%'); this will work just as fine.
3. Instead of having two independent columns, one for date and the other for time you caould have a single datetime or timestamp column. Look into the specs of each to find out which one is more appropriate in your case.
4. Since InvoiceNum is an auto generated column, you need not pass data for it (I assume it is something like AUTO_INCREMENT in MySQL) you can omit this coulmn name and it's corresponding value from the query.
Alsoyou have not stated what error as such you are getting, mention that to get more specific help.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Oh, he's talking about variables in SQL Query.
Table structure :
-----------------------
Name : testTable
-----------------------
userID - int
username - varchar(20)
password - varchar(20)
-----------------------
Queries:
If you know the table attributes datatype and series.
If you want to left any of attribute null (only if it can hold null value).
if you only know the attributes name but dont know the series.
Regards!
Table structure :
-----------------------
Name : testTable
-----------------------
userID - int
username - varchar(20)
password - varchar(20)
-----------------------
Queries:
If you know the table attributes datatype and series.
Java Syntax (Toggle Plain Text)
insert into testTable values (1,'user','pass');
Java Syntax (Toggle Plain Text)
insert into testTable (username, password ) values ('user','pass');
Java Syntax (Toggle Plain Text)
insert into testTable (username, userID, password ) values ('user',1,'pass');
Regards!
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi, Thanks for all the replies.. I was facing some syntax issue earlier, but i tried retyping the Query over again and it works well now.. However, i still have another issue right now... If i have a form which i need the user to fill up and submit to the DB, should each textbox have a variable along with it as well? and also, i have some issue creating a button group for the Radiobutton option, could someone provide a simple code for a couple of radiobutton which are grouped.. Thanks... Appreciate all the help!!!
java Syntax (Toggle Plain Text)
private JRadioButton male; private JRadioButton female; private ButtonGroup gender; // create the radiobuttons male = new JRadioButton(); female = new JRadioButton(); gender = new ButtonGroup(); // set the necessary parameters for the radio buttons male.setText("M"); male.setSelected(true); female.setText("F"); female.setSelected(false); /* *can be used to catch the button on which the event is generated * (by checking the action commad) */ male.setActionCommand("Male"); female.setActionCommand("Female"); // finally add the two buttons to the 'gender group' gender.add(male); gender.add(female);
Last edited by verruckt24; Dec 31st, 2008 at 2:48 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi Verruckt24, Thanks for being really helpful, i have tried the above coding, and it works well, however, if i would like to input the radiobutton into a dialog box, is there anything which i might need to tweak a little? Thanks once again. And for my insert query, i got a error saying "Number of query values and destination fields are not the same.". What does this mean?
Use JFrame to build what ever you need
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
And for my insert query, i got a error saying "Number of query values and destination fields are not the same.". What does this mean?
Pay more attention to puneetkay's post.
Also try posting the query you are trying to run
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Dec 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi,
I have progressively overcome the above stages... However, i have met another issue regarding the Submission button, i would like to execute the above coding thru clicking of the button, may i find out how this can be done? Apologies as i am still very new to all this programming stuffs.. Hope you guys could help me out.. Thanks
I have progressively overcome the above stages... However, i have met another issue regarding the Submission button, i would like to execute the above coding thru clicking of the button, may i find out how this can be done? Apologies as i am still very new to all this programming stuffs.. Hope you guys could help me out.. Thanks
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: adding graphics to GUI
- Next Thread: Help
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






