Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lesson24;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import java.awt.Font;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextField;
- import java.sql.*;
- public class prog extends JFrame {
- private panel contentPane;
- private JTextField textField;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- prog frame = new prog();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public prog() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 514, 556);
- contentPane = new panel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JButton btnNewButton = new JButton("\u0423\u0417\u041D\u0410\u0422\u042C!!!");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- 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 e) {
- // TODO: handle exception
- }
- }
- });
- btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 17));
- btnNewButton.setBounds(249, 394, 179, 53);
- contentPane.add(btnNewButton);
- textField = new JTextField();
- textField.setFont(new Font("Tahoma", Font.PLAIN, 16));
- textField.setBounds(35, 457, 420, 38);
- contentPane.add(textField);
- textField.setColumns(10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement