help with sql d/base & java gui

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

help with sql d/base & java gui

 
0
  #1
Dec 16th, 2007
HI all am trying to construct this hybrid of java and mysql. the data comes from a mysql database and I want it to display in the gui. this I have achieved thus far. However I have buttons that sort by surname, first name, ID tag etc....I need event handlers for these buttons but am quite unsure as to how to do it. any help would be much appreciated. Thanks in advance.

  1. /* Student Contact Database GUI
  2. * Phillip Wells
  3. */
  4. import java.awt.BorderLayout;
  5. // imports java class. All import class statements tell the compiler to use a class that is defined in the Java API.
  6. // Borderlayout is a layout manager that assists GUI layout.
  7. import javax.swing.*; // imports java class. Swing enables the use of a GUI.
  8. import javax.swing.JOptionPane; // imports java class. JOptionPane displays messages in a dialog box as opposed to a console window.
  9. import javax.swing.JPanel; // imports java class. A component of a GUI.
  10. import javax.swing.JFrame; // imports java class. A component of a GUI.
  11. import javax.swing.JButton; // imports java class. A component of a GUI.
  12. import javax.swing.JScrollPane; // imports java class. A component of a GUI.
  13. import javax.swing.JTable; // imports java class. A component of a GUI.
  14. import java.awt.*; // imports java class. Similar to Swing but with different components and functions.
  15. import java.awt.event.*; // imports java class. Deals with events.
  16. import java.awt.event.ActionEvent; // imports java class. Deals with events.
  17. import java.awt.event.ActionListener; // imports java class. Deals with events.
  18. import java.sql.*; // imports java class. Provides API for accessing and processing data stored in a data source.
  19. import java.util.*; // imports java class. Contains miscellaneous utility classes such as strings.
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. public class studentContact extends JFrame { // public class declaration. The ‘public’ statement enables class availability to other java elements.
  27.  
  28. private JPanel jContentPane; // initialises content pane
  29. private JButton snam, id, fname, exit; // initialises Jbuttons
  30. String firstname = "firstname"; //initialises String firstname
  31. String secondname = "secondname"; //initialises String
  32.  
  33.  
  34. public studentContact() {
  35. Vector columnNames = new Vector(); // creates new vector object. Vectors are arrays that are expandable.
  36. Vector data = new Vector();
  37.  
  38. initialize();
  39.  
  40. try {
  41. // Connect to the Database
  42.  
  43. String driver = "com.mysql.jdbc.Driver"; // connect to JDBC driver
  44.  
  45. String url = "jdbc:mysql://localhost/Studentprofiles"; //location of Database
  46. String userid = "root"; //user logon information for MySQL server
  47. String password = ""; //logon password for above
  48.  
  49. Class.forName(driver); //reference to JDBC connector
  50. Connection connection = DriverManager.getConnection(url, userid,
  51. password); // initiates connection
  52.  
  53.  
  54.  
  55. // Read data from a table
  56.  
  57. String sql = "Select * from studentprofile order by "+ firstname;
  58. //SQL query sent to database, orders results by firstname.
  59. Statement stmt = connection.createStatement
  60. (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  61. //statement to create connection.
  62. //Scroll sensitive allows movement forth and back through results.
  63. //Concur updatable allows updating of database.
  64. ResultSet rs = stmt.executeQuery(sql); // executes SQL query stated above and sets the results in a table
  65. ResultSetMetaData md = rs.getMetaData(); // used to get the properties of the columns in a ResultSet object.
  66. int columns = md.getColumnCount(); //
  67.  
  68.  
  69. for (int i = 1; i <= columns; i++) {
  70. columnNames.addElement(md.getColumnName(i)); // Get column names
  71.  
  72. }
  73.  
  74.  
  75. while (rs.next()) {
  76. Vector row = new Vector(columns); // vectors data from table
  77.  
  78. for (int i = 1; i <= columns; i++) {
  79. row.addElement(rs.getObject(i)); // Get row data
  80.  
  81. }
  82.  
  83. data.addElement(row); // adds row data
  84. }
  85.  
  86. rs.close();
  87. stmt.close();
  88. } catch (Exception e) { // catches exceptions
  89. System.out.println(e); // prints exception message
  90. }
  91.  
  92. JTable table = new JTable(data, columnNames) { //constructs JTable
  93.  
  94. public Class getColumnClass(int column) {
  95. for (int row = 0; row < getRowCount(); row++) {
  96. Object o = getValueAt(row, column);
  97.  
  98. if (o != null) {
  99. return o.getClass();
  100. }
  101. }
  102.  
  103. return Object.class;
  104. }
  105. };
  106.  
  107. JScrollPane scrollPane = new JScrollPane( table ); // constructs scrollpane 'table'
  108. getContentPane().add(new JScrollPane(table), BorderLayout.SOUTH); //adds table to a scrollpane
  109. }
  110.  
  111. private void initialize() {
  112. this.setContentPane(getJContentPane());
  113. this.setTitle("Student Contact Database"); // sets title of table
  114.  
  115. ButtonListener b1 = new ButtonListener(); // constructs button listener
  116.  
  117. snam = new JButton ("Sort by surname"); // constructs Jbutton
  118. snam.addActionListener(b1); // adds action listener
  119. jContentPane.add(snam); //adds button to pane
  120.  
  121. id = new JButton ("Sort by ID"); // constructs Jbutton
  122. id.addActionListener(b1); // adds action listener
  123. jContentPane.add(id); //adds button to pane
  124.  
  125. fname = new JButton ("Sort by first name"); // constructs Jbutton
  126. fname.addActionListener(b1); // adds action listener
  127. jContentPane.add(fname); //adds button to pane
  128.  
  129. exit = new JButton ("Exit"); // constructs Jbutton
  130. exit.addActionListener(b1); // adds action listener
  131. jContentPane.add(exit); //adds button to pane
  132.  
  133.  
  134. }
  135.  
  136.  
  137. private JPanel getJContentPane() {
  138. if (jContentPane == null) {
  139. jContentPane = new JPanel(); // constructs new panel
  140. jContentPane.setLayout(new FlowLayout()); // sets new layout manager
  141.  
  142. }
  143. return jContentPane; // returns Jcontentpane
  144. }
  145.  
  146. private class ButtonListener implements ActionListener { // create inner class button listener that uses action listener
  147. public void actionPerformed (ActionEvent e)
  148. {
  149. if (e.getSource () == exit) // adds listener to button exit.
  150. {
  151. System.exit(0); // exits the GUI
  152. }
  153. if (e.getSource () == snam)
  154. {
  155.  
  156. }
  157. if (e.getSource () == id)
  158. {
  159.  
  160. }
  161. if (e.getSource () == fname)
  162. {
  163.  
  164. }
  165. }
  166. }
  167.  
  168.  
  169.  
  170.  
  171. public static void main(String[] args) { // declaration of main method
  172.  
  173.  
  174. studentContact frame = new studentContact(); // constructs new frame
  175. frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //exits frame on closing
  176. frame.setSize(600, 300); // set size of frame
  177. frame.setVisible(true); // displays frame
  178.  
  179.  
  180. }
  181.  
  182. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: help with sql d/base & java gui

 
0
  #2
Dec 16th, 2007
Get rid of ALL your comments except the header comments.
Replace them all by a proper Javadoc block per method.

Close ALL your JDBC resources as soon as you're done with them. General rule: never let them survive outside the method in which they're used.

Concentrate all database operations in their own class.

Create a special class to hold your records.

Use a custom TableModel to hold the information.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: help with sql d/base & java gui

 
0
  #3
Dec 16th, 2007
ok thanks for the advice will give it a bash, sorry about comments did not realise they would look so unsightly!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 51
Reputation: Nige Ridd is an unknown quantity at this point 
Solved Threads: 9
Nige Ridd Nige Ridd is offline Offline
Junior Poster in Training

Re: help with sql d/base & java gui

 
0
  #4
Dec 16th, 2007
I'm fairly new to GUI coding, but what I would have done is to separate out the routine that populates the table, passing into it the sort order as a parameter. Then when the user asks the data to be resorted, you can simply call this routine with the sort order required.
A couple of things about the code as well, make sure that you ALWAYS close any database resources you use, ResultSets, Statements and ( for Oracle anyway ) Connections. To help in this I've always put the close of these resources in the finally clause of the try/catch block that allocates and uses these resources. This means that if your ResultSet fails for some reason, the Statement and Connection are closed, or if your statement is invalid and the executeQuery fails - the Connection gets closed. Unless anyone know a better way of doing this.
Nige
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: help with sql d/base & java gui

 
0
  #5
Dec 18th, 2007
just had a thought, is there a way of sorting the data once it is in the table, through swing commands...? am about to check API now
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: help with sql d/base & java gui

 
0
  #6
Dec 18th, 2007
brilliant, have found what I need. BY inserting 'table.setAutoCreateRowSorter(true);' the titles of the table (when doube tapped) sort appropriately. Got that straight from API - great
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: help with sql d/base & java gui

 
1
  #7
Dec 18th, 2007
If you take a look in your JDK installation folder under "demo\jfc\TableExample", there are several table example programs, including a TableSorter.

I've also attached a zip file containing a JSortTable implementation from Java Pro magazine. I was going to link to it in the archives, but it seems Java Pro got swallowed up by another company and I can't locate the archives.

Edit: Oops, guess you found something hehe. Didn't know they had rolled that into the api in 1.6.
Last edited by Ezzaral; Dec 18th, 2007 at 1:19 pm.
Attached Files
File Type: zip jp0208vc.zip (30.0 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: help with sql d/base & java gui

 
0
  #8
Dec 18th, 2007
lol thanks for the assistance anyhow. you must have been typing that whilst my message was sent. cheers!
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the Java Forum


Views: 1286 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC