Guest User

Untitled

a guest
May 25th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package jse;
  2.  
  3.  
  4. import java.awt.BorderLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.awt.event.MouseMotionListener;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20. import javax.swing.JButton;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JTextArea;
  26. import javax.swing.JTextField;
  27.  
  28.  
  29. public class MainWindow {
  30.     public JFrame frame;
  31.     JButton butWypelnij;
  32.     JButton butCzysc;
  33.     JTextArea textArea;
  34.     JTextField textField;
  35.     JLabel label;
  36.     JPanel panel;
  37.     JPanel panelPodLabel;
  38.     JScrollPane scrollPane;
  39.     ButtonListener buttonListener;
  40.     MouseListenerr mouseListener;
  41.    
  42.     public MainWindow(){
  43.        
  44.         createButtons();
  45.         createTextField();
  46.         createTextArea();
  47.         createScrollPane();
  48.         createLabel();
  49.         createPanel();
  50.         createFrame();
  51.         buttonListener = new ButtonListener();
  52.         butCzysc.addActionListener(buttonListener);
  53.         butWypelnij.addActionListener(buttonListener);
  54.        
  55.         textField.addActionListener(new ActionListener() {
  56.            
  57.             @Override
  58.             public void actionPerformed(ActionEvent arg0) {
  59.                 String nazwa = textField.getText();
  60.                 getFromDB(nazwa);
  61.                
  62.             }
  63.         });
  64.        
  65.         mouseListener = new MouseListenerr();
  66.         textArea.addMouseMotionListener(mouseListener);
  67.        
  68.     }
  69.    
  70.     public void createFrame(){
  71.         frame = new JFrame();
  72.         frame.getContentPane().add(panel);
  73.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  74.         frame.setSize(800, 600);
  75.     }
  76.    
  77.     public void createButtons(){
  78.         butWypelnij = new JButton("Wypelnij");
  79.         butCzysc = new JButton("Czysc");
  80.     }
  81.    
  82.     public void createTextField(){
  83.         textField = new JTextField();
  84.     }
  85.    
  86.     private void createScrollPane(){
  87.         scrollPane = new JScrollPane(textArea);
  88.     }
  89.    
  90.     public void createTextArea(){
  91.         textArea = new JTextArea();
  92.     }
  93.    
  94.     public void createLabel(){
  95.         label = new JLabel();
  96.         label.setText("Label");
  97.         panelPodLabel = new JPanel();
  98.         panelPodLabel.add(label);
  99.     }
  100.    
  101.     public void createPanel(){
  102.         panel = new JPanel();
  103.         panel.setLayout(new BorderLayout());
  104.         panel.add(panelPodLabel,BorderLayout.NORTH);
  105.         panel.add(butWypelnij,BorderLayout.WEST);
  106.         panel.add(scrollPane,BorderLayout.CENTER);
  107.         panel.add(butCzysc,BorderLayout.EAST);
  108.         panel.add(textField,BorderLayout.SOUTH);
  109.     }
  110.  
  111.     private Connection getConnection(String username, String password) {
  112.         Connection connection = null;
  113.         try {
  114.             // Load the JDBC driver
  115.             String driverName = "oracle.jdbc.driver.OracleDriver";
  116.             Class.forName(driverName);
  117.  
  118.             // Create a connection to the database
  119.             String serverName = "127.0.0.1";
  120.             String portNumber = "1521";
  121.             String sid = "xe";
  122.             String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
  123.            
  124.             connection = DriverManager.getConnection(url, username, password);
  125.         } catch (ClassNotFoundException e) {
  126.         } catch (SQLException e) {
  127.         }
  128.         return connection;
  129.     }
  130.    
  131.  
  132.    
  133.     public void getFromDB(String nazwaWydzialu){
  134.         String sql_findWydzialByName = "SELECT * FROM WYDZIAL WHERE NAZWA = ?";
  135.         Connection conn = getConnection("Karolina", "bazydanych");
  136.         try {
  137.             PreparedStatement pstatement = conn.prepareStatement(sql_findWydzialByName);
  138.             pstatement.setString(1, nazwaWydzialu);
  139.             ResultSet rs = pstatement.executeQuery();
  140.             String result ="";
  141.             if(rs.next()){
  142.                 result += "Nazwa wydzialu: " + rs.getString("nazwa") + "\n";
  143.                 result += "Identyfikator: " + rs.getString("identyfikator") + "\n";
  144.                 result += "Ulica: " + rs.getString("ulica") + "\n";
  145.             }
  146.             textArea.setText(result);
  147.             rs.close();
  148.             pstatement.close();
  149.         } catch (SQLException e) {
  150.             e.printStackTrace();
  151.         }
  152.        
  153.     }
  154.    
  155.     public static void main(String[] args) {
  156.           java.awt.EventQueue.invokeLater(new Runnable() {
  157.  
  158.                 public void run() {
  159.                     new MainWindow().frame.setVisible(true);
  160.                 }
  161.             });
  162.  
  163.     }
  164.    
  165.     private class ButtonListener implements ActionListener{
  166.  
  167.         @Override
  168.         public void actionPerformed(ActionEvent e) {
  169.             if(e.getSource() == butCzysc){
  170.                 textArea.setText("");
  171.             }
  172.             else if(e.getSource() == butWypelnij){
  173.                 String nazwa = textField.getText();
  174.                 getFromDB(nazwa);
  175.             }
  176.            
  177.         }
  178.        
  179.     }
  180.    
  181.     private class MouseListenerr implements MouseMotionListener{
  182.  
  183.         @Override
  184.         public void mouseDragged(MouseEvent e) {
  185.             // TODO Auto-generated method stub
  186.            
  187.         }
  188.  
  189.         @Override
  190.         public void mouseMoved(MouseEvent mouseEvent) {
  191.             int x = mouseEvent.getX();
  192.             int y = mouseEvent.getY();
  193.             String text = "Pozycja myszy: x = " + x + " y = " + y;
  194.             label.setText(text);
  195.            
  196.         }
  197.        
  198.     }
  199.  
  200. }
Add Comment
Please, Sign In to add comment