Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. package main;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JTextField;
  18.  
  19.  
  20.  
  21.  
  22. public class frame
  23. {
  24. private static Connection con = null;
  25. private static String dbHost = "localhost"; // Hostname
  26. private static String dbPort = "3306"; // Port -- Standard: 3306
  27. private static String dbName = "skypvp"; // Datenbankname
  28. private static String dbUser = "root"; // Datenbankuser
  29. private static String dbPass = "123456"; // Datenbankpasswort
  30.  
  31.  
  32. public static void main (String[] args)
  33. {
  34. getInstance();
  35. update("CREATE TABLE IF NOT EXISTS mysql(name varchar(100) UNIQUE, email varchar(100), password varchar(100))");
  36. JFrame frame = new JFrame ("Rechner");
  37. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  38. frame.setLayout(null);
  39. frame.setSize(500,300);
  40. Color cl = Color.getHSBColor(508, 463, 320);
  41. frame.getContentPane().setBackground(cl);
  42. //Name
  43. JTextField field1 = new JTextField(16);
  44. frame.add(field1);
  45. field1.setBounds(160,40,160,30);
  46.  
  47. JLabel label = new JLabel("Name");
  48. label.setBounds(120, 30, 300, 50);
  49. label.setText("<html>Name:</html>");
  50. frame.getContentPane().add(label);
  51.  
  52. //E-Mail
  53. JPasswordField field2 = new JPasswordField(16);
  54. frame.add(field2);
  55. field2.setBounds(160, 80, 160, 30);
  56.  
  57. JLabel label2 = new JLabel("Password");
  58. label2.setBounds(95, 70, 300, 50);
  59. label2.setText("<html>Passwort:</html>");
  60. frame.getContentPane().add(label2);
  61.  
  62. //Button
  63. JButton button = new JButton ("Anmelden");
  64. button.setBounds(160,120,160,40);
  65. button.addActionListener (new ActionListener() {
  66.  
  67. @SuppressWarnings("deprecation")
  68. @Override
  69. public void actionPerformed (ActionEvent e)
  70. {
  71. String eingabePassword = field2.getText();
  72. String eingabeName = field1.getText();
  73. if(getInstance() == null){
  74. Object[] options= { "Ok" };
  75. JOptionPane.showOptionDialog(null, "Verbindung konnte nicht aufgebaut werden", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  76. return;
  77. } else {
  78. if(getName(eingabeName) == true && eingabePassword.equals(getPassword(eingabeName))){
  79. System.out.println("Accept");
  80. } else {
  81. Object[] options= { "Ok" };
  82. JOptionPane.showOptionDialog(null, "Passwort oder Name falsch", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  83. }
  84.  
  85. }
  86. }
  87. });
  88.  
  89.  
  90. JButton button2 = new JButton("Registrieren");
  91. button2.setBounds(160, 170, 160, 40);
  92. button2.addActionListener(new ActionListener() {
  93.  
  94. @Override
  95. public void actionPerformed(ActionEvent e) {
  96. JFrame register = new JFrame("Registrieren");
  97. register.setLayout(null);
  98. register.setSize(400, 500);
  99.  
  100. Color cl = Color.getHSBColor(5208, 463, 320);
  101. register.getContentPane().setBackground(cl);
  102. //Name
  103. JTextField field3 = new JTextField(16);
  104. register.add(field3);
  105. field3.setBounds(120,90,160,30);
  106.  
  107. JLabel label3 = new JLabel("Name");
  108. label3.setBounds(80, 80, 300, 50);
  109. label3.setText("<html>Name:</html>");
  110. register.getContentPane().add(label3);
  111.  
  112. //Password
  113. JTextField field4 = new JTextField(16);
  114. register.add(field4);
  115. field4.setBounds(120, 130, 160, 30);
  116.  
  117. JLabel label4 = new JLabel("email");
  118. label4.setBounds(77, 120, 300, 50);
  119. label4.setText("<html>E-Mail:</html>");
  120. register.getContentPane().add(label4);
  121.  
  122. //Password
  123. JPasswordField field5 = new JPasswordField(16);
  124. register.add(field5);
  125. field5.setBounds(120, 170, 160, 30);
  126.  
  127. JLabel label5 = new JLabel("password");
  128. label5.setBounds(55, 160, 300, 50);
  129. label5.setText("<html>Passwort:</html>");
  130. register.getContentPane().add(label5);
  131.  
  132. JButton buttonRegister = new JButton("Registrieren");
  133. buttonRegister.setBounds(120, 210, 160, 40);
  134. buttonRegister.addActionListener(new ActionListener() {
  135.  
  136. @Override
  137. public void actionPerformed(ActionEvent e) {
  138. String eingabeName = field3.getText();
  139. String eingabeEmail = field4.getText();
  140. String eingabePW = field5.getText();
  141. boolean name = false;
  142. boolean email = false;
  143.  
  144. if(eingabeEmail != null || eingabeName != null || eingabePW != null){
  145.  
  146. if(getName(eingabeName) == true){
  147. name = true;
  148. }
  149.  
  150. if(getEmail(eingabeEmail) == true){
  151. email = true;
  152. }
  153.  
  154. if(email == true || name == true){
  155. Object[] options= { "Ok" };
  156. JOptionPane.showOptionDialog(null, "Name oder Email wird schon verwendet", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  157. return;
  158. } else {
  159. if(eingabePW.length() >=8){
  160. EmailValidator emailValidator = new EmailValidator();
  161. if(!emailValidator.validate(field4.getText().trim())) {
  162. System.out.print("Invalid Email ID");
  163. field4.setBackground(Color.red);
  164. Object[] options= { "Ok" };
  165. JOptionPane.showOptionDialog(null, "Invalid E-Mail", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  166. return;
  167. }
  168. register(eingabeName, eingabeEmail, eingabePW);
  169. System.out.println(eingabeName + " wurde mit der E-Mail " + eingabeEmail + " registriert");
  170. register.hide();
  171. Object[] options= { "Ok" };
  172. JOptionPane.showOptionDialog(null, "Erfolgreich registriet", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  173. } else {
  174. Object[] options= { "Ok" };
  175. JOptionPane.showOptionDialog(null, "Passwort ist zu kurz", "ERROR", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
  176. }
  177.  
  178. }
  179.  
  180. }
  181.  
  182. }
  183. });
  184.  
  185.  
  186.  
  187. register.setVisible(true);
  188. register.add(buttonRegister);
  189. }
  190. });
  191.  
  192.  
  193. frame.add(button2);
  194. frame.add(button);
  195. frame.setVisible (true);
  196. }
  197.  
  198.  
  199. private frame(){
  200. try {
  201. Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.
  202.  
  203. // Verbindung zur JDBC-Datenbank herstellen.
  204. con = DriverManager.getConnection("jdbc:mysql://"+dbHost+":"+ dbPort+"/"+dbName+"?"+"user="+dbUser+"&"+"password="+dbPass);
  205. System.out.println("Verbindung erfolgreich");
  206. } catch (ClassNotFoundException e) {
  207. System.out.println("Treiber nicht gefunden");
  208. e.printStackTrace();
  209. } catch (SQLException e) {
  210. System.out.println("Verbindung nicht moglich");
  211. System.out.println("SQLException: " + e.getMessage());
  212. System.out.println("SQLState: " + e.getSQLState());
  213. System.out.println("VendorError: " + e.getErrorCode());
  214. }
  215. }
  216.  
  217. private static Connection getInstance(){
  218. if(con == null)
  219. new frame();
  220. return con;
  221. }
  222.  
  223. public static void update(String qry) {
  224. try {
  225. Statement st = (Statement) getInstance().createStatement();
  226. st.executeUpdate(qry);
  227. st.close();
  228. } catch (Exception localException) {
  229. }
  230. }
  231.  
  232. public static ResultSet query(String qry) {
  233.  
  234. ResultSet rs = null;
  235.  
  236. try {
  237. Statement st = (Statement) getInstance().createStatement();
  238. rs = st.executeQuery(qry);
  239. } catch (Exception localException) {
  240. }
  241. return rs;
  242. }
  243.  
  244. public static boolean getName(String name) {
  245.  
  246. ResultSet rs = query("SELECT * FROM mysql WHERE name = '" + name + "' LIMIT 1");
  247. try {
  248. return rs.next();
  249. } catch (SQLException e) {
  250. }
  251. return false;
  252. }
  253.  
  254. public static boolean getEmail(String email) {
  255.  
  256. ResultSet rs = query("SELECT * FROM mysql WHERE email = '" + email + "' LIMIT 1");
  257. try {
  258. return rs.next();
  259. } catch (SQLException e) {
  260. }
  261. return false;
  262. }
  263.  
  264. public static String getPassword(String name) {
  265.  
  266. ResultSet rs = query("SELECT password FROM mysql WHERE name = '" + name + "' LIMIT 1");
  267. String array = new String();
  268. try {
  269. while(rs.next()) {
  270. array = rs.getString("password");
  271. }
  272. } catch (SQLException e) {
  273. e.printStackTrace();
  274. return array;
  275. }
  276. return array;
  277. }
  278.  
  279. public static void register(String name, String email, String pw){
  280. update("INSERT INTO mysql (name, email, password) VALUES ('" + name + "','" + email + "', '" + pw + "')");
  281. }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement