Advertisement
m4ly

[BD2] Java

Oct 12th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. /* mojmodeljava */
  2. package pl.polsl.bd;
  3.  
  4. import java.util.Vector;
  5.  
  6. import javax.swing.table.AbstractTableModel;
  7.  
  8.  
  9.  
  10. public class MojModel extends AbstractTableModel {
  11.    
  12.     Vector data;
  13.     Vector cols;
  14.    
  15.     public MojModel(Vector p_cols)
  16.     {
  17.         cols = p_cols;
  18.         data = new Vector();
  19.     }
  20.    
  21.     public void addRow(Vector row)
  22.     {
  23.         data.add(row);
  24.     }
  25.     public int getRowCount()
  26.     {  
  27.         return data.size();
  28.     }
  29.    
  30.     public int getColumnCount()
  31.     {
  32.         return cols.size();
  33.     }
  34.    
  35.     public Object getValueAt(int nRow, int nCol)
  36.     {
  37.         return ((Vector)data.get(nRow)).get(nCol);
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. /*GUI mysql connection*/
  44.  
  45. package pl.polsl.bd;
  46.  
  47. import java.awt.BorderLayout;
  48.  
  49. import javax.swing.JOptionPane;
  50. import javax.swing.JPanel;
  51. import javax.swing.JFrame;
  52. import javax.swing.JSplitPane;
  53. import javax.swing.JToolBar;
  54. import java.awt.Dimension;
  55. import java.sql.Connection;
  56. import java.sql.DriverManager;
  57. import java.sql.ResultSet;
  58. import java.sql.SQLException;
  59. import java.sql.Statement;
  60.  
  61. import javax.swing.JTextArea;
  62. import javax.swing.JButton;
  63. import java.sql.*;
  64. import java.util.Vector;
  65.  
  66. import javax.swing.JTable;
  67.  
  68. public class MyWindow extends JFrame {
  69.  
  70.     private static final long serialVersionUID = 1L;
  71.     private JPanel jContentPane = null;  //  @jve:decl-index=0:visual-constraint="10,10"
  72.     private JSplitPane jSplitPane = null;
  73.     private JToolBar jJToolBarBar = null;
  74.     private JTextArea jTextArea = null;
  75.     private JButton jButton = null;
  76.     private JButton jButton1 = null;
  77.    
  78.     /* sql connection  */
  79.     private Connection con = null;
  80.     private JTable jTable = null;
  81.     /**
  82.      * This is the default constructor
  83.      */
  84.     public MyWindow() {
  85.         super();
  86.         initialize();
  87.     }
  88.  
  89.     /**
  90.      * This method initializes this
  91.      *
  92.      * @return void
  93.      */
  94.     private void initialize() {
  95.         this.setSize(923, 311);
  96.         this.setContentPane(getJContentPane());
  97.         this.setTitle("JFrame");
  98.         this.jButton.setEnabled(false);
  99.     }
  100.  
  101.     /**
  102.      * This method initializes jContentPane
  103.      *
  104.      * @return javax.swing.JPanel
  105.      */
  106.     private JPanel getJContentPane() {
  107.         if (jContentPane == null) {
  108.             jContentPane = new JPanel();
  109.             jContentPane.setLayout(new BorderLayout());
  110.             jContentPane.setSize(new Dimension(515, 169));
  111.             jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
  112.             jContentPane.add(getJJToolBarBar(), BorderLayout.NORTH);
  113.         }
  114.         return jContentPane;
  115.     }
  116.  
  117.     /**
  118.      * This method initializes jSplitPane  
  119.      *  
  120.      * @return javax.swing.JSplitPane  
  121.      */
  122.     private JSplitPane getJSplitPane() {
  123.         if (jSplitPane == null) {
  124.             jSplitPane = new JSplitPane();
  125.             jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  126.             jSplitPane.setBottomComponent(getJTable());
  127.             jSplitPane.setTopComponent(getJTextArea());
  128.         }
  129.         return jSplitPane;
  130.     }
  131.  
  132.     /**
  133.      * This method initializes jJToolBarBar
  134.      *  
  135.      * @return javax.swing.JToolBar
  136.      */
  137.     private JToolBar getJJToolBarBar() {
  138.         if (jJToolBarBar == null) {
  139.             jJToolBarBar = new JToolBar();
  140.             jJToolBarBar.setPreferredSize(new Dimension(18, 50));
  141.             jJToolBarBar.add(getJButton());
  142.             jJToolBarBar.add(getJButton1());
  143.         }
  144.         return jJToolBarBar;
  145.     }
  146.  
  147.     /**
  148.      * This method initializes jTextArea   
  149.      *  
  150.      * @return javax.swing.JTextArea   
  151.      */
  152.     private JTextArea getJTextArea() {
  153.         if (jTextArea == null) {
  154.             jTextArea = new JTextArea();
  155.             jTextArea.setText("select * from pracownicy");
  156.         }
  157.         return jTextArea;
  158.     }
  159.  
  160.     /**
  161.      * This method initializes jButton 
  162.      *  
  163.      * @return javax.swing.JButton 
  164.      */
  165.     private JButton getJButton() {
  166.         if (jButton == null) {
  167.             jButton = new JButton();
  168.             jButton.setText("uruchom");
  169.             jButton.addActionListener(new java.awt.event.ActionListener() {
  170.                 public void actionPerformed(java.awt.event.ActionEvent e) {
  171.                     // TODO Auto-generated Event stub actionPerformed()
  172.                     String query =  jTextArea.getText();
  173.                     Vector cols = new Vector();
  174.                     try
  175.                     {
  176.                    
  177.                     Statement stmt = con.createStatement();
  178.                     ResultSet rs = stmt.executeQuery(query);
  179.                
  180.                     int noOfCols = rs.getMetaData().getColumnCount();
  181.                     for(int i=1; i < noOfCols; i++) {
  182.                         cols.add(i);
  183.                     }
  184.                    
  185.                    
  186.                     MojModel md = new MojModel(cols);
  187.                
  188.                    
  189.                     while(rs.next()) {
  190.                         Vector row = new Vector();
  191.                         for(int i=1; i < noOfCols; i++) {
  192.                            
  193.                             row.add(rs.getString(i));
  194.                            
  195.                         }  
  196.                        
  197.                         md.addRow(row);
  198.                     }
  199.                    
  200.                     // Binduj do tableki
  201.                     jTable.setModel(md);
  202.                    
  203.                     }
  204.                     catch(SQLException ec)
  205.                     {
  206.                         JOptionPane.showMessageDialog(null, ec.getMessage());
  207.                     }
  208.                    
  209.  
  210.                 }
  211.             });
  212.         }
  213.         return jButton;
  214.     }
  215.    
  216.     /**
  217.      * This method initializes jButton1
  218.      *  
  219.      * @return javax.swing.JButton 
  220.      */
  221.     private JButton getJButton1() {
  222.         if (jButton1 == null) {
  223.             jButton1 = new JButton();
  224.             jButton1.setText("polacz");
  225.             jButton1.addActionListener(new java.awt.event.ActionListener() {
  226.                
  227.                 // Polacz/rozlacz EventHandler
  228.                 public void actionPerformed(java.awt.event.ActionEvent e) {
  229.                     try
  230.                     {
  231.                         if(con == null)
  232.                         {
  233.                         Class.forName("com.mysql.jdbc.Driver");
  234.                         con = DriverManager.getConnection("jdbc:mysql://localhost:6033/uczelnia","root","");
  235.                         jButton1.setText("Rozlacz");
  236.                         jButton.setEnabled(true);
  237.                         }
  238.                         else {
  239.                             con.close();
  240.                             con = null; // ;-)
  241.                             jButton1.setText("Połącz");
  242.                             jButton.setEnabled(false);
  243.                         }
  244.                        
  245.                     }
  246.                     catch(SQLException ec)
  247.                     {
  248.                         JOptionPane.showMessageDialog(null, ec.getMessage());
  249.                     }
  250.                     catch(ClassNotFoundException ex)
  251.                     {
  252.                         JOptionPane.showMessageDialog(null, "Brak klasy com.mysql.jdbc.Driver!");
  253.                        
  254.                     }
  255.                 }
  256.             });
  257.         }
  258.         return jButton1;
  259.     }
  260.  
  261.     /**
  262.      * This method initializes jTable  
  263.      *  
  264.      * @return javax.swing.JTable  
  265.      */
  266.     private JTable getJTable() {
  267.         if (jTable == null) {
  268.             jTable = new JTable();
  269.         }
  270.         return jTable;
  271.     }
  272.  
  273. }  //  @jve:decl-index=0:visual-constraint="10,10"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement