Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.SpringLayout;
- /** guide for event buttons... and listbox */
- public class first extends Frame
- {
- //buttons what we using
- Button btnShowOnline, btnShowNpc, btnCalculator;
- //connection
- Connection con;
- Statement stmt;
- //text area
- TextArea txt;
- public first(String title)
- {
- try {
- //connect to mysql base
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://80.240.211.244/c6server";
- con = DriverManager.getConnection(url, "Ruzli", "slH59RUwt6711");
- stmt = con.createStatement();
- //drop IOException error
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- setTitle(title);
- //set window size
- setSize(200,100);
- //slots, rassortirovka
- FlowLayout fl = new FlowLayout();
- btnShowOnline = new Button("Show online");
- //razmer showOnline
- btnShowOnline.setPreferredSize(new Dimension(90, 25));
- btnShowNpc = new Button("Show npc");
- btnCalculator = new Button("Calculator");
- txt = new TextArea();
- //add buttons
- add(btnShowOnline);
- add(btnShowNpc);
- add(btnCalculator);
- //add text area
- add(txt);
- // rasshirit chtobi vse pomestilos
- pack();
- setLayout(fl);
- //otobrazit'
- setVisible(true);
- addWindowListener(new WindowAdapter()
- {
- //support for close main window
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- enum Direction
- {
- LEFT,
- RIGHT
- }
- String justifyStr(String s, int size, Direction d)
- {
- StringBuilder ret = new StringBuilder(s);
- for (int i = 0; i < size - s.length(); i++)
- {
- if (d == Direction.LEFT)
- {
- ret.append(' ');
- } else
- {
- ret.insert(0, ' ');
- }
- }
- return ret.toString();
- }
- // button 1 showOnline() ****************************************
- public void showOnline()
- {
- //Don't need ResultSet, due its use only for show result, for update use executeUpdate
- try {
- stmt.executeUpdate("UPDATE characters SET online='1' WHERE account_name = 'botname';");
- System.out.println("Starting query 3\n");
- ResultSet rs = stmt.executeQuery("SELECT char_name, online, level from characters ORDER BY online, level");
- while (rs.next()) {
- // get information from database row
- String char_name = rs.getString(1);
- int online = rs.getInt(2);
- int level = rs.getInt(3);
- //make table clean, from left we add space
- char_name = justifyStr(char_name, 20, Direction.LEFT);
- //print result
- System.out.printf("%s\t - (online %d) - (level %d)\n", char_name, online, level);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // button 2 showNpc() ****************************************
- public void showNpc()
- {
- try {
- ResultSet rs2 = stmt.executeQuery("SELECT name, id, idtemplate, serverSideName, level from npc ORDER BY id");
- // reading resultset line by line
- while (rs2.next()) {
- // get information from database row
- String name = rs2.getString(1);
- int id = rs2.getInt(2);
- int idt = rs2.getInt(3);
- int serverSideName = rs2.getInt(4);
- int level = rs2.getInt(5);
- // %d -- decimal number \n - newline \t - TABulator
- System.out.printf("%d(%d) - %s - (S.Name = %d) - (level %d)\n", id, idt, name, serverSideName, level);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //button 3 prepared for Calculator **********************************
- private static void Calculator()
- {
- JFrame frame = new JFrame("Calculator");
- //Exit program if you close second window of Calculator
- //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container contentPane = frame.getContentPane();
- SpringLayout layout = new SpringLayout();
- contentPane.setLayout(layout);
- Container button = frame.getContentPane();
- SpringLayout layout2 = new SpringLayout();
- button.setLayout(layout2);
- //JButton button2 = new JButton("Confirm");
- //JButton butt = new JButton("Confirm");
- button.add(new JButton("Confirm"));
- //Label(text area)
- //Create and add the components.
- JLabel chance = new JLabel("Chance: ");
- JLabel count = new JLabel("Count: ");
- JTextField ChanceTextField = new JTextField("Write here chance.", 25);
- JTextField CountTextField = new JTextField("Write here count.", 25);
- //add text for chance
- contentPane.add(chance);
- contentPane.add(ChanceTextField);
- //add text for count
- contentPane.add(count);
- contentPane.add(CountTextField);
- //resize panel for chance
- layout.putConstraint(SpringLayout.WEST, contentPane,145,SpringLayout.WEST, ChanceTextField);
- layout.putConstraint(SpringLayout.NORTH, contentPane,145,SpringLayout.NORTH, ChanceTextField);
- //resize panel for count
- layout.putConstraint(SpringLayout.WEST, contentPane,145,SpringLayout.WEST, CountTextField);
- layout.putConstraint(SpringLayout.NORTH, contentPane,145,SpringLayout.NORTH, CountTextField);
- //chance
- layout.putConstraint(SpringLayout.WEST, chance,5,SpringLayout.WEST, contentPane);
- layout.putConstraint(SpringLayout.NORTH, chance,5,SpringLayout.NORTH, contentPane);
- layout.putConstraint(SpringLayout.WEST, ChanceTextField,5,SpringLayout.EAST, chance);
- layout.putConstraint(SpringLayout.NORTH, ChanceTextField,5,SpringLayout.NORTH, contentPane);
- //count
- layout.putConstraint(SpringLayout.WEST, count,5,SpringLayout.WEST, contentPane);
- layout.putConstraint(SpringLayout.NORTH, count,30,SpringLayout.NORTH, contentPane);
- layout.putConstraint(SpringLayout.WEST, CountTextField,5,SpringLayout.EAST, chance);
- layout.putConstraint(SpringLayout.NORTH, CountTextField,30,SpringLayout.NORTH, contentPane);
- //button
- layout2.putConstraint(SpringLayout.WEST, button,60,SpringLayout.WEST, button);
- layout2.putConstraint(SpringLayout.NORTH, button,300,SpringLayout.NORTH, button);
- // rasshirit chtobi vse pomestilos
- frame.setLayout(layout);
- frame.pack();
- frame.setVisible(true);
- frame.setLocation(0, 200);
- frame.setSize(450, 150);
- button.setLocation(50, 50);
- }
- public boolean handleEvent(Event e) {
- //debug
- //System.out.println(e);
- if (e.id != 1001) return false;
- if (e.target == btnShowNpc) {
- showNpc();
- } else if (e.target == btnShowOnline) {
- showOnline();
- } else if (e.target == btnCalculator) {
- Calculator();
- }
- return true;
- }
- public static void main(String[] args)
- {
- new first("SQL manager");
- }
- }
Add Comment
Please, Sign In to add comment