Advertisement
Guest User

1

a guest
May 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JTextPane;
  8. import java.awt.Color;
  9. import javax.swing.JButton;
  10. import javax.swing.JTextField;
  11. import java.awt.event.ActionListener;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.Statement;
  16. import java.awt.event.ActionEvent;
  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. setTitle("\u0418\u0433\u0440\u0430 - \u041F\u0440\u0435\u0434\u0441\u043A\u0430\u0437\u0430\u043D\u0438\u0435.");
  44. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. setBounds(100, 100, 532, 513);
  46. contentPane = new Panel();
  47. contentPane.setBackground(Color.RED);
  48. contentPane.setForeground(Color.RED);
  49. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  50. setContentPane(contentPane);
  51. contentPane.setLayout(null);
  52.  
  53. JButton button = new JButton("\u0423\u0437\u043D\u0430\u0442\u044C");
  54. button.addActionListener(new ActionListener() {
  55. public void actionPerformed(ActionEvent arg0) {
  56.  
  57. Connection con;
  58. try{
  59. String driverName = "com.mysql.jdbc.Driver";
  60. Class.forName(driverName);
  61. String serverName = "localhost";
  62. String mybase = "game";
  63. String url_="jdbc:mysql://"+serverName+"/"+mybase;
  64. String username = "root";
  65. String password = "";
  66. con = DriverManager.getConnection(url_, username, password);
  67. int rez = (int)(Math.random()*5)+1;
  68. String query = "SELECT * FROM game00 WHERE (id="+rez+")";
  69.  
  70. Statement stmt = con.createStatement();
  71. ResultSet rs = stmt.executeQuery(query);
  72. String temp;
  73. while(rs.next()){
  74. temp = rs.getString("text");
  75. textField.setText(temp);
  76. }
  77. con.close();
  78. }
  79. catch(Exception exp){}
  80. }});
  81.  
  82. button.setBounds(61, 202, 89, 23);
  83. contentPane.add(button);
  84.  
  85. textField = new JTextField();
  86. textField.setBounds(0, 11, 415, 187);
  87. contentPane.add(textField);
  88. textField.setColumns(10);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement