Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  2.  
  3. import javax.sql.DataSource;
  4. import javax.swing.*;
  5. import javax.swing.table.DefaultTableModel;
  6. import javax.xml.transform.Result;
  7. import java.sql.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.util.Vector;
  11.  
  12. import static javax.swing.JOptionPane.WARNING_MESSAGE;
  13.  
  14. public class App {
  15.     private JPanel pnlMain;
  16.     private JComboBox dropDriver;
  17.     private JComboBox dropDatabase;
  18.     private JTextField txtUsername;
  19.     private JTextField txtPassword;
  20.     private JTextArea txtASQLStatement;
  21.     private JLabel lblDriver;
  22.     private JLabel lblDatabase;
  23.     private JLabel lblUsername;
  24.     private JLabel lblPassword;
  25.     private JLabel lblDatabaseInfo;
  26.     private JLabel lblSQLCommand;
  27.     private JTextArea txtAConnectionStatus;
  28.     private JButton btnConnect;
  29.     private JButton btnExecute;
  30.     private JButton btnClearSQLCommand;
  31.     private JButton btnClearTable;
  32.     private JScrollPane scrlExecution;
  33.     private JTable tblOutput;
  34.  
  35.     public Connection connection;
  36.     public ResultSet rs;
  37.     public boolean connectedToDatabase = false;
  38.  
  39.     public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
  40.         ResultSetMetaData metaData = rs.getMetaData();
  41.  
  42.         // names of columns
  43.         Vector<String> columnNames = new Vector<String>();
  44.         int columnCount = metaData.getColumnCount();
  45.         for (int column = 1; column <= columnCount; column++) {
  46.             columnNames.add(metaData.getColumnName(column));
  47.         }
  48.  
  49.         // data of the table
  50.         Vector<Vector<Object>> data = new Vector<Vector<Object>>();
  51.         while (rs.next()) {
  52.             Vector<Object> vector = new Vector<Object>();
  53.             for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
  54.                 vector.add(rs.getObject(columnIndex));
  55.             }
  56.             data.add(vector);
  57.         }
  58.  
  59.         return new DefaultTableModel(data, columnNames);
  60.     }
  61.  
  62.     public App() {
  63.         // Create Listener for Clear SQL button
  64.         btnClearSQLCommand.addActionListener(new ActionListener() {
  65.             @Override
  66.             public void actionPerformed(ActionEvent e) {
  67.                 txtASQLStatement.setText("");
  68.             }
  69.         });
  70.  
  71.         // Create Listener for Clear Table button
  72.         btnClearTable.addActionListener(new ActionListener() {
  73.             @Override
  74.             public void actionPerformed(ActionEvent e) {
  75.                 scrlExecution.removeAll();
  76.             }
  77.         });
  78.  
  79.         // Create Listener for Connect button
  80.         // Load JDBC Driver
  81.         // Establish Connection
  82.         btnConnect.addActionListener(new ActionListener() {
  83.             @Override
  84.             public void actionPerformed(ActionEvent e) {
  85.                 // Get username string
  86.                 String username = txtUsername.getText();
  87.                 // Get password string
  88.                 String password = txtPassword.getText();
  89.                 // Get database string
  90.                 String database = (String)dropDatabase.getSelectedItem();
  91.  
  92.                 // Set username, password, database
  93.                 MysqlDataSource dataSource = new MysqlDataSource();
  94.                 dataSource.setUser(username);
  95.                 dataSource.setPassword(password);
  96.                 dataSource.setURL(database);
  97.  
  98.                 try {
  99.                     // Disable Connect button, drop-downs and text fields upon successful connection
  100.                     // Otherwise, throw an exception
  101.                     connection = dataSource.getConnection();
  102.                     btnConnect.setEnabled(false);
  103.                     dropDatabase.setEnabled(false);
  104.                     dropDriver.setEnabled(false);
  105.                     txtUsername.setEditable(false);
  106.                     txtPassword.setEditable(false);
  107.                     connectedToDatabase = true;
  108.                     JOptionPane.showMessageDialog(null, "Database connected!");
  109.                     txtAConnectionStatus.setText(String.format("Connected to %s", database));
  110.                 } catch (SQLException sql) {
  111.                     JOptionPane.showMessageDialog(null,"Error establishing connection",
  112.                             "Error", WARNING_MESSAGE);
  113.                     sql.printStackTrace();
  114.                 }
  115.  
  116.             }
  117.         });
  118.  
  119.         // Create Listener for Execute button
  120.         btnExecute.addActionListener(new ActionListener() {
  121.             @Override
  122.             public void actionPerformed(ActionEvent e) throws IllegalStateException {
  123.                 // Check if connected to the database
  124.                 if (!connectedToDatabase) {
  125.                     JOptionPane.showMessageDialog(null, "Not connected to database.  Please " +
  126.                             "connect and try again", "Error", WARNING_MESSAGE);
  127.                     throw new IllegalStateException("Not connected to database");
  128.                 }
  129.  
  130.                 // Set the query to the text in the text area
  131.                 String query = txtASQLStatement.getText();
  132.  
  133.                 try {
  134.                     // Create the statement
  135.                     Statement statement = connection.createStatement();
  136.                     // Execute the query
  137.                     rs = statement.executeQuery(query);
  138.                     JTable table = new JTable(buildTableModel(rs));
  139.                     JOptionPane.showMessageDialog(null, new JScrollPane(table));
  140.                 } catch (SQLException sql) {
  141.                     JOptionPane.showMessageDialog(null, "Error with query", "Error",
  142.                             WARNING_MESSAGE);
  143.                     sql.printStackTrace();
  144.                 }
  145.  
  146.  
  147.             }
  148.         });
  149.     }
  150.  
  151.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  152.         JFrame frame = new JFrame("SQL Client GUI");
  153.         frame.setContentPane(new App().pnlMain);
  154.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  155.         frame.setSize(928, 600);
  156.         frame.setResizable(false);
  157.         //frame.pack();
  158.         frame.setVisible(true);
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement