Guest User

Untitled

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