help with database assignment

Reply

Join Date: Aug 2004
Posts: 15
Reputation: axiss is an unknown quantity at this point 
Solved Threads: 0
axiss's Avatar
axiss axiss is offline Offline
Newbie Poster

help with database assignment

 
0
  #1
Aug 14th, 2004
Well basically for the assignment I have to write a project that reads data from a .mdb database file and stores this data into a 2D array, then lets the user manipulate the data in the array (stuff like adding, editing, sorting, deleting and searching data) by using GUI and then finally writing back from the array into the database.

Everything in the program works exept for where I have to write to and from the array. :cry:

This is my program so far:

The class that creates an object that the array will consist of
  1. public class CarObject
  2. {
  3. private String make;
  4. private String model;
  5. private int year;
  6. private String price;
  7.  
  8. //Default constructor
  9. public CarObject()
  10. { }
  11.  
  12. //parameterised constructor
  13. public CarObject(String m, String mod, int y, String p)
  14. {
  15. make = m;
  16. model = mod;
  17. year = y;
  18. price = p;
  19. }
  20.  
  21. /**Mutator method for make field
  22. *@param m make as String
  23. */ public void setMake(String m)
  24. {
  25. make = m;
  26. }
  27.  
  28. /**Mutator method for model field
  29. *@param mod model as String
  30. */ public void setModel(String mod)
  31. { model = mod;
  32. }
  33.  
  34. /**Mutator method for year field
  35. *@param y year as int
  36. */ public void setYear(int y)
  37. {
  38. year = y;
  39. }
  40.  
  41. /**Mutator method for price field
  42. *@param p price as String
  43. */ public void setPrice(String p)
  44. {
  45. price = p;
  46. }
  47.  
  48. /**Accessor method for make field
  49. *@return make as String
  50. */ public String getMake()
  51. {
  52. return make;
  53. }
  54.  
  55. /**Accessor method for model field
  56. *@return model as String
  57. */ public String getModel()
  58. {
  59. return model;
  60. }
  61.  
  62. /**Accessor method for year field
  63. *@return year as int
  64. */ public int getYear()
  65. {
  66. return year;
  67. }
  68.  
  69. /**Accessor method for price field
  70. *@return price as String
  71. */ public String getPrice()
  72. {
  73. return price;
  74. }
  75.  
  76. /**toString method for CarObject, intended for screen output
  77. *@return CarObject properties as String
  78.  */ public String toString()
  79. {
  80. return this.getMake()+ ", " + this.getModel() + ", made in "
  81. + this.getYear() + " costs " + this.getPrice();
  82. }
  83. }//end of class


This is the main component of the program that calls mot of the other classes
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import java.io.*;
  5. import java.sql.*;
  6.  
  7. /**Car application class
  8.   */
  9. public class CarApplication extends JFrame
  10. {
  11. protected static CarArray ca = new CarArray();
  12. public CarApplication()
  13. {
  14. //Creates panel
  15. super("Main Menu");
  16. JPanel firstPanel = new JPanel();
  17.  
  18. //Allows the frame to be closed when the cross is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.  
  20. //Adds buttons to it
  21. JButton displayButton = new JButton("Display");
  22. JButton addButton = new JButton("Add");
  23. JButton deleteButton = new JButton("Delete");
  24. JButton editButton = new JButton("Edit");
  25. JButton sortButton = new JButton("Sort");
  26. JButton searchButton = new JButton("Search");
  27. JButton exitButton = new JButton("EXIT");
  28.  
  29. //Add components to it.
  30. getContentPane().add(firstPanel);
  31. firstPanel.add(displayButton);
  32. firstPanel.add(addButton);
  33. firstPanel.add(deleteButton);
  34. firstPanel.add(editButton);
  35. firstPanel.add(sortButton);
  36. firstPanel.add(searchButton);
  37. firstPanel.add(exitButton);
  38.  
  39. //Displays the window.
  40. setSize(200,200);
  41. setLocationRelativeTo(null); //centers it
  42. show();
  43.  
  44. displayButton.addActionListener(new ActionListener()
  45. {
  46. public void actionPerformed(ActionEvent e)
  47. {new DisplayFrame();}
  48. });
  49.  
  50. addButton.addActionListener(new ActionListener()
  51. {
  52. public void actionPerformed(ActionEvent e)
  53. {new AddFrame(); }
  54. });
  55.  
  56. deleteButton.addActionListener(new ActionListener()
  57. {
  58. public void actionPerformed(ActionEvent e)
  59. {new DeleteFrame();}
  60. });
  61.  
  62. editButton.addActionListener(new ActionListener()
  63. {
  64. public void actionPerformed(ActionEvent e)
  65. {new EditFrame1();}
  66. });
  67.  
  68. sortButton.addActionListener(new ActionListener()
  69. {
  70. public void actionPerformed(ActionEvent e)
  71. { new SortArray();}
  72. });
  73.  
  74. searchButton.addActionListener(new ActionListener()
  75. {
  76. public void actionPerformed(ActionEvent e)
  77. {new SearchFrame();}
  78. });
  79.  
  80. exitButton.addActionListener(new ActionListener()
  81. {
  82. public void actionPerformed(ActionEvent e)
  83. { setVisible(false);
  84. dispose();
  85. try
  86. {
  87. ca.arrayToFile();
  88. }
  89. catch(SQLException se)
  90. {
  91. System.out.println("ERROR!!!! CAN'T WRITE TO DATABASE FILE");
  92. }
  93. }
  94. });
  95. }
  96.  
  97. public static void main(String [] args) throws IOException
  98. {
  99. try
  100. {
  101. ca.fileToArray();
  102. }
  103. catch(SQLException se)
  104. {
  105. System.out.println("ERROR!!! CAN'T READ FROM DATABASE FILE");
  106. }
  107. new CarApplication();
  108. }
  109. }


