943,777 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2315
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 29th, 2008
0

java app with INSERT statement...

Expand 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

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jeffreyjs is offline Offline
28 posts
since Dec 2008
Dec 29th, 2008
0

Re: java app with INSERT statement...

Click to Expand / Collapse  Quote originally posted by jeffreyjs ...
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.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Dec 29th, 2008
0

Re: java app with INSERT statement...

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 29th, 2008
0

Re: java app with INSERT statement...

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.
Java Syntax (Toggle Plain Text)
  1. insert into testTable values (1,'user','pass');
If you want to left any of attribute null (only if it can hold null value).
Java Syntax (Toggle Plain Text)
  1. insert into testTable (username, password ) values ('user','pass');
if you only know the attributes name but dont know the series.
Java Syntax (Toggle Plain Text)
  1. insert into testTable (username, userID, password ) values ('user',1,'pass');

Regards!
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Dec 30th, 2008
0

Re: java app with INSERT statement...

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!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
jeffreyjs is offline Offline
28 posts
since Dec 2008
Dec 31st, 2008
0

Re: java app with INSERT statement...

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 31st, 2008
0

Re: java app with INSERT statement...

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
jeffreyjs is offline Offline
28 posts
since Dec 2008
Dec 31st, 2008
0

Re: java app with INSERT statement...

Use JFrame to build what ever you need
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 31st, 2008
0

Re: java app with INSERT statement...

Click to Expand / Collapse  Quote originally posted by jeffreyjs ...
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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 4th, 2009
0

Re: java app with INSERT statement...

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
Reputation Points: 10
Solved Threads: 0
Light Poster
jeffreyjs is offline Offline
28 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need Help in Connecting MySQL Database to JTable
Next Thread in Java Forum Timeline: Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC