Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package working;// Fig. 28.28: DisplayQueryResults.java
- // Display the contents of the Authors table in the books database.
- // Átírta: Keszthelyi Zsolt 2017. április 17.
- // (9. kiadás)
- import connection.BookResultTableModel;
- import connection.ResultSetTableModel;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.table.*;
- import java.sql.SQLException;
- import java.util.regex.PatternSyntaxException;
- public class DisplayQueryResults extends JFrame {
- private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/Library?useSSL=false";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "password";
- // default query retrieves all data from authors table
- private static final String DEFAULT_QUERY = "SELECT * FROM book";
- private BookResultTableModel bookResultTableModel;
- // A contentPane-ben megjelenő gyerekkomponensek:
- private JTextArea taQuery = new JTextArea(DEFAULT_QUERY, 3, 100);
- private JScrollPane spQuery = new JScrollPane(taQuery,
- ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- private JButton btSubmit = new JButton("Submit Query");
- private JTable tbResult;
- private JScrollPane spResult;
- private JLabel lbFilter = new JLabel("Filter:");
- private JTextField tfFilter = new JTextField();
- private JButton btFilter = new JButton("Apply Filter");
- private TableRowSorter<TableModel> sorter;
- public DisplayQueryResults() {
- setTitle("Displaying Query Results");
- setBounds(100, 100, 500, 250);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- try {
- // create TableModel for results of query SELECT * FROM authors
- bookResultTableModel= new BookResultTableModel(DATABASE_URL,
- USERNAME, PASSWORD, DEFAULT_QUERY);
- System.out.println("succefull connection with books");
- }
- catch (SQLException sqlException) {
- JOptionPane.showMessageDialog(null, sqlException.getMessage(),
- "Database error", JOptionPane.ERROR_MESSAGE);
- System.exit(1); // terminate application
- }
- // A contentPane középső részének kialakítása
- // create JTable based on the resultSetTableModel
- tbResult = new JTable(bookResultTableModel);
- spResult = new JScrollPane(tbResult);
- tbResult.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
- tbResult.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
- tbResult.setFillsViewportHeight(true);
- add(spResult, BorderLayout.CENTER); // contentPane-hez adjuk
- // A contentPane északi részének kialakítása
- // set up JTextArea in which user types queries
- taQuery.setWrapStyleWord(true);
- taQuery.setLineWrap(true);
- Box boxNorth = Box.createHorizontalBox();
- boxNorth.add(spQuery);
- boxNorth.add(btSubmit);
- add(boxNorth, BorderLayout.NORTH); // contentPane-hez adjuk
- // A contentPane déli részének kialakítása
- Box boxSouth = Box.createHorizontalBox();
- boxSouth.add(lbFilter);
- boxSouth.add(tfFilter);
- boxSouth.add(btFilter);
- add(boxSouth, BorderLayout.SOUTH); // contentPane-hez adjuk
- setVisible(true);
- } // end DisplayQueryResults constructor
- // execute application
- public static void main(String args[]) {
- new DisplayQueryResults();
- } // end main
- } // end class DisplayQueryResults
- /**
- * ************************************************************************
- * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * Pearson Education,
- * Inc. All Rights Reserved. * * DISCLAIMER: The authors and publisher of this
- * book have used their * best efforts in preparing the book. These efforts
- * include the * development, research, and testing of the theories and programs
- * * to determine their effectiveness. The authors and publisher make * no
- * warranty of any kind, expressed or implied, with regard to these * programs
- * or to the documentation contained in these books. The authors * and publisher
- * shall not be liable in any event for incidental or * consequential damages in
- * connection with, or arising out of, the * furnishing, performance, or use of
- * these programs. *
- ************************************************************************
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement