javaSql (1)
javaSql (1)
K. ANANYA - 23WH1A05D7
Date: Signature:
1
Problem Statement:
Design a simple Online Survey System using Java and MySQL to collect a
nd manage survey responses. The system should provide users with a web
interface to perform various operations including creating surveys, submitti
ng responses, and viewing results.
Functional Requirements:
Create Survey:
Allow users to create new surveys.
Accept input for survey title, description, and various question types (
multiple choice, short answer, etc.).
Save the survey details in the database.
Submit Response:
Provide users with a list of available surveys.
Accept and store responses for selected surveys in the database.
Ensure secure submission to maintain data integrity.
View Results:
Allow users to view aggregated survey results.
Provide various data visualization options (charts, graphs) for analysi
s.
Ensure only authorized users can view sensitive data.
User Authentication:
Implement secure login and registration functionality.
Ensure only authenticated users can create surveys and view results.
Constraints:
The system should handle concurrent user access and data submissi
on.
Surveys should support multiple question types.
The database must securely store all survey data and user informatio
n.
The system should provide real-
time data visualization for survey results.
2
SOURCE CODE:
MYSQL DATABASE
--Questions table
CREATE TABLE questions(surveycode varchar(5), question varchar(255), option1
varchar(255), option2 varchar(255), option3 varchar(255), option4 varchar(255));
3
JAVA CODE :
package survey;
import java.sql.SQLException;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
int id;
4
heading.setHorizontalAlignment(JLabel.CENTER);
heading.setFont(new Font("Times New Roman", Font.BOLD, 40));
frame.add(heading);
5
JOptionPane.showMessageDialog(frame, "No User
Found!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else if(id == 0) {
JOptionPane.showMessageDialog(frame, "Wrong
Password!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else {
MainPage mainPage = new MainPage();
try {
mainPage.mainPageView(id);
} catch (SQLException e1) {
e1.printStackTrace();
}
frame.dispose();
}
}
}
});
6
if(!surveyCode.isEmpty() && surveyCode.length() == 5) {
if(manage.check(surveyCode)) {
Guest guest = new Guest();
guest.guestView(surveyCode);
}
else {
JOptionPane.showMessageDialog(frame,
"No Survey Available!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
}
}
catch(Exception e2) {
}
}
});
frame.setVisible(true);
}
package survey;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
7
public class SignUp {
public void signUpView() {
JFrame frame = new JFrame();
frame.setSize(450, 450);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
8
frame.add(uPass2);
9
else {
JOptionPane.showMessageDialog(frame,
"Password Mismatch", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
}
}
});
frame.setVisible(true);
}
}
package survey;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
10
SQLManage manage;
JButton submit;
String[] questionsArray, option1Array, option2Array, option3Array, option4Array;
static DefaultTableModel model;
String cd;
//-------------------------------------ADD PANEL--------------------------------------------
------
JPanel addPanel = new JPanel();
addPanel.setBounds(250, 0, 550, 600);
addPanel.setLayout(null);
11
JLabel option1 = new JLabel("Option 1 : ");
option1.setBounds(50, 165, 100, 20);
addPanel.add(option1);
JTextField option1Field = new JTextField();
option1Field.setBounds(50, 190, 200, 30);
addPanel.add(option1Field);
12
public void actionPerformed(ActionEvent e) {
String qn = questionField.getText();
String op1 = option1Field.getText();
String op2 = option2Field.getText();;
String op3 = option3Field.getText();;
String op4 = option4Field.getText();;
if(qn.equals("") || op1.equals("") || op2.equals("") ||
op3.equals("") || op4.equals("")) {
JOptionPane.showMessageDialog(frame, "Please Enter All
Details!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else {
questionField.setText("");
option1Field.setText("");
option2Field.setText("");
option3Field.setText("");
option4Field.setText("");
queStr[i] = qn;
op1Str[i] = op1;
op2Str[i] = op2;
op3Str[i] = op3;
op4Str[i] = op4;
i++;
submit.setEnabled(true);
}
}
});
13
String op4 = option4Field.getText();;
if(!(qn.equals("") && op1.equals("") && op2.equals("") &&
op3.equals("") && op4.equals(""))) {
JOptionPane.showMessageDialog(frame, "Last details are
not added. If not required, Please clear the fields!!!", "Warning Message",
JOptionPane.WARNING_MESSAGE);
}
else {
if(i==0) {
JOptionPane.showMessageDialog(frame, "No
Questions Added", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else {
try {
manage.userQuestionAdd(id, code);
for(int j=0; j<i; j++) {
manage.newQuestion(code,
queStr[j], op1Str[j], op2Str[j], op3Str[j], op4Str[j]);
}
14
option2Field.setText("");
option3Field.setText("");
option4Field.setText("");
i=0;
}
});
frame.add(addPanel);
//-------------------------------------------------------------------------------------------------
--
//----------------------------------------------------VIEW PANEL----------------------------
-----------------
JPanel viewPanel = new JPanel();
viewPanel.setBounds(250, 0, 550, 600);
viewPanel.setLayout(null);
JLabel searchLabel = new JLabel("Search : ");
searchLabel.setBounds(100, 20, 100, 50);
viewPanel.add(searchLabel);
@Override
public void keyTyped(KeyEvent e) {
//To avoid errors.
}
@Override
public void keyPressed(KeyEvent e) {
//To avoid errors.
}
});
15
JTable table=new JTable(){
public boolean isCellEditable(int row,int column){
return false;
}
};
model = (DefaultTableModel)table.getModel();
table.setBackground(Color.decode("#f9d6c4"));
model.addColumn("Your Surveys");
tblupdt("");
JScrollPane scPane=new JScrollPane(table);
scPane.setBounds(100, 70, 350, 225);
viewPanel.add(scPane);
16
JLabel op3Select = new JLabel();
op3Select.setBounds(100, 480, 150, 50);
viewPanel.add(op3Select);
op1Select.setText(String.valueOf(manage.getCount(cd, h, 1)));
op2Select.setText(String.valueOf(manage.getCount(cd, h, 2)));
op3Select.setText(String.valueOf(manage.getCount(cd, h, 3)));
op4Select.setText(String.valueOf(manage.getCount(cd, h, 4)));
} catch (SQLException e1) {
e1.printStackTrace();
}
17
}
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
manage.removeSurvey(cd);
} catch (SQLException e1) {
e1.printStackTrace();
}
tblupdt(search.getText());
}
});
18
}
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
h=0;
delete.setEnabled(true);
viewNext.setEnabled(true);
viewPrev.setEnabled(true);
int row = table.getSelectedRow();
cd = String.valueOf(model.getValueAt(row, 0));
try {
ResultSet rst1 = manage.getQuestions(cd);
for(int x=0; rst1.next(); x++) {
questionsArray[x] = rst1.getString("question");
option1Array[x] = rst1.getString("option1");
option2Array[x] = rst1.getString("option2");
option3Array[x] = rst1.getString("option3");
option4Array[x] = rst1.getString("option4");
}
op1Select.setText(String.valueOf(manage.getCount(cd, h,
1)));
op2Select.setText(String.valueOf(manage.getCount(cd, h,
2)));
op3Select.setText(String.valueOf(manage.getCount(cd, h,
3)));
op4Select.setText(String.valueOf(manage.getCount(cd, h,
4)));
}
catch (SQLException e1) {
e1.printStackTrace();
}
quesView.setText(questionsArray[h]);
op1View.setText(option1Array[h]);
op2View.setText(option2Array[h]);
op3View.setText(option3Array[h]);
op4View.setText(option4Array[h]);
19
}
});
frame.add(viewPanel);
//-------------------------------------------------------------------------------------------------
-------------
//-----------------------------------------------SIDE PANEL---------------------------------
----------------------
JPanel optionPanel = new JPanel();
optionPanel.setBounds(0, 0, 250, 600);
optionPanel.setBackground(Color.gray);
optionPanel.setLayout(null);
frame.add(optionPanel);
20
JButton logout = new JButton("LOGOUT");
logout.setBounds(50, 440, 150, 50);
optionPanel.add(logout);
logout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Login login = new Login();
try {
login.loginView();
} catch (SQLException e1) {
e1.printStackTrace();
}
frame.dispose();
}
});
//-------------------------------------------------------------------------------------------------
------
viewPanel.setVisible(false);
frame.setVisible(true);
}
21
model.removeRow(i);
for(i=0; res.next(); i++) {
model.addRow(new Object[0]);
model.setValueAt(res.getString("surveycode"), i, 0);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
package survey;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Connection con;
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);
}
22
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 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);
}
23
}
public int getCount(String surveycode, int qno, int op) throws SQLException {
24
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;
}
OUTPUTS :
25
26
27