Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public class Main {
  2. public static void main(String[] args) {
  3. new Window();
  4. }
  5. }
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. import javax.swing.*;
  19.  
  20. import java.awt.event.*;
  21.  
  22. public class Window extends JFrame {
  23. private static Database db;
  24. JPanel panel;
  25. JLabel lblName;
  26. JLabel lblLvl;
  27. JTextField txtName;
  28. JTextField txtLvl;
  29. JComboBox<String> boxType;
  30. JButton btnSave;
  31. Boolean boxTypeExist;
  32.  
  33. public Window() {
  34. db = new Database();
  35.  
  36. Boolean connection;
  37. do{
  38. connection = db.connect();
  39. }while(!connection);
  40.  
  41. setTitle("Register");
  42. setSize(300,70);
  43. setResizable(false);
  44.  
  45. JPanel panel = new JPanel();
  46. panel.setLayout(null);
  47.  
  48. lblName = new JLabel("Name :");
  49. lblLvl = new JLabel("Level :");
  50.  
  51. txtName = new JTextField(12);
  52. txtLvl = new JTextField(5);
  53.  
  54. boxTypeExist = false;
  55. boxType = new JComboBox<String>(new String[]{"Type","Warrior","Magician","Archer","Assassin","Thief","Yordle"});
  56. boxType.addActionListener(new ActionListener() {
  57. public void actionPerformed(ActionEvent e) {
  58. boxType.removeItemAt(0);
  59. boxTypeExist = true;
  60. boxType.removeActionListener(this);
  61. }
  62. });
  63.  
  64. btnSave = new JButton("Save");
  65. btnSave.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. String name = txtName.getText();
  68. String level = txtLvl.getText();
  69. if (name.length() > 0)
  70. if (level.length() > 0)
  71. if (boxTypeExist){
  72. String type = boxType.getSelectedItem().toString();
  73. db.execute("insert into Characters values ('" + name + "'," + level + ",'" + type + "')");
  74. db.disconnect();
  75. dispose();
  76. new Window();
  77. }
  78. }
  79. });
  80.  
  81. lblName.setBounds(15,10,50,20);
  82. lblLvl.setBounds(15,30,50,20);
  83.  
  84. txtName.setBounds(75,10,100,20);
  85. txtLvl.setBounds(75,30,100,20);
  86.  
  87. boxType.setBounds(180,10,100,20);
  88. btnSave.setBounds(180,30,100,20);
  89.  
  90. panel.add(lblName);
  91. panel.add(txtName);
  92. panel.add(lblLvl);
  93. panel.add(txtLvl);
  94. panel.add(boxType);
  95. panel.add(btnSave);
  96.  
  97. getContentPane().add(panel);
  98. setVisible(true);
  99. }
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. import java.sql.Connection;
  123. import java.sql.DriverManager;
  124. import java.sql.ResultSet;
  125. import java.sql.SQLException;
  126. import java.sql.Statement;
  127.  
  128. public class Database {
  129. // mysql -h localhost -u root -p 1234
  130.  
  131. private static Connection connection;
  132.  
  133. public static boolean connect() {
  134. try {
  135. Class.forName("com.mysql.jdbc.Driver");
  136.  
  137. String dbName = "SavedData";
  138. String userName = "root";
  139. String dbPw = "1234";
  140. Database.connection = DriverManager.getConnection("jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+dbPw);
  141.  
  142. } catch (ClassNotFoundException | SQLException e) {
  143. e.printStackTrace();
  144. return false;
  145. }
  146. return true;
  147. }
  148.  
  149. public static boolean disconnect() {
  150.  
  151. try {
  152. Database.connection.close();
  153. } catch (SQLException e) {
  154. e.printStackTrace();
  155. return false;
  156. }
  157.  
  158. return true;
  159. }
  160.  
  161. public static ResultSet execute(String sql) {
  162. Statement exec;
  163.  
  164. try {
  165. exec = Database.connection.createStatement();
  166. exec.execute(sql);
  167.  
  168. return exec.getResultSet();
  169. } catch (SQLException e) {
  170. e.printStackTrace();
  171. return null;
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement