Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import java.awt.BorderLayout;
- import java.awt.event.*;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import java.sql.*;
- public class DatabaseViewer extends JFrame implements ActionListener, MouseMotionListener,KeyListener {
- public void Połączenie()
- {
- Connection connection = null;
- String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=Northwind;user=Baza_Testowa;password=Testbazy123;";
- try {
- connection = DriverManager.getConnection(connectionString);
- String SQL = Polecenie.getText();
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery(SQL);
- String temp=new String();
- ResultSetMetaData rsmd=rs.getMetaData();
- int liczba_kolumn=rsmd.getColumnCount();
- while (rs.next()) {
- for (int i=1;i<=liczba_kolumn;i++)
- {
- if(i!=liczba_kolumn)
- {
- temp+=rs.getString(i)+" ";
- }
- if(i==liczba_kolumn)
- {
- temp+=rs.getString(i)+"\n";
- }
- }
- }
- Tekst.setText(temp);
- }
- catch (SQLException e) {
- JOptionPane.showConfirmDialog(this,e.getMessage(),"Error", JOptionPane.OK_OPTION);
- }
- finally {
- if (connection != null) try { connection.close(); } catch(Exception e) {}
- }
- }
- JButton Wyczysc;
- JButton Wykonaj;
- JTextArea Tekst;
- JLabel Pozycja;
- JScrollPane Pasek;
- int mX,mY;
- JTextField Polecenie;
- public DatabaseViewer() {
- super("Hello World");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);}
- public void initComponents ()
- {
- Pozycja=new JLabel();
- Pozycja.setSize(200, 150);
- Pozycja.setText("Tekst");
- this.getContentPane().add(BorderLayout.NORTH, Pozycja);
- Wyczysc = new JButton("Wyczyść");
- Wyczysc.setSize(200, 150);
- this.getContentPane().add(BorderLayout.WEST, Wyczysc);
- Wyczysc.addActionListener(this);
- Wykonaj = new JButton("Wykonaj");
- Wykonaj.setSize(200,150);
- Wykonaj.addActionListener(this);
- this.getContentPane().add(BorderLayout.EAST, Wykonaj);
- Tekst = new JTextArea();
- this.getContentPane().add(BorderLayout.CENTER, Tekst);
- Tekst.setEditable(false);
- Polecenie=new JTextField();
- this.getContentPane().add(BorderLayout.SOUTH, Polecenie);
- addMouseMotionListener(this);
- Polecenie.addActionListener(this);
- Polecenie.addKeyListener(this);
- Wyczysc.addMouseMotionListener(this);
- Wykonaj.addMouseMotionListener(this);
- Tekst.addMouseMotionListener(this);
- Pozycja.addMouseMotionListener(this);
- }
- public void actionPerformed(ActionEvent akcja) {
- if( akcja.getSource()==Wyczysc )
- {
- Tekst.setText("");
- Polecenie.setText("");
- }
- if( akcja.getSource()==Wykonaj )
- {
- Połączenie();
- }
- }
- @Override
- public void mouseMoved(MouseEvent me) {
- mX = (int) me.getPoint().getX();
- mY = (int) me.getPoint().getY();
- Pozycja.setText("Współrzędne myszy: x="+mX+" y="+mY+" .");
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_ENTER)
- {
- Połączenie();
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement