java app with INSERT statement...

Thread Solved

Join Date: Dec 2008
Posts: 24
Reputation: jeffreyjs is an unknown quantity at this point 
Solved Threads: 0
jeffreyjs jeffreyjs is offline Offline
Newbie Poster

java app with INSERT statement...

 
0
  #1
Dec 29th, 2008
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

  1. public class Payment {
  2.  
  3. private int InvoiceNo;
  4. private String Date;
  5. private String Time;
  6. private int TableNo;
  7. private String PayMode;
  8. private String ProductName;
  9. private int Qty;
  10. private String Size;
  11. private double UnitPrice;
  12. private double TotalPrice;
  13. private double SubTotal;
  14. private float GST;
  15. private float SurCharge;
  16. private float MemDisc;
  17. private float DelCharge;
  18. private double GrandTotal;
  19. private double Tendered;
  20. private double Changed;
  21. private String Name;
  22. private int CreditCardNo;
  23. private String CreditCardType;
  24. private String ExpDate;
  25. private int CVC;
  26.  
  27. public String getName() {
  28. return Name;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.Name = name;
  33. }
  34.  
  35. public int getCreditNo() {
  36. return CreditCardNo;
  37. }
  38.  
  39. public void setCreditNo(int creditNo) {
  40. this.CreditCardNo = creditNo;
  41. }
  42.  
  43. public String getExpiryDate() {
  44. return ExpDate;
  45. }
  46.  
  47. public void setExpiryDate(String expiryDate) {
  48. ExpDate = expiryDate;
  49. }
  50.  
  51. public String getCreditType() {
  52. return CreditCardType;
  53. }
  54.  
  55. public void setCreditType(String creditType) {
  56. this.CreditCardType = creditType;
  57. }
  58.  
  59. public int getCVC() {
  60. return CVC;
  61. }
  62.  
  63. public void setCVC(int cvc) {
  64. CVC = cvc;
  65. }
  66.  
  67. public float getGST() {
  68. return GST;
  69. }
  70.  
  71. public void setGST(float gst) {
  72. GST = gst;
  73. }
  74.  
  75. public Payment() {
  76. // TODO Auto-generated constructor stub
  77. }
  78.  
  79. public boolean retrieveProduct(){
  80. // declare local variables
  81.  
  82. boolean success = false;
  83.  
  84. ResultSet rs = null;
  85.  
  86. DBController db = new DBController();
  87.  
  88. // step 1 of using DBcontroller, passing data source name setup during last practical
  89.  
  90. db.setUp("SweetParadise");
  91.  
  92. // declare the SQL
  93.  
  94. String dbQuery = "SELECT * FROM PAYMENT WHERE InvoiceNo =" + InvoiceNo;
  95.  
  96. // step 2 of using DBcontroller, for retrieve SQL use readRequest method
  97.  
  98. rs = db.readRequest(dbQuery);
  99.  
  100. try{
  101.  
  102. if (rs.next()){
  103.  
  104. InvoiceNo = rs.getInt("InvoiceNo");
  105.  
  106. /*name = rs.getString("ProductName");
  107.  
  108. image = rs.getString("ProductImage");
  109.  
  110. unitPrice = rs.getDouble("ProductUnitPrice");
  111. */
  112. success = true;
  113. }
  114. }
  115.  
  116. catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119.  
  120. // step 3 of using DBcontroller
  121. db.terminate();
  122. return success;
  123. }
  124.  
  125. public boolean createPayment(){
  126.  
  127. // declare local variables
  128.  
  129. boolean success = false;
  130.  
  131. ResultSet rs = null;
  132.  
  133. DBController db = new DBController();
  134.  
  135. // step 1 of using DBcontroller, passing data source name setup during last practical
  136.  
  137. db.setUp("SweetParadise");
  138.  
  139. // declare the SQL
  140.  
  141. String dbQuery = "INSERT INTO PAYMENT(InvoiceNo, Date, Time, TableNo, PayMode, ";
  142.  
  143. dbQuery = dbQuery + "ProductName, Qty, Size, UnitPrice, TotalPrice, ";
  144.  
  145. dbQuery = dbQuery + "SubTotal, GST, SurCharge, MemDisc, DelCharge, GrandTotal, ";
  146.  
  147. dbQuery = dbQuery + "Tendered, Changed, Name, CreditCardNo, CreditCardType, ExpDate, CVC) ";
  148.  
  149. dbQuery = dbQuery + "VALUES (" + InvoiceNo + ",'" + Date + "', " + Time + ", ";
  150.  
  151. dbQuery = dbQuery + "'" + TableNo + "', '" + PayMode + "' , '" + ProductName + "', '" + Qty + "',";
  152.  
  153. dbQuery = dbQuery + "'" + Size + "', '" + UnitPrice + "', '" + TotalPrice + "', ";
  154.  
  155. dbQuery = dbQuery + "'" + SubTotal + "', '" + GST + "', '" + SurCharge + "', '" + MemDisc + "', " ;
  156.  
  157. dbQuery = dbQuery + "'" + DelCharge + "', '" + GrandTotal + "', '" + Tendered + "', '" + Changed + "', '" + Name + "',";
  158.  
  159. dbQuery = dbQuery + "'" + CreditCardNo + "', '" + CreditCardType + "', '" + ExpDate + "', '" + CVC + "')";
  160.  
  161.  
  162.  
  163. // step 2 of using DBcontroller, use updateRequestKey method if you want the value of
  164.  
  165. // the key back (usually, if the key is autoNumber)
  166.  
  167. rs = db.updateRequestKey(dbQuery);
  168.  
  169.  
  170.  
  171. try{
  172.  
  173.  
  174.  
  175. if (rs.next()){
  176.  
  177.  
  178.  
  179.  
  180. // this will return the ID which is a autoNumber primary key
  181.  
  182. InvoiceNo = rs.getInt("InvoiceNo");
  183.  
  184. success = true;
  185.  
  186. }
  187.  
  188.  
  189.  
  190. }
  191.  
  192. catch (Exception e) {
  193.  
  194. e.printStackTrace();
  195.  
  196.  
  197.  
  198. }
  199.  
  200. // step 3 of using DBcontroller
  201.  
  202. db.terminate();
  203.  
  204. return success;
  205.  
  206. }
  207.  
  208.  
  209.  
  210. /*public static ArrayList<Product> getProductByCategory(int inCatID){
  211.  
  212.  
  213.  
  214. // declare local variables
  215.  
  216. ArrayList<Product> pdtList = new ArrayList<Product>();
  217.  
  218.  
  219.  
  220. ResultSet rs = null;
  221.  
  222. DBController db = new DBController();
  223.  
  224.  
  225.  
  226. // step 1 of using DBcontroller, passing data source name setup during last practical
  227.  
  228. db.setUp("myDatabase");
  229.  
  230. String dbQuery = "SELECT * FROM PRODUCTS WHERE ProductCategoryID = " + inCatID;
  231.  
  232.  
  233.  
  234. // step 2 of using DBcontroller, use updateRequest method
  235.  
  236. rs = db.readRequest(dbQuery);
  237.  
  238.  
  239.  
  240. try{
  241.  
  242.  
  243.  
  244. while (rs.next()){
  245.  
  246. int pdtID = rs.getInt("ProductID");
  247.  
  248. int pdtCatID = rs.getInt("ProductCategoryID");
  249.  
  250. String pdtName = rs.getString("ProductName");
  251.  
  252. String pdtImage = rs.getString("ProductImage");
  253.  
  254. double pdtUnitPrice = rs.getDouble("ProductUnitPrice");
  255.  
  256. Product pdt = new Product(pdtID, pdtCatID, pdtName, pdtImage, pdtUnitPrice);
  257.  
  258. pdtList.add(pdt);
  259.  
  260. }
  261.  
  262. }
  263.  
  264. catch (Exception e) {
  265.  
  266. e.printStackTrace();
  267.  
  268. }
  269.  
  270. // step 3 of using DBcontroller
  271.  
  272. db.terminate();
  273.  
  274. return pdtList;
  275.  
  276. }
  277.  
  278. */
  279.  
  280. public static void main(String[] args) {
  281.  
  282. Payment p1 = new Payment();
  283.  
  284. p1.setName("test");
  285.  
  286. p1.setCreditNo(3567843);
  287.  
  288. System.out.println(p1.createPayment());
  289.  
  290. }
  291.  
  292. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: java app with INSERT statement...

 
0
  #2
Dec 29th, 2008
Originally Posted by jeffreyjs View Post
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
Hello,

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: java app with INSERT statement...

 
0
  #3
Dec 29th, 2008
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: java app with INSERT statement...

 
0
  #4
Dec 29th, 2008
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.
  1. insert into testTable values (1,'user','pass');
If you want to left any of attribute null (only if it can hold null value).
  1. insert into testTable (username, password ) values ('user','pass');
if you only know the attributes name but dont know the series.
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: jeffreyjs is an unknown quantity at this point 
Solved Threads: 0
jeffreyjs jeffreyjs is offline Offline
Newbie Poster

Re: java app with INSERT statement...

 
0
  #5
Dec 30th, 2008
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!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: java app with INSERT statement...

 
0
  #6
Dec 31st, 2008
  1. private JRadioButton male;
  2. private JRadioButton female;
  3. private ButtonGroup gender;
  4.  
  5. // create the radiobuttons
  6. male = new JRadioButton();
  7. female = new JRadioButton();
  8. gender = new ButtonGroup();
  9.  
  10. // set the necessary parameters for the radio buttons
  11. male.setText("M");
  12. male.setSelected(true);
  13. female.setText("F");
  14. female.setSelected(false);
  15.  
  16. /*
  17. *can be used to catch the button on which the event is generated * (by checking the action commad)
  18. */
  19. male.setActionCommand("Male");
  20. female.setActionCommand("Female");
  21.  
  22. // finally add the two buttons to the 'gender group'
  23. gender.add(male);
  24. 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 !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: jeffreyjs is an unknown quantity at this point 
Solved Threads: 0
jeffreyjs jeffreyjs is offline Offline
Newbie Poster

Re: java app with INSERT statement...

 
0
  #7
Dec 31st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,189
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: java app with INSERT statement...

 
0
  #8
Dec 31st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: java app with INSERT statement...

 
0
  #9
Dec 31st, 2008
Originally Posted by jeffreyjs View Post
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?
Probably the number of values you are using are not the same with the number of columns you have in your query.
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 24
Reputation: jeffreyjs is an unknown quantity at this point 
Solved Threads: 0
jeffreyjs jeffreyjs is offline Offline
Newbie Poster

Re: java app with INSERT statement...

 
0
  #10
Jan 4th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC