Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package logowanie;
- import java.io.UnsupportedEncodingException;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class MDDB {
- public static Connection connect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
- Connection connection;
- String connectionURL = "jdbc:mysql://127.0.0.1:3306/logowanie";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection(connectionURL, "root", "");
- return connection;
- }
- public static ResultSet select(Connection c, String s) throws SQLException {
- Statement stmt = c.createStatement();
- return stmt.executeQuery(s);
- }
- public static void execute(Connection c, String s) throws SQLException {
- Statement stmt = c.createStatement();
- stmt.execute(s);
- }
- public static int resultSize(ResultSet rs) throws SQLException {
- int size;
- rs.last();
- size = rs.getRow();
- rs.beforeFirst();
- return size;
- }
- private static String md5(String aString) throws NoSuchAlgorithmException, UnsupportedEncodingException {
- MessageDigest md;
- String hex;
- StringBuffer hexString;
- byte[] bytesOfMessage;
- byte[] theDigest;
- hexString = new StringBuffer();
- bytesOfMessage = aString.getBytes("UTF-8");
- md = MessageDigest.getInstance("MD5");
- theDigest = md.digest(bytesOfMessage);
- for (int i = 0; i < theDigest.length; i++) {
- hex = Integer.toHexString(0xff & theDigest[i]);
- if (hex.length() == 1) {
- hexString.append('0');
- }
- hexString.append(hex);
- }
- return hexString.toString();
- }
- public static boolean createUser(Connection con, String user, String password) {
- Random random;
- String insert;
- String salt;
- random = new Random();
- salt = new BigInteger(130, random).toString(16);
- insert = "INSERT INTO uzytkownik "
- + "(login, haslo) "
- + "VALUES (?, ?)";
- try (PreparedStatement pstmt = con.prepareStatement(insert)) {
- pstmt.setString(1, user);
- pstmt.setString(2, md5(password));
- pstmt.executeUpdate();
- return true;
- } catch (NoSuchAlgorithmException | SQLException | UnsupportedEncodingException ex) {
- return false;
- }
- }
- public static void main(String[] args) {
- try {
- Connection c = connect();
- if (createUser(c, "nazi", "Kappa")) {
- System.out.println("User created");
- }
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
- Logger.getLogger(MDDB.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement