-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLManage.java
109 lines (92 loc) · 3.64 KB
/
SQLManage.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package survey;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLManage {
Connection con;
public SQLManage() throws SQLException {
String url = "jdbc:mysql://localhost:3306/survey";
String usr = "root";
String pass = "roshanhussainv1969@";
con = DriverManager.getConnection(url, usr, pass);
}
public void newUser(String name, String uname, String pass) throws SQLException {
String str = "INSERT INTO actors(fname, uname, pass) values ('"+name+"', '"+uname+"', '"+pass+"')";
Statement stm = con.createStatement();
stm.executeUpdate(str);
}
public int authUser(String uname, String pass) throws SQLException {
String str = "SELECT * FROM actors WHERE uname = '"+uname+"'";
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(str);
if (!rst.next())
return -1;
else {
if(rst.getString("pass").equals(pass))
return rst.getInt("id");
else
return 0;
}
}
public void newQuestion(String code, String question, String op1, String op2, String op3, String op4) throws SQLException {
String str = "INSERT INTO questions values ('"+code+"', '"+question+"', '"+op1+"', '"+op2+"', '"+op3+"', '"+op4+"')";
Statement stm = con.createStatement();
stm.executeUpdate(str);
}
public void userQuestionAdd(int id, String surveycode) throws SQLException {
String str = "INSERT INTO userQuestions values ("+id+", '"+surveycode+"', 0)";
Statement stm = con.createStatement();
stm.executeUpdate(str);
}
public void answerUpdt(String surveycode, int qno, int option) throws SQLException {
String str = "INSERT INTO surveyquestions values ('"+surveycode+"', " + qno + ", " + option + ")";
Statement stm = con.createStatement();
stm.executeUpdate(str);
}
public ResultSet getQuestions(String surveycode) throws SQLException {
String str = "SELECT * FROM questions WHERE surveycode = '"+surveycode+"'";
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(str);
return rst;
}
public ResultSet surveys(int id, String search) throws SQLException {
String str = "SELECT * FROM userQuestions WHERE id = "+id+" and surveycode like '%"+search+"%'";
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(str);
return rst;
}
public void addTotal() throws SQLException {
String str = "UPDATE userQuestions SET total = total+1";
Statement stm = con.createStatement();
stm.executeUpdate(str);
}
public boolean check(String search) throws SQLException {
String str = "SELECT * FROM userQuestions WHERE surveycode = '"+search+"'";
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(str);
if(rst.next())
return true;
else
return false;
}
public void removeSurvey(String surveycode) throws SQLException {
String str = "DELETE FROM questions WHERE surveycode = '"+surveycode+"'";
Statement stm = con.createStatement();
stm.executeUpdate(str);
str = "DELETE FROM surveyquestions WHERE surveycode = '"+surveycode+"'";
stm.executeUpdate(str);
str = "DELETE FROM userQuestions WHERE surveycode = '"+surveycode+"'";
stm.executeUpdate(str);
}
public int getCount(String surveycode, int qno, int op) throws SQLException {
String str = "SELECT count(opno) FROM surveyquestions WHERE surveycode = '"+surveycode+"' AND qno = "+(qno+1)+" AND opno = "+op;
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(str);
if(rst.next())
return rst.getInt("count(opno)");
else
return 0;
}
}