Advertisement
far_light

Action

Mar 17th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         JFrame frame = new JFrame();
  9.         frame.setBounds(128, 128, 800, 600);
  10.         frame.setTitle("Hello");
  11.         frame.setVisible(true);
  12.  
  13.         frame.setFocusable(true);
  14.         frame.getContentPane().add(new Panel());
  15.     }
  16.  
  17.     public void action() {
  18.         Connection connect;
  19.  
  20.         try {
  21.             Class.forName("com.mysql.jdbc.Driver");
  22.  
  23.             String serverName = "localhost",
  24.                     databaseName = "it-learning",
  25.                     url  = "jdbc:mysql://" + serverName + "/" + databaseName,
  26.                     user = "root",
  27.                     password = "";
  28.  
  29.             connect = DriverManager.getConnection(url, user, password);
  30.             int id = (int)(Math.random() * 21) + 1;
  31.             String query = "SELECT * FROM table WHERE id = " + id;
  32.             Statement statement = connect.createStatement();
  33.             ResultSet set = statement.executeQuery(query);
  34.  
  35.             while (set.next())
  36.                 textField.setText(set.getString("text"));
  37.  
  38.             connect.close();
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement