Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* mojmodeljava */
- package pl.polsl.bd;
- import java.util.Vector;
- import javax.swing.table.AbstractTableModel;
- public class MojModel extends AbstractTableModel {
- Vector data;
- Vector cols;
- public MojModel(Vector p_cols)
- {
- cols = p_cols;
- data = new Vector();
- }
- public void addRow(Vector row)
- {
- data.add(row);
- }
- public int getRowCount()
- {
- return data.size();
- }
- public int getColumnCount()
- {
- return cols.size();
- }
- public Object getValueAt(int nRow, int nCol)
- {
- return ((Vector)data.get(nRow)).get(nCol);
- }
- }
- /*GUI mysql connection*/
- package pl.polsl.bd;
- import java.awt.BorderLayout;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- import javax.swing.JSplitPane;
- import javax.swing.JToolBar;
- import java.awt.Dimension;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JTextArea;
- import javax.swing.JButton;
- import java.sql.*;
- import java.util.Vector;
- import javax.swing.JTable;
- public class MyWindow extends JFrame {
- private static final long serialVersionUID = 1L;
- private JPanel jContentPane = null; // @jve:decl-index=0:visual-constraint="10,10"
- private JSplitPane jSplitPane = null;
- private JToolBar jJToolBarBar = null;
- private JTextArea jTextArea = null;
- private JButton jButton = null;
- private JButton jButton1 = null;
- /* sql connection */
- private Connection con = null;
- private JTable jTable = null;
- /**
- * This is the default constructor
- */
- public MyWindow() {
- super();
- initialize();
- }
- /**
- * This method initializes this
- *
- * @return void
- */
- private void initialize() {
- this.setSize(923, 311);
- this.setContentPane(getJContentPane());
- this.setTitle("JFrame");
- this.jButton.setEnabled(false);
- }
- /**
- * This method initializes jContentPane
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJContentPane() {
- if (jContentPane == null) {
- jContentPane = new JPanel();
- jContentPane.setLayout(new BorderLayout());
- jContentPane.setSize(new Dimension(515, 169));
- jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
- jContentPane.add(getJJToolBarBar(), BorderLayout.NORTH);
- }
- return jContentPane;
- }
- /**
- * This method initializes jSplitPane
- *
- * @return javax.swing.JSplitPane
- */
- private JSplitPane getJSplitPane() {
- if (jSplitPane == null) {
- jSplitPane = new JSplitPane();
- jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
- jSplitPane.setBottomComponent(getJTable());
- jSplitPane.setTopComponent(getJTextArea());
- }
- return jSplitPane;
- }
- /**
- * This method initializes jJToolBarBar
- *
- * @return javax.swing.JToolBar
- */
- private JToolBar getJJToolBarBar() {
- if (jJToolBarBar == null) {
- jJToolBarBar = new JToolBar();
- jJToolBarBar.setPreferredSize(new Dimension(18, 50));
- jJToolBarBar.add(getJButton());
- jJToolBarBar.add(getJButton1());
- }
- return jJToolBarBar;
- }
- /**
- * This method initializes jTextArea
- *
- * @return javax.swing.JTextArea
- */
- private JTextArea getJTextArea() {
- if (jTextArea == null) {
- jTextArea = new JTextArea();
- jTextArea.setText("select * from pracownicy");
- }
- return jTextArea;
- }
- /**
- * This method initializes jButton
- *
- * @return javax.swing.JButton
- */
- private JButton getJButton() {
- if (jButton == null) {
- jButton = new JButton();
- jButton.setText("uruchom");
- jButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- // TODO Auto-generated Event stub actionPerformed()
- String query = jTextArea.getText();
- Vector cols = new Vector();
- try
- {
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- int noOfCols = rs.getMetaData().getColumnCount();
- for(int i=1; i < noOfCols; i++) {
- cols.add(i);
- }
- MojModel md = new MojModel(cols);
- while(rs.next()) {
- Vector row = new Vector();
- for(int i=1; i < noOfCols; i++) {
- row.add(rs.getString(i));
- }
- md.addRow(row);
- }
- // Binduj do tableki
- jTable.setModel(md);
- }
- catch(SQLException ec)
- {
- JOptionPane.showMessageDialog(null, ec.getMessage());
- }
- }
- });
- }
- return jButton;
- }
- /**
- * This method initializes jButton1
- *
- * @return javax.swing.JButton
- */
- private JButton getJButton1() {
- if (jButton1 == null) {
- jButton1 = new JButton();
- jButton1.setText("polacz");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- // Polacz/rozlacz EventHandler
- public void actionPerformed(java.awt.event.ActionEvent e) {
- try
- {
- if(con == null)
- {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:6033/uczelnia","root","");
- jButton1.setText("Rozlacz");
- jButton.setEnabled(true);
- }
- else {
- con.close();
- con = null; // ;-)
- jButton1.setText("Połącz");
- jButton.setEnabled(false);
- }
- }
- catch(SQLException ec)
- {
- JOptionPane.showMessageDialog(null, ec.getMessage());
- }
- catch(ClassNotFoundException ex)
- {
- JOptionPane.showMessageDialog(null, "Brak klasy com.mysql.jdbc.Driver!");
- }
- }
- });
- }
- return jButton1;
- }
- /**
- * This method initializes jTable
- *
- * @return javax.swing.JTable
- */
- private JTable getJTable() {
- if (jTable == null) {
- jTable = new JTable();
- }
- return jTable;
- }
- } // @jve:decl-index=0:visual-constraint="10,10"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement