Advertisement
Guest User

Untitled

a guest
Dec 26th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package lesson24;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import java.awt.Font;
  11. import javax.swing.JButton;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import javax.swing.JTextField;
  15.  
  16. import java.sql.*;
  17.  
  18. public class prog extends JFrame {
  19.  
  20.     private panel contentPane;
  21.     private JTextField textField;
  22.  
  23.     /**
  24.      * Launch the application.
  25.      */
  26.     public static void main(String[] args) {
  27.         EventQueue.invokeLater(new Runnable() {
  28.             public void run() {
  29.                 try {
  30.                     prog frame = new prog();
  31.                     frame.setVisible(true);
  32.                 } catch (Exception e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.         });
  37.     }
  38.  
  39.     /**
  40.      * Create the frame.
  41.      */
  42.     public prog() {
  43.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.         setBounds(100, 100, 514, 556);
  45.         contentPane = new panel();
  46.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  47.         setContentPane(contentPane);
  48.         contentPane.setLayout(null);
  49.        
  50.         JButton btnNewButton = new JButton("\u0423\u0417\u041D\u0410\u0422\u042C!!!");
  51.         btnNewButton.addActionListener(new ActionListener() {
  52.             public void actionPerformed(ActionEvent arg0) {
  53.                 Connection connect;
  54.                
  55.                 try
  56.                 {
  57.                     //переменные для подключения
  58.                     String driverName = "com.mysql.jdbc.Driver";
  59.                     Class.forName(driverName);
  60.                    
  61.                     String serverName = "localhost";
  62.                     String mybase = "game";
  63.                     String url_ = "jdbc:mysql://" + serverName + "/" + mybase;
  64.                
  65.                     String userName = "root";
  66.                     String password = "";
  67.                    
  68.                     //устанавливаем подключение
  69.                     connect = DriverManager.getConnection(url_, userName, password);
  70.                     //рандом
  71.                     int rez = (int)(Math.random()*21)+1;
  72.                     String query = "SELECT * FROM tabl WHERE (id=" + rez + ")";
  73.                    
  74.                     Statement stmt = connect.createStatement();
  75.                    
  76.                     ResultSet rs = stmt.executeQuery(query);
  77.                    
  78.                     String temp;
  79.                     while(rs.next())
  80.                     {
  81.                         temp = rs.getString("text");
  82.                         textField.setText(temp);
  83.                     }
  84.                     connect.close();
  85.                 }
  86.                 catch (Exception e) {
  87.                     // TODO: handle exception
  88.                 }
  89.             }
  90.         });
  91.         btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 17));
  92.         btnNewButton.setBounds(249, 394, 179, 53);
  93.         contentPane.add(btnNewButton);
  94.        
  95.         textField = new JTextField();
  96.         textField.setFont(new Font("Tahoma", Font.PLAIN, 16));
  97.         textField.setBounds(35, 457, 420, 38);
  98.         contentPane.add(textField);
  99.         textField.setColumns(10);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement