Advertisement
Guest User

Tábla

a guest
May 9th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package working;// Fig. 28.28: DisplayQueryResults.java
  2. // Display the contents of the Authors table in the books database.
  3.  
  4. // Átírta: Keszthelyi Zsolt 2017. április 17.
  5. // (9. kiadás)
  6. import connection.BookResultTableModel;
  7. import connection.ResultSetTableModel;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import javax.swing.table.*;
  13. import java.sql.SQLException;
  14. import java.util.regex.PatternSyntaxException;
  15.  
  16. public class DisplayQueryResults extends JFrame {
  17.     private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/Library?useSSL=false";
  18.     private static final String USERNAME = "root";
  19.     private static final String PASSWORD = "password";
  20.     // default query retrieves all data from authors table
  21.     private static final String DEFAULT_QUERY = "SELECT * FROM book";
  22.  
  23.     private BookResultTableModel bookResultTableModel;
  24.     // A contentPane-ben megjelenő gyerekkomponensek:
  25.     private JTextArea taQuery = new JTextArea(DEFAULT_QUERY, 3, 100);
  26.     private JScrollPane spQuery = new JScrollPane(taQuery,
  27.                     ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
  28.                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  29.     private JButton btSubmit = new JButton("Submit Query");        
  30.     private JTable tbResult;
  31.     private JScrollPane spResult;
  32.     private JLabel lbFilter = new JLabel("Filter:");    
  33.     private JTextField tfFilter = new JTextField();    
  34.     private JButton btFilter = new JButton("Apply Filter");    
  35.  
  36.     private TableRowSorter<TableModel> sorter;
  37.    
  38.     public DisplayQueryResults() {
  39.         setTitle("Displaying Query Results");
  40.         setBounds(100, 100, 500, 250);
  41.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  42.  
  43.         try {
  44.             // create TableModel for results of query SELECT * FROM authors
  45.             bookResultTableModel= new BookResultTableModel(DATABASE_URL,
  46.                     USERNAME, PASSWORD, DEFAULT_QUERY);
  47.             System.out.println("succefull connection with books");
  48.         }
  49.         catch (SQLException sqlException) {
  50.             JOptionPane.showMessageDialog(null, sqlException.getMessage(),
  51.                     "Database error", JOptionPane.ERROR_MESSAGE);
  52.             System.exit(1); // terminate application
  53.         }
  54.  
  55.         // A contentPane középső részének kialakítása
  56.         // create JTable based on the resultSetTableModel
  57.         tbResult = new JTable(bookResultTableModel);
  58.         spResult = new JScrollPane(tbResult);
  59.         tbResult.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  60.         tbResult.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  61.         tbResult.setFillsViewportHeight(true);
  62.         add(spResult, BorderLayout.CENTER); // contentPane-hez adjuk            
  63.  
  64.         // A contentPane északi részének kialakítása
  65.         // set up JTextArea in which user types queries
  66.         taQuery.setWrapStyleWord(true);
  67.         taQuery.setLineWrap(true);
  68.         Box boxNorth = Box.createHorizontalBox();
  69.         boxNorth.add(spQuery);
  70.         boxNorth.add(btSubmit);
  71.         add(boxNorth, BorderLayout.NORTH); // contentPane-hez adjuk
  72.  
  73.         // A contentPane déli részének kialakítása        
  74.         Box boxSouth = Box.createHorizontalBox();
  75.         boxSouth.add(lbFilter);
  76.         boxSouth.add(tfFilter);
  77.         boxSouth.add(btFilter);
  78.         add(boxSouth, BorderLayout.SOUTH); // contentPane-hez adjuk
  79.  
  80.        
  81.  
  82.  
  83.         setVisible(true);                  
  84.     } // end DisplayQueryResults constructor
  85.  
  86.    
  87.     // execute application
  88.     public static void main(String args[]) {
  89.         new DisplayQueryResults();
  90.     } // end main
  91. } // end class DisplayQueryResults
  92.  
  93. /**
  94.  * ************************************************************************
  95.  * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * Pearson Education,
  96.  * Inc. All Rights Reserved. * * DISCLAIMER: The authors and publisher of this
  97.  * book have used their * best efforts in preparing the book. These efforts
  98.  * include the * development, research, and testing of the theories and programs
  99.  * * to determine their effectiveness. The authors and publisher make * no
  100.  * warranty of any kind, expressed or implied, with regard to these * programs
  101.  * or to the documentation contained in these books. The authors * and publisher
  102.  * shall not be liable in any event for incidental or * consequential damages in
  103.  * connection with, or arising out of, the * furnishing, performance, or use of
  104.  * these programs. *
  105.  ************************************************************************
  106.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement