-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.java
More file actions
95 lines (79 loc) · 2.82 KB
/
user.java
File metadata and controls
95 lines (79 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* 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 javastdapp;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author HP
*/
public class user {
private final MessageDigest md;
public user() throws SecurityException {
try {
md = MessageDigest.getInstance("MD5", "SUN");
}catch(NoSuchAlgorithmException | NoSuchProviderException se) {
throw new SecurityException("In MD5 constructor " + se);
}
}
public void insertUser(String username,String password){
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try
{
ps = con.prepareStatement("INSERT INTO user(username,password) VALUES (?,?)");
ps.setString(1, username);
ps.setString(2, password);
if(ps.executeUpdate() > 0){
JOptionPane.showMessageDialog(null, "New User Added");
}
}
catch (SQLException ex)
{
Logger.getLogger(user.class.getName()).log(Level.SEVERE,null,ex);
}
}
public boolean isUserExist(String userName){
boolean isExist= false;
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try{
ps = con.prepareStatement("SELECT * FROM `user` WHERE `username` = ?");
ps.setString(1,userName);
ResultSet rs =ps.executeQuery();
if(rs.next())
{
isExist= true;
}
} catch (SQLException ex) {
Logger.getLogger(user.class.getName()).log(Level.SEVERE, null, ex);
}
return isExist;
}
public static String getMD5Hash(String s) throws NoSuchAlgorithmException {
String result = s;
if (s != null) {
MessageDigest md = MessageDigest.getInstance("MD5"); // or "SHA-1"
md.update(s.getBytes());
BigInteger hash = new BigInteger(1, md.digest());
result = hash.toString(16);
while (result.length() < 32) { // 40 for SHA-1
result = "0" + result;
}
}
return result;
}
}