Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
- /**
- * Created by svgood on 07.06.17.
- */
- public class Main {
- public static Connection connection;
- public static Statement stmt = null;
- public static ResultSet rs = null;
- static Boolean found = false;
- static int cur_item = 0;
- public static void main (String [] args){
- String url = "jdbc:mysql://localhost:3306/med"; //Я хуй знает, надо ли туту что-то менять
- String username = "root";
- String password = "Svgood22";
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- try {
- connection = DriverManager.getConnection(url, username, password);
- System.out.println("Connected");
- } catch (SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- JFrame frame;
- JPanel panel;
- JList list ;
- JTextField field1 ;
- JTextField field2 ;
- JButton btn_find ;
- JButton btn_last ;
- JButton btn_next ;
- JTextField f1 = new JTextField(10);
- JTextField f2 = new JTextField(10);
- JTextField f3 = new JTextField(10);
- JTextField f4 = new JTextField(10);
- JTextField f5 = new JTextField(10);
- DefaultListModel model = new DefaultListModel();
- for (int i = 0; i < 3; i++)
- model.addElement("Растение номер " + (i+1));
- frame = new JFrame("DataBase");
- panel = new JPanel();
- list = new JList(model);
- field1 = new JTextField(15);
- field2 = new JTextField(15);
- btn_find = new JButton("Найти");
- btn_last = new JButton("<<");
- btn_next = new JButton(">>");
- panel.setLayout(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- Insets insets = new Insets(10,10,10,10);
- c.insets = insets;
- c.gridx = 0;
- c.gridy = 0;
- c.gridheight = 3;
- c.gridwidth = 3;
- c.ipadx = 300;
- c.ipady = 200;
- panel.add(list, c);
- c.gridwidth = 2;
- c.ipadx = 0;
- c.ipady = 0;
- c.gridheight = 1;
- c.gridx = 3;
- panel.add(field1, c);
- c.gridy = 1;
- panel.add(field2, c);
- c.gridy = 2;
- panel.add(btn_find,c);
- c.gridx = 0;
- c.gridy = 3;
- c.gridwidth = 1;
- panel.add(f1, c);
- c.gridx = 1;
- panel.add(f2, c);
- c.gridx = 2;
- panel.add(f3, c);
- c.gridx = 3;
- panel.add(f4, c);
- c.gridx = 4;
- panel.add(f5, c);
- c.gridwidth = 3;
- c.anchor = GridBagConstraints.EAST;
- c.gridy = 4;
- c.gridx = 0;
- panel.add(btn_last, c);
- c.anchor = GridBagConstraints.WEST;
- c.gridx = 3;
- panel.add(btn_next, c);
- btn_last.setVisible(false);
- btn_next.setVisible(false);
- f1.setVisible(false);
- f2.setVisible(false);
- f3.setVisible(false);
- f4.setVisible(false);
- f5.setVisible(false);
- list.addListSelectionListener(new ListSelectionListener() {
- @Override
- public void valueChanged(ListSelectionEvent e) {
- try {
- String text;
- String exp;
- stmt = connection.createStatement();
- rs = stmt.executeQuery("SELECT PlantName, PlantQuantity FROM Plant WHERE PID = " + (list.getSelectedIndex() + 1));
- while (rs.next()) {
- text = rs.getString("PlantName");
- exp = rs.getString("PlantQuantity");
- field1.setText(text);
- field2.setText(exp);
- }
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- }
- });
- btn_find.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFrame frame1 = new JFrame("Поиск");
- JPanel panel1 = new JPanel();
- panel1.setLayout(new GridBagLayout());
- frame1.setMinimumSize(new Dimension(300, 200));
- frame1.setResizable(false);
- JLabel label = new JLabel("Введите семейство растений, что хотите найти!");
- JButton btn_ok = new JButton("Искать");
- JTextField txtf_category = new JTextField(10);
- GridBagConstraints c1 = new GridBagConstraints();
- Insets insets1 = new Insets(10,10,10,10);
- c1.insets = insets1;
- c1.gridx = 0;
- c1.gridy = 0;
- panel1.add(label, c1);
- c1.gridy = 1;
- panel1.add(txtf_category, c1);
- c1.gridy = 2;
- panel1.add(btn_ok, c1);
- frame1.add(panel1);
- frame1.pack();
- frame1.setVisible(true);
- btn_ok.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (!txtf_category.equals("")){
- try {
- String text;
- String exp;
- stmt = connection.createStatement();
- rs = null;
- rs = stmt.executeQuery("SELECT * FROM Plant WHERE PlantType = " + txtf_category.getText());
- if (rs.next()){
- found = true;
- f1.setText(rs.getString("PID"));
- f2.setText(rs.getString("PlantName"));
- f3.setText(rs.getString("PlantType"));
- f4.setText(rs.getString("PlantPrice"));
- f5.setText(rs.getString("PlantQuantity"));
- cur_item = Integer.parseInt(f1.getText());
- }
- else {
- txtf_category.setText("Отсутсвует");
- }
- }
- catch (SQLException ex){
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) { } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) { } // ignore
- stmt = null;
- }
- }
- if (found) {
- btn_next.setVisible(true);
- btn_last.setVisible(true);
- f1.setVisible(true);
- f2.setVisible(true);
- f3.setVisible(true);
- f4.setVisible(true);
- f5.setVisible(true);
- }
- }
- }
- });
- }
- });
- btn_next.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (cur_item < 3) {
- try {
- cur_item+= 1;
- stmt = connection.createStatement();
- rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
- if (rs.next()) {
- f1.setText(rs.getString("PID"));
- f2.setText(rs.getString("PlantName"));
- f3.setText(rs.getString("PlantType"));
- f4.setText(rs.getString("PlantPrice"));
- f5.setText(rs.getString("PlantQuantity"));
- }
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- }
- }
- });
- btn_last.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (cur_item > 1) {
- try {
- cur_item -= 1;
- stmt = connection.createStatement();
- rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
- if (rs.next()) {
- f1.setText(rs.getString("PID"));
- f2.setText(rs.getString("PlantName"));
- f3.setText(rs.getString("PlantType"));
- f4.setText(rs.getString("PlantPrice"));
- f5.setText(rs.getString("PlantQuantity"));
- }
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- }
- }
- });
- frame.setMinimumSize(new Dimension(800, 600));
- frame.setResizable(false);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.add(panel);
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement