Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Window extends JFrame {
- MyPanel panel = new MyPanel();
- JTextField textField;
- public Window() {
- setTitle("Магический квадрат");
- setContentPane(panel);
- panel.setLayout(null);
- JLabel lblNewLabel = new JLabel("Магический квадрат отгадает Ваши мечты!");
- lblNewLabel.setBounds(46, 23, 342, 23);
- panel.add(lblNewLabel);
- JLabel label1 = new JLabel("Подумайте о том, что Вы хотите...");
- label1.setFont(new Font("Tahoma", Font.PLAIN, 17));
- label1.setBounds(46, 365, 342, 23);
- panel.add(label1);
- JButton btnNewButton = new JButton("Узнать");
- btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
- btnNewButton.setBounds(304, 399, 134, 23);
- btnNewButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Connection connect;
- try
- {
- String driverName = "com.mysql.jdbc.Driver";
- Class.forName(driverName);
- String serverName = "localhost";
- String mybase = "game";
- String url = "jdbc:mysql://"+serverName+"/"+mybase;
- //Имя пользователя
- String username = "root";
- String password = "";
- connect = DriverManager.getConnection(url,username,password);
- int rez = (int)(Math.random()*21)+1;
- String query = "Select * from tabl where (id="+rez+")";
- //Создаем запрос
- Statement stmt = connect.createStatement();
- //Выполним запрос
- ResultSet rs = stmt.executeQuery(query);
- String temp;
- while (rs.next()) {
- temp = rs.getString("text");
- textField.setText(temp);
- }
- connect.close();
- }
- catch (Exception ex) {
- System.out.println("error: " + ex.getMessage());
- }
- }
- });
- panel.add(btnNewButton);
- textField = new JTextField();
- textField.setBounds(23, 437, 457, 66);
- panel.add(textField);
- textField.setColumns(10);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(0,0, 800, 600);
- }
- public static void main(String... args) {
- Window wnd = new Window();
- wnd.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement