Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jse;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class MainWindow {
- public JFrame frame;
- JButton butWypelnij;
- JButton butCzysc;
- JTextArea textArea;
- JTextField textField;
- JLabel label;
- JPanel panel;
- JPanel panelPodLabel;
- JScrollPane scrollPane;
- ButtonListener buttonListener;
- MouseListenerr mouseListener;
- public MainWindow(){
- createButtons();
- createTextField();
- createTextArea();
- createScrollPane();
- createLabel();
- createPanel();
- createFrame();
- buttonListener = new ButtonListener();
- butCzysc.addActionListener(buttonListener);
- butWypelnij.addActionListener(buttonListener);
- textField.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- String nazwa = textField.getText();
- getFromDB(nazwa);
- }
- });
- mouseListener = new MouseListenerr();
- textArea.addMouseMotionListener(mouseListener);
- }
- public void createFrame(){
- frame = new JFrame();
- frame.getContentPane().add(panel);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(800, 600);
- }
- public void createButtons(){
- butWypelnij = new JButton("Wypelnij");
- butCzysc = new JButton("Czysc");
- }
- public void createTextField(){
- textField = new JTextField();
- }
- private void createScrollPane(){
- scrollPane = new JScrollPane(textArea);
- }
- public void createTextArea(){
- textArea = new JTextArea();
- }
- public void createLabel(){
- label = new JLabel();
- label.setText("Label");
- panelPodLabel = new JPanel();
- panelPodLabel.add(label);
- }
- public void createPanel(){
- panel = new JPanel();
- panel.setLayout(new BorderLayout());
- panel.add(panelPodLabel,BorderLayout.NORTH);
- panel.add(butWypelnij,BorderLayout.WEST);
- panel.add(scrollPane,BorderLayout.CENTER);
- panel.add(butCzysc,BorderLayout.EAST);
- panel.add(textField,BorderLayout.SOUTH);
- }
- private Connection getConnection(String username, String password) {
- Connection connection = null;
- try {
- // Load the JDBC driver
- String driverName = "oracle.jdbc.driver.OracleDriver";
- Class.forName(driverName);
- // Create a connection to the database
- String serverName = "127.0.0.1";
- String portNumber = "1521";
- String sid = "xe";
- String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
- connection = DriverManager.getConnection(url, username, password);
- } catch (ClassNotFoundException e) {
- } catch (SQLException e) {
- }
- return connection;
- }
- public void getFromDB(String nazwaWydzialu){
- String sql_findWydzialByName = "SELECT * FROM WYDZIAL WHERE NAZWA = ?";
- Connection conn = getConnection("Karolina", "bazydanych");
- try {
- PreparedStatement pstatement = conn.prepareStatement(sql_findWydzialByName);
- pstatement.setString(1, nazwaWydzialu);
- ResultSet rs = pstatement.executeQuery();
- String result ="";
- if(rs.next()){
- result += "Nazwa wydzialu: " + rs.getString("nazwa") + "\n";
- result += "Identyfikator: " + rs.getString("identyfikator") + "\n";
- result += "Ulica: " + rs.getString("ulica") + "\n";
- }
- textArea.setText(result);
- rs.close();
- pstatement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MainWindow().frame.setVisible(true);
- }
- });
- }
- private class ButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == butCzysc){
- textArea.setText("");
- }
- else if(e.getSource() == butWypelnij){
- String nazwa = textField.getText();
- getFromDB(nazwa);
- }
- }
- }
- private class MouseListenerr implements MouseMotionListener{
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseMoved(MouseEvent mouseEvent) {
- int x = mouseEvent.getX();
- int y = mouseEvent.getY();
- String text = "Pozycja myszy: x = " + x + " y = " + y;
- label.setText(text);
- }
- }
- }
Add Comment
Please, Sign In to add comment