This is where the error occurs, in the in the FileToArray() and ArrayToFile() methods. :mad: :o
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. /**Creates a CarArray object<br>
  5.   *CarArray object is an array of length 20 of CarObject objects
  6.   */
  7. public class CarArray
  8. {
  9. /**Array of length 20 of CarObject objects*/
  10. private CarObject[] carArr = new CarObject[20];
  11.  
  12. /**Number of items used in array, ie number of CarObject objects in array
  13.   */
  14. private int size = 0;
  15.  
  16. /**Default constructor */
  17. public CarArray()
  18. {}
  19.  
  20. /**Loads JDBC-ODBC bridge driver*/
  21. public static boolean loadODBC ()
  22. {
  23. try
  24. {
  25. Class.forName
  26. ("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println ("Bridge loaded");
  27. return true;
  28. }
  29. catch (ClassNotFoundException e)
  30. {
  31. System.out.println ("Bridge not found"); return false;
  32. } // catch block
  33. } // loadODBC method
  34.  
  35. /**Reads contents of cars.mdb file containing properties of CarObject
  36.   *objects and constructs CarObject objects in array using data as parameters.
  37.   *<br>size field is updated to reflect number of CarObject objects in array.*/
  38. public void fileToArray() throws SQLException
  39. {
  40. Connection conn;
  41. conn = DriverManager.getConnection ("jdbc:odbc:Cars", "", "");
  42. Statement stmt = conn.createStatement();
  43. String query = "SELECT Make, Model, Year, Price ";
  44. ResultSet rs = stmt.executeQuery(query);
  45. while (rs.next())
  46. {
  47. String m = rs.getString("Make");
  48. String mod = rs.getString("Model");
  49. int y = rs.getInt("Year");
  50. String p = rs.getString("Price");
  51. carArr[size] = new CarObject(m, mod, y, p);
  52. size++;
  53. }
  54. }
  55.  
  56. /**Writes contents of array to cars.mdb database file.
  57.   *The properties of each CarObject object in the array are recorded in the file */
  58. public void arrayToFile()throws SQLException
  59. {
  60. Connection conn;
  61. conn = DriverManager.getConnection ("jdbc:odbc:Cars", "", "");
  62.  
  63. for(int x = 0; x < size; x++)
  64. {
  65. Statement stmt = conn.createStatement();
  66. stmt.executeUpdate( "INSERT INTO Cars " + "VALUES (carArr[x].getMake(), carArr[x].getModel(), carArr[x].getYear(), carArr[x].getPrice())");
  67. }
  68. }
  69.  
  70. /**Sends carArr to DisplayFrame which calls it so that the data can be displayed
  71.   */
  72. public CarObject[] display()
  73. {
  74. return carArr;
  75. }
  76.  
  77. /**Allows user to input data for a new CarObject object to be added to the end
  78.   *of the array.
  79.   *<br>size field is incremented to account for new CarObject object added.
  80.   *@throws IOException*/
  81. public void addToArray(String m, String mod, String y, String p) throws IOException
  82. {
  83. carArr[size] = new CarObject(m, mod, Integer.parseInt(y), p);
  84. size++;
  85. }
  86.  
  87. /**Deletes one CarObject object from array.
  88.   *Position of object to be deleted is determined by user input.
  89.   *@throws IOException*/
  90. public void deleteCar(int pos) throws IOException
  91. {
  92. if(pos<19)//prevents array out of bound error if last object in the list is selected
  93. {
  94. for(int x = pos; x < size; x++)
  95. {
  96. carArr[x] = carArr[x+1];
  97. }
  98. }
  99. size--;
  100. }
  101.  
  102. /**Edits the properties of a CarObject object in the array. *@throws IOException
  103.   */
  104. public void editArray(String m, String mod, String y, String p, int edNum) throws IOException
  105. {
  106. String make
  107. ; String model
  108. ; int year;
  109. String price;
  110.  
  111. if(!(m.equals(null)))
  112. {
  113. make = m;
  114. }
  115. else
  116. {
  117. make = carArr[edNum].getMake();
  118. }
  119.  
  120. if(!(mod.equals(null)))
  121. {
  122. model = mod;
  123. }
  124. else
  125. {
  126. model = carArr[edNum].getModel();
  127. }
  128.  
  129. if(!(y.equals(null)))
  130. {
  131. year = Integer.parseInt(y);
  132. }
  133. else
  134. {
  135. year = carArr[edNum].getYear();
  136. }
  137.  
  138. if(!(p.equals(null)))
  139. {
  140. price = p;
  141. }
  142. else
  143. {
  144. price = carArr[edNum].getPrice();
  145. }
  146.  
  147. carArr[edNum] = new CarObject(make, model, year, price);
  148. }
  149.  
  150. /**Sorts the records in the array ascending by price property
  151.   */
  152. public void sortPrice()
  153. {
  154. //uses bubble sort
  155. for(int outt = size - 1; outt >= 0; outt--)
  156. {
  157. for(int inn = 0; inn < outt; inn++)
  158. {
  159. if(carArr[inn].getPrice().compareToIgnoreCase(carArr[inn + 1].getPrice()) > 0)
  160. {
  161. CarObject temp = new CarObject();
  162. temp = carArr[inn];
  163. carArr[inn] = carArr[inn + 1];
  164. carArr[inn + 1] = temp;
  165. }
  166. }
  167. }
  168. }
  169.  
  170. /**Searches for make input by user.
  171.   *<br>calls FoundFrame if found or NotFoundFrame if not found,
  172.   *@throws IOException
  173.   */
  174. public void searchArray(String searchText) throws IOException
  175. {
  176. //use binary search
  177. boolean found = false;
  178. int startPt = 0;
  179. int endPt = size - 1;
  180. int midPt = 0;
  181. while(startPt <= endPt && !found)
  182. {
  183. midPt = (endPt + startPt) / 2;
  184. if(carArr[midPt].getMake().equalsIgnoreCase(searchText))
  185. //it has been found
  186. {
  187. found = true;
  188. }
  189. else if(carArr[midPt].getMake().compareToIgnoreCase(searchText) > 0)
  190. //searchText may be in 1st half
  191. {
  192. endPt = midPt - 1;
  193. }
  194. else
  195. //searchText may be in 2nd half
  196. {
  197. startPt = midPt + 1;
  198. }
  199. }
  200. if(found)
  201. {
  202. new FoundFrame();
  203. }
  204. else
  205. {
  206. new NotFoundFrame();
  207. }
  208. }
  209. }//end of class

I have managed to find a fragment of code on writing to the array but I have not yet had time to test this:
  1. dbArray = rs.GetRows()
  2.  
  3. for i = 0 to ubound(dbArray, 2)
  4. response.write dbArray(0, i) & ",&nbsp;
  5. " response.write dbArray(1, i) & "<br>"
  6. next
Would this work effectively?

Please note that these are not all my classes but the rest just have to do with displaying different windows depends on which button the user presses in window created in the CarApplication class and these all work so it would just be a waste of both your and my time by posting them.

Any help would be grately apprechiated, so please don't hesitate if you can think of anything
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: help with database assignment

 
0
  #2
Aug 16th, 2004
Hi,

What is the error you receive when you run the program? Please be specific... maybe even copy and paste the error into this thread.

I've banged my head against the wall before with all sorts of dumb database errors.

By the way... your code is meticulously commented---a very good habit that will reap many benefits for you. Nice work.


Ed
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 15
Reputation: axiss is an unknown quantity at this point 
Solved Threads: 0
axiss's Avatar
axiss axiss is offline Offline
Newbie Poster

Re: help with database assignment

 
0
  #3
Aug 20th, 2004
Ok first of all I managed to get it to write to the array by changing the code in my fileToArray method to this:

  1. public void fileToArray() throws SQLException
  2. {
  3. loadODBC ();//calls method to load the driver
  4.  
  5. Connection conn;
  6. conn = DriverManager.getConnection ("jdbc:odbc:Cars", "", "");
  7.  
  8. Statement stmt = conn.createStatement();
  9. ResultSet rs = stmt.executeQuery("SELECT Make, Model, Year, Price FROM cars");
  10.  
  11. while (rs.next())
  12. {
  13. String m = rs.getString(1);
  14. String mod = rs.getString(2);
  15. int y = rs.getInt(3);
  16. String p = rs.getString(4);
  17. carArr[size] = new CarObject(m, mod, y, p);
  18. size++;
  19. }
  20.  
  21. recordNum = size;//classwide variable storing the amount of record in the database
  22. }

This works great so I'm really happy.

Originally Posted by cosi
What is the error you receive when you run the program? Please be specific... maybe even copy and paste the error into this thread.
Well there isn't doesn't actually give me an error, just my try catch block that tries to load my arrayToFile method printed the error that it can't write to the database file as specified.
  1. try
  2. {
  3. ca.arrayToFile();
  4. }
  5. catch(SQLException se)
  6. {
  7. System.out.println("ERROR!!!! CAN'T WRITE TO DATABASE FILE");
  8. }

So it's only the arrayToFile method that I need help with now.
I've changed it quite a bit and this is what it looks like now:
  1. public void arrayToFile()throws SQLException
  2. {
  3. loadODBC ();//calls method to load the driver
  4.  
  5. Connection conn;
  6. conn = DriverManager.getConnection ("jdbc:odbc:Cars", "", "");
  7.  
  8.  
  9. Statement stmt = conn.createStatement();
  10. ResultSet rs = stmt.executeQuery("SELECT Make, Model, Year, Price FROM cars");
  11.  
  12. //the array holds the same amount of car data as Cars.mdb
  13. if(size == recordNum)
  14. {
  15. for(int x = 0; x < size; x++)
  16. {
  17. //UPDATE
  18. }
  19. }
  20.  
  21. //the array holds more car data then Cars.mdb
  22. else if(size > recordNum)
  23. {
  24. for(int x = 0; x < size; x++)
  25. {
  26. if(x <= recordNum)
  27. {
  28. //UPDATE
  29. }
  30.  
  31. else
  32. {
  33. //INSERT
  34. }
  35.  
  36. }
  37. }
  38.  
  39. //Cars.mdb holds more car data then the array
  40. else
  41. {
  42. for(int x = 0; x < recordNum; x++)
  43. {
  44. while(size < recordNum)
  45. {
  46. String delNum = String.valueOf(recordNum);
  47. stmt.executeUpdate("DELETE FROM cars WHERE ID = " + delNum);
  48. recordNum--;
  49. }
  50.  
  51. //UPDATE
  52. }
  53. }
  54.  
  55. }

Basically what I'm trying to do is compare the amount of record in the array with the amount in the database (stored in recordNum) so that basically is they are the same it updates all the records, if there are more records in the array the it updates and inserts records and if the array has less records then it deletes the records from the database till it equals the amount in the array and then updates the remaining records.

The delete part works fine, I'm just haveing trouble with the UPDATE AND INSERT statements since I've never worked with databases before and honestly have no idea how the use them. :-|

By the way, thanks for trying to help so far.
Me fail English?
Unpossible
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 15
Reputation: axiss is an unknown quantity at this point 
Solved Threads: 0
axiss's Avatar
axiss axiss is offline Offline
Newbie Poster

Re: help with database assignment

 
0
  #4
Aug 28th, 2004
By the way if it helps my database has the following columns in it in this order:
ID, make, model, year, price
(ID is just a sequencial numbers for every record)

I need to put my values from my array into my database by using my .getMake() .getModel() .getPrice() and .getYear() methods

so if anyone can think of the proper syntax to use to UPDATE and INSERT statements it would be a great help since I have really tried everything and really can't get it to work.
Me fail English?
Unpossible
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC