Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project3;
- // Display the results of queries against the bikes table in the bikedb database.
- import java.awt.BorderLayout;
- import javax.swing.JComboBox;
- import java.awt.Color;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.JScrollPane;
- import javax.swing.ScrollPaneConstants;
- import javax.sql.DataSource;
- import com.mysql.cj.jdbc.MysqlDataSource;
- import javax.swing.JTable;
- import javax.swing.JOptionPane;
- import javax.swing.JButton;
- import javax.swing.Box;
- public class DisplayQueryResults extends JFrame
- {
- // default query retrieves all data from bikes table
- static final String DEFAULT_QUERY = "SELECT * FROM bikes";
- private Connection connection;
- private Statement statement;
- private ResultSetTableModel tableModel;
- private JTextArea queryArea;
- // create ResultSetTableModel and GUI
- public DisplayQueryResults()
- {
- super( "Displaying Query Results" );
- boolean isConnected;
- // create ResultSetTableModel and display database table
- try
- {
- // create TableModel for results of query SELECT * FROM bikes
- tableModel = new ResultSetTableModel( DEFAULT_QUERY);
- // set up JTextArea in which user types queries
- // queryArea = new JTextArea( 3, 100);
- queryArea = new JTextArea( DEFAULT_QUERY, 3, 100 );
- queryArea.setWrapStyleWord( true );
- queryArea.setLineWrap( true );
- JScrollPane scrollPane = new JScrollPane( queryArea,
- ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- // set up JButton for submitting queries
- JButton submitButton = new JButton( " Execute SQL Command " );
- submitButton.setBackground(Color.GREEN);
- submitButton.setForeground(Color.BLUE);
- JButton clearButton = new JButton( " Clear SQL Command " );
- clearButton.setBackground(Color.WHITE);
- clearButton.setForeground(Color.RED);
- JButton connectButton = new JButton( " Connect to Database " );
- connectButton.setBackground(Color.BLUE);
- connectButton.setForeground(Color.YELLOW);
- String[] connection1 = { "jdbc:mysql://127.0.0.1:3312/project3"};
- String[] connection2 = { "com.mysql.cj.jdbc.MysqlDataSource"};
- JComboBox<String> connectionMenu = new JComboBox<String>(connection1);
- JLabel cMenu1 = new JLabel("JDBC Driver");
- JComboBox<String> connectionMenu2 = new JComboBox<String>(connection2);
- JLabel cMenu2 = new JLabel("Database Username");
- JTextField userField = new JTextField(16);
- JTextField passField = new JTextField(16);
- // create Box to manage placement of queryArea and
- // submitButton in GUI
- Box box = Box.createHorizontalBox();
- box.add( scrollPane );
- box.add( submitButton );
- box.add( clearButton );
- box.add( connectButton );
- box.add(cMenu1);
- box.add(connectionMenu);
- box.add(cMenu2);
- box.add(connectionMenu2);
- Box box2 = Box.createHorizontalBox();
- box2.add(clearButton);
- //THIS IS WHERE I ADD MORE BUTTONS
- // create JTable delegate for tableModel
- JTable resultTable = new JTable( tableModel );
- // place GUI components on content pane
- add( box, BorderLayout.NORTH );
- add(queryArea, BorderLayout.EAST);
- add( new JScrollPane( resultTable ), BorderLayout.SOUTH );
- add(box2, BorderLayout.WEST);
- box2.add(new JLabel("Username"));
- box2.add(userField, BorderLayout.WEST);
- box2.add(new JLabel("Password"));
- box2.add(passField, BorderLayout.WEST);
- //box2.add(new JTextField(16), BorderLayout.WEST); <- not sure if I need this
- MysqlDataSource dataSource = null;
- connectButton.addActionListener(
- new ActionListener()
- {
- // pass query to table model
- public void actionPerformed( ActionEvent event )
- {
- // perform a new query
- try
- {
- dataSource = new MysqlDataSource();
- dataSource.setURL(connectionMenu2.getItemAt(0).toString());
- dataSource.setUser(userField.getText());
- dataSource.setPassword(passField.getText());
- Connection connection = dataSource.getConnection();
- // create Statement to query database
- //statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
- }
- catch ( SQLException sqlException )
- {
- sqlException.printStackTrace();
- System.exit( 1 );
- }
- }
- });
- clearButton.addActionListener(
- new ActionListener()
- {
- // pass query to table model
- public void actionPerformed( ActionEvent event )
- {
- resultTable.setVisible(false);
- }
- });
- // create event listener for submitButton
- submitButton.addActionListener(
- new ActionListener()
- {
- // pass query to table model
- public void actionPerformed( ActionEvent event )
- {
- // perform a new query
- try
- {
- resultTable.setVisible(true);
- if (queryArea.getText().toUpperCase().contains("SELECT")) {
- tableModel.setQuery( queryArea.getText() );
- resultTable.setModel(tableModel);
- }
- else {
- tableModel.setUpdate(queryArea.getText());
- }
- //
- } // end try
- catch ( SQLException sqlException )
- {
- JOptionPane.showMessageDialog( null,
- sqlException.getMessage(), "Database error",
- JOptionPane.ERROR_MESSAGE );
- // try to recover from invalid user query
- // by executing default query
- try
- {
- tableModel.setQuery( DEFAULT_QUERY );
- queryArea.setText( DEFAULT_QUERY );
- } // end try
- catch ( SQLException sqlException2 )
- {
- JOptionPane.showMessageDialog( null,
- sqlException2.getMessage(), "Database error",
- JOptionPane.ERROR_MESSAGE );
- // ensure database connection is closed
- tableModel.disconnectFromDatabase();
- System.exit( 1 ); // terminate application
- } // end inner catch
- } // end outer catch
- } // end actionPerformed
- } // end ActionListener inner class
- ); // end call to addActionListener
- setSize( 600, 300 ); // set window size
- setVisible( true ); // display window
- } // end try
- catch ( ClassNotFoundException classNotFound )
- {
- JOptionPane.showMessageDialog( null,
- "MySQL driver not found", "Driver not found",
- JOptionPane.ERROR_MESSAGE );
- isConnected = false;
- System.exit( 1 ); // terminate application
- } // end catch
- catch ( SQLException sqlException )
- {
- JOptionPane.showMessageDialog( null, sqlException.getMessage(),
- "Database error", JOptionPane.ERROR_MESSAGE );
- // ensure database connection is closed
- tableModel.disconnectFromDatabase();
- isConnected = false;
- System.exit( 1 ); // terminate application
- } // end catch
- // dispose of window when user quits application (this overrides
- // the default of HIDE_ON_CLOSE)
- setDefaultCloseOperation( DISPOSE_ON_CLOSE );
- // ensure database connection is closed when user quits application
- addWindowListener(new WindowAdapter()
- {
- // disconnect from database and exit when window has closed
- public void windowClosed( WindowEvent event )
- {
- tableModel.disconnectFromDatabase();
- System.exit( 0 );
- } // end method windowClosed
- } // end WindowAdapter inner class
- ); // end call to addWindowListener
- } // end DisplayQueryResults constructor
- // execute application
- public static void main( String args[] )
- {
- new DisplayQueryResults();
- } // end main
- } // end class DisplayQueryResults
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement