Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package logowanie;
  7.  
  8. import java.io.UnsupportedEncodingException;
  9. import java.math.BigInteger;
  10. import java.security.MessageDigest;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.security.SecureRandom;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import java.util.Random;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22.  
  23. /**
  24. *
  25. * @author student
  26. */
  27. public class MDDB {
  28.  
  29. public static Connection connect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  30. Connection connection;
  31. String connectionURL = "jdbc:mysql://127.0.0.1:3306/logowanie";
  32. Class.forName("com.mysql.jdbc.Driver").newInstance();
  33. connection = DriverManager.getConnection(connectionURL, "root", "");
  34. return connection;
  35. }
  36.  
  37. public static ResultSet select(Connection c, String s) throws SQLException {
  38. Statement stmt = c.createStatement();
  39. return stmt.executeQuery(s);
  40. }
  41.  
  42. public static void execute(Connection c, String s) throws SQLException {
  43. Statement stmt = c.createStatement();
  44. stmt.execute(s);
  45. }
  46.  
  47. public static int resultSize(ResultSet rs) throws SQLException {
  48. int size;
  49. rs.last();
  50. size = rs.getRow();
  51. rs.beforeFirst();
  52. return size;
  53. }
  54.  
  55. private static String md5(String aString) throws NoSuchAlgorithmException, UnsupportedEncodingException {
  56. MessageDigest md;
  57. String hex;
  58. StringBuffer hexString;
  59. byte[] bytesOfMessage;
  60. byte[] theDigest;
  61.  
  62. hexString = new StringBuffer();
  63. bytesOfMessage = aString.getBytes("UTF-8");
  64. md = MessageDigest.getInstance("MD5");
  65. theDigest = md.digest(bytesOfMessage);
  66.  
  67. for (int i = 0; i < theDigest.length; i++) {
  68. hex = Integer.toHexString(0xff & theDigest[i]);
  69. if (hex.length() == 1) {
  70. hexString.append('0');
  71. }
  72. hexString.append(hex);
  73. }
  74.  
  75. return hexString.toString();
  76. }
  77.  
  78. public static boolean createUser(Connection con, String user, String password) {
  79. Random random;
  80. String insert;
  81. String salt;
  82.  
  83. random = new Random();
  84. salt = new BigInteger(130, random).toString(16);
  85.  
  86. insert = "INSERT INTO uzytkownik "
  87. + "(login, haslo) "
  88. + "VALUES (?, ?)";
  89.  
  90. try (PreparedStatement pstmt = con.prepareStatement(insert)) {
  91. pstmt.setString(1, user);
  92. pstmt.setString(2, md5(password));
  93. pstmt.executeUpdate();
  94.  
  95. return true;
  96. } catch (NoSuchAlgorithmException | SQLException | UnsupportedEncodingException ex) {
  97. return false;
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102.  
  103. try {
  104. Connection c = connect();
  105. if (createUser(c, "nazi", "Kappa")) {
  106. System.out.println("User created");
  107. }
  108. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
  109. Logger.getLogger(MDDB.class.getName()).log(Level.SEVERE, null, ex);
  110. }
  111.  
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement