-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserDAO.java
More file actions
73 lines (70 loc) · 2.39 KB
/
UserDAO.java
File metadata and controls
73 lines (70 loc) · 2.39 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
/*
* 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 techquiz.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import techquiz.dbutil.DBConnection;
import techquiz.dto.UserPojo;
/**
*
* @author devanshi
*/
public class UserDAO {
private static PreparedStatement ps,ps1,ps2;
static
{
try
{
ps=DBConnection.getConnection().prepareStatement("Select * from users where userid=? and password=? and usertype=?");
}
catch(SQLException ex)
{
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"Cannot connect to the DB!");
}
}
public static boolean validateUser(UserPojo user)throws SQLException
{ boolean done=false;
ps.setString(1,user.getUserId());
ps.setString(2,user.getPassword());
ps.setString(3,user.getUserType().toLowerCase());
ResultSet rs=ps.executeQuery();
if(rs.next())
done=true;
System.out.println("done val:"+done);
return done;
}
public static boolean addUser(UserPojo user)throws SQLException
{
ps1=DBConnection.getConnection().prepareStatement("Select * from users where userid=?");
ps1.setString(1,user.getUserId());
boolean status=true;
ResultSet rs=ps1.executeQuery();
if(rs.next())
{
status=false;
}
else
{
ps2=DBConnection.getConnection().prepareStatement("Insert into users values(?,?,?)");
ps2.setString(1,user.getUserId());
ps2.setString(2,user.getPassword());
ps2.setString(3,user.getUserType());
ps2.executeUpdate();
}
return status;
}
public static boolean updateUser(UserPojo user)throws SQLException
{
PreparedStatement ps3=DBConnection.getConnection().prepareStatement("update users set password=? where userid=? and usertype='Student'");
ps3.setString(1, user.getPassword());
ps3.setString(2,user.getUserId());
int ans=ps3.executeUpdate();
return (ans!=0);
}
}