Advertisement
vgoncharov

ref_m2_13_idea_Window

Dec 7th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. public class Window extends JFrame {
  11.  
  12.     MyPanel panel = new MyPanel();
  13.     JTextField textField;
  14.  
  15.     public Window() {
  16.  
  17.         setTitle("Магический квадрат");
  18.  
  19.         setContentPane(panel);
  20.         panel.setLayout(null);
  21.  
  22.         JLabel lblNewLabel = new JLabel("Магический квадрат отгадает Ваши мечты!");
  23.         lblNewLabel.setBounds(46, 23, 342, 23);
  24.         panel.add(lblNewLabel);
  25.  
  26.         JLabel label1 = new JLabel("Подумайте о том, что Вы хотите...");
  27.         label1.setFont(new Font("Tahoma", Font.PLAIN, 17));
  28.         label1.setBounds(46, 365, 342, 23);
  29.         panel.add(label1);
  30.  
  31.         JButton btnNewButton = new JButton("Узнать");
  32.         btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
  33.         btnNewButton.setBounds(304, 399, 134, 23);
  34.  
  35.         btnNewButton.addActionListener(new ActionListener() {
  36.             @Override
  37.             public void actionPerformed(ActionEvent e) {
  38.  
  39.                Connection connect;
  40.  
  41.                try
  42.                {
  43.                     String driverName = "com.mysql.jdbc.Driver";
  44.                     Class.forName(driverName);
  45.  
  46.                     String serverName = "localhost";
  47.                     String mybase = "game";
  48.                     String url = "jdbc:mysql://"+serverName+"/"+mybase;
  49.  
  50.                     //Имя пользователя
  51.                     String username = "root";
  52.                     String password = "";
  53.  
  54.                     connect = DriverManager.getConnection(url,username,password);
  55.  
  56.  
  57.                     int rez = (int)(Math.random()*21)+1;
  58.  
  59.                     String query = "Select * from tabl where (id="+rez+")";
  60.  
  61.                     //Создаем запрос
  62.                     Statement stmt = connect.createStatement();
  63.                     //Выполним запрос
  64.                     ResultSet rs = stmt.executeQuery(query);
  65.  
  66.                     String temp;
  67.                     while (rs.next()) {
  68.                        temp = rs.getString("text");
  69.                        textField.setText(temp);
  70.                     }
  71.                    connect.close();
  72.                }
  73.                catch (Exception ex) {
  74.                    System.out.println("error: " + ex.getMessage());
  75.                }
  76.             }
  77.         });
  78.  
  79.  
  80.  
  81.         panel.add(btnNewButton);
  82.  
  83.         textField = new JTextField();
  84.         textField.setBounds(23, 437, 457, 66);
  85.         panel.add(textField);
  86.         textField.setColumns(10);
  87.  
  88.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89.         setBounds(0,0, 800, 600);
  90.     }
  91.  
  92.     public static void main(String... args) {
  93.         Window wnd = new Window();
  94.         wnd.setVisible(true);
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement