Guest User

Untitled

a guest
Apr 22nd, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JTextField;
  14. import javax.swing.SpringLayout;
  15.  
  16. /** guide for event buttons... and listbox */
  17. public class first extends Frame
  18. {
  19. //buttons what we using
  20. Button btnShowOnline, btnShowNpc, btnCalculator;
  21. //connection
  22. Connection con;
  23. Statement stmt;
  24. //text area
  25. TextArea txt;
  26.  
  27. public first(String title)
  28. {
  29. try {
  30. //connect to mysql base
  31. Class.forName("com.mysql.jdbc.Driver");
  32.  
  33. String url = "jdbc:mysql://80.240.211.244/c6server";
  34. con = DriverManager.getConnection(url, "Ruzli", "slH59RUwt6711");
  35. stmt = con.createStatement();
  36. //drop IOException error
  37. } catch (ClassNotFoundException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44.  
  45. setTitle(title);
  46. setResizable(false);
  47. //set window size
  48. setSize(200,100);
  49. //slots, rassortirovka
  50. FlowLayout fl = new FlowLayout();
  51. btnShowOnline = new Button("Show online");
  52. //razmer showOnline
  53. btnShowOnline.setPreferredSize(new Dimension(90, 25));
  54. btnShowNpc = new Button("Show npc");
  55. btnCalculator = new Button("Calculator");
  56. txt = new TextArea();
  57. //add buttons
  58. add(btnShowOnline);
  59. add(btnShowNpc);
  60. add(btnCalculator);
  61. //add text area
  62. add(txt);
  63. // rasshirit chtobi vse pomestilos
  64. pack();
  65. setLayout(fl);
  66. //otobrazit'
  67. setVisible(true);
  68. addWindowListener(new WindowAdapter()
  69. {
  70. //support for close main window
  71. public void windowClosing(WindowEvent e)
  72. {
  73. System.exit(0);
  74. }
  75. });
  76. }
  77.  
  78. enum Direction
  79. {
  80. LEFT,
  81. RIGHT
  82. }
  83.  
  84. String justifyStr(String s, int size, Direction d)
  85. {
  86. StringBuilder ret = new StringBuilder(s);
  87. for (int i = 0; i < size - s.length(); i++)
  88. {
  89. if (d == Direction.LEFT)
  90. {
  91. ret.append(' ');
  92. } else
  93. {
  94. ret.insert(0, ' ');
  95. }
  96. }
  97. return ret.toString();
  98. }
  99. // button 1 showOnline() ****************************************
  100. public void showOnline()
  101. {
  102. //Don't need ResultSet, due its use only for show result, for update use executeUpdate
  103. try {
  104. stmt.executeUpdate("UPDATE characters SET online='1' WHERE account_name = 'botname';");
  105.  
  106. System.out.println("Starting query 3\n");
  107. ResultSet rs = stmt.executeQuery("SELECT char_name, online, level from characters ORDER BY online, level");
  108.  
  109. while (rs.next()) {
  110. // get information from database row
  111. String char_name = rs.getString(1);
  112. int online = rs.getInt(2);
  113. int level = rs.getInt(3);
  114. //make table clean, from left we add space
  115. char_name = justifyStr(char_name, 20, Direction.LEFT);
  116.  
  117. //print result
  118. System.out.printf("%s\t - (online %d) - (level %d)\n", char_name, online, level);
  119. }
  120. } catch (SQLException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. }
  125. // button 2 showNpc() ****************************************
  126. public void showNpc()
  127. {
  128. try {
  129. ResultSet rs2 = stmt.executeQuery("SELECT name, id, idtemplate, serverSideName, level from npc ORDER BY id");
  130.  
  131. // reading resultset line by line
  132. while (rs2.next()) {
  133. // get information from database row
  134. String name = rs2.getString(1);
  135. int id = rs2.getInt(2);
  136. int idt = rs2.getInt(3);
  137. int serverSideName = rs2.getInt(4);
  138. int level = rs2.getInt(5);
  139.  
  140. // %d -- decimal number \n - newline \t - TABulator
  141. System.out.printf("%d(%d) - %s - (S.Name = %d) - (level %d)\n", id, idt, name, serverSideName, level);
  142. }
  143. } catch (SQLException e) {
  144. // TODO Auto-generated catch block
  145. e.printStackTrace();
  146. }
  147. }
  148. //button 3 prepared for Calculator **********************************
  149. public static void Calculator()
  150. {
  151. JFrame frame = new JFrame("Calculator");
  152.  
  153. Container box = frame.getContentPane();
  154. BoxLayout layout = new BoxLayout(frame, 0);
  155. box.setLayout(layout);
  156.  
  157. //JButton button2 = new JButton("Confirm");
  158. //JButton butt = new JButton("Confirm");
  159. //button.add(new JButton("Confirm"));
  160.  
  161. //Create and add the components.
  162. JLabel chance = new JLabel("Chance: ");
  163. JLabel count = new JLabel("Count: ");
  164. //Label(text area)
  165. JTextField ChanceTextField = new JTextField("Write here chance.", 25);
  166. JTextField CountTextField = new JTextField("Write here count.", 25);
  167.  
  168. //add text for chance
  169. box.add(chance);
  170. //box.add(ChanceTextField);
  171.  
  172. //add text for count
  173. box.add(count);
  174. // box.add(CountTextField);
  175.  
  176. //chance
  177. // layout.putConstraint(SpringLayout.WEST, chance,5,SpringLayout.WEST, contentPane);
  178. // layout.putConstraint(SpringLayout.NORTH, chance,5,SpringLayout.NORTH, contentPane);
  179. //layout.box(BoxLayout, 5);
  180. chance.setAlignmentX(Component.CENTER_ALIGNMENT);
  181. count.setAlignmentX(Component.CENTER_ALIGNMENT);
  182. // box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
  183. // contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));
  184. // contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  185. // contentPane.setLayout(new BoxLayout(ChanceTextField, BoxLayout.LINE_AXIS));
  186. // contentPane.setLayout(new BoxLayout(ChanceTextField, BoxLayout.PAGE_AXIS));
  187. // layout.putConstraint(SpringLayout.WEST, ChanceTextField,5,SpringLayout.EAST, chance);
  188. // layout.putConstraint(SpringLayout.NORTH, ChanceTextField,5,SpringLayout.NORTH, contentPane);
  189.  
  190. //count
  191.  
  192. // layout.putConstraint(SpringLayout.WEST, count,5,SpringLayout.WEST, contentPane);
  193. // layout.putConstraint(SpringLayout.NORTH, count,30,SpringLayout.NORTH, contentPane);
  194.  
  195. // layout.putConstraint(SpringLayout.WEST, CountTextField,5,SpringLayout.EAST, chance);
  196. // layout.putConstraint(SpringLayout.NORTH, CountTextField,30,SpringLayout.NORTH, contentPane);
  197.  
  198. //button
  199.  
  200. //layout.putConstraint(SpringLayout.WEST, button,60,SpringLayout.WEST, button);
  201. //layout.putConstraint(SpringLayout.NORTH, button,300,SpringLayout.NORTH, button);
  202.  
  203. frame.setLayout(layout);
  204. frame.pack();
  205. frame.setVisible(true);
  206. frame.setLocation(0, 200);
  207. frame.setSize(450, 150);
  208. frame.setResizable(false);
  209.  
  210. }
  211.  
  212. public boolean handleEvent(Event e) {
  213. //debug
  214. //System.out.println(e);
  215.  
  216. if (e.id != 1001) return false;
  217.  
  218. if (e.target == btnShowNpc) {
  219. showNpc();
  220. } else if (e.target == btnShowOnline) {
  221. showOnline();
  222. } else if (e.target == btnCalculator) {
  223. Calculator();
  224. }
  225. return true;
  226. }
  227.  
  228. public static void main(String[] args)
  229. {
  230. new first("SQL manager");
  231. }
  232. }
Add Comment
Please, Sign In to add comment