-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsurveyPage1.java
More file actions
145 lines (128 loc) · 5.47 KB
/
Copy pathsurveyPage1.java
File metadata and controls
145 lines (128 loc) · 5.47 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class surveyPage1 extends JFrame {
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;
private JTextField textField5;
private JButton sendButton;
private JButton backToButton;
private JPanel surveyPage1;
private JLabel q1;
private JLabel q2;
private JLabel q3;
private JLabel q4;
private JLabel q5;
public surveyPage1(String ssn) {
q1.setText(getQuestions().get(0));
q2.setText(getQuestions().get(1));
q3.setText(getQuestions().get(2));
q4.setText(getQuestions().get(3));
q5.setText(getQuestions().get(4));
setContentPane(surveyPage1);
setTitle("User Panel");
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
backToButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userPanel userPanel = new userPanel(ssn);
userPanel.setVisible(true);
setVisible(false);
}
});
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String q1Answer = textField1.getText();
String q2Answer = textField2.getText();
String q3Answer = textField3.getText();
String q4Answer = textField4.getText();
String q5Answer = textField5.getText();
//this codes provides us unique response id
int id1= Integer.parseInt(getResponseid().get(getResponseid().size()-1)) +2 ;
//and this adding answers to the database
response(id1,q1Answer,"1",ssn);
int id2= Integer.parseInt(getResponseid().get(getResponseid().size()-1)) +2;
response(id2,q2Answer,"2",ssn);
int id3= Integer.parseInt(getResponseid().get(getResponseid().size()-1))+2;
response(id3,q3Answer,"3",ssn);
int id4= Integer.parseInt(getResponseid().get(getResponseid().size()-1))+2;
response(id4,q4Answer,"4",ssn);
int id5= Integer.parseInt(getResponseid().get(getResponseid().size()-1))+2;
response(id5,q5Answer,"5",ssn);
JOptionPane.showMessageDialog(null,"Send");
userPanel userPanel = new userPanel(ssn);
userPanel.setVisible(true);
setVisible(false);
}
});
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
public List<String> getQuestions( ){
List<String> list=new ArrayList<String>();
try {
//connection with database
Class.forName("org.sqlite.JDBC");
java.sql.Connection connection = DriverManager.getConnection("jdbc:sqlite:data.sqlite");
java.sql.Statement statement = connection.createStatement();
//question description query
ResultSet resultSet = statement.executeQuery("SELECT * FROM questions");
while (resultSet.next()){
list.add(resultSet.getString("Description"));
}
}
catch (Exception ioException){
ioException.printStackTrace();
}
return list;
}
public List<String> getResponseid( ){
List<String> list=new ArrayList<String>();
try {
Class.forName("org.sqlite.JDBC");
java.sql.Connection connection = DriverManager.getConnection("jdbc:sqlite:data.sqlite");
java.sql.Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM responses");
while (resultSet.next()){
list.add((resultSet.getString("Responses_id")));
}
}
catch (Exception ioException){
ioException.printStackTrace();
}
return list;
}
public void response(int Responses_idInt, String Answer ,String Question_id, String SSN ){
try {
String Responses_id = String.valueOf(Responses_idInt);
Class.forName("org.sqlite.JDBC");
java.sql.Connection connection = DriverManager.getConnection("jdbc:sqlite:data.sqlite");
// add users responses' to database
String sql = " insert into responses (`Responses_id`, `Answer`, `Question_id`, `SSN`)"
+ " values (?, ?, ?, ?)";
PreparedStatement preparedStmt = connection.prepareStatement(sql);
preparedStmt.setString (1, Responses_id);
preparedStmt.setString (2, Answer);
preparedStmt.setString(3, Question_id);
preparedStmt.setString (4, SSN);
preparedStmt.execute();
}
catch (Exception ioException){
ioException.printStackTrace();
}
}}