0% found this document useful (0 votes)
21 views

javaSql (1)

Uploaded by

ananyakarra2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

javaSql (1)

Uploaded by

ananyakarra2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

OOPJ PROJECT

Department of CSE Certified that this is a Bonafide


Record of the word done by:

K. ANANYA - 23WH1A05D7

Of Class CSE C of Year 2 of Semester 1 in OOPJ Laboratory

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

CREATE DATABASE survey;


USE survey;
--Actor table
CREATE TABLE actors(id int primary key auto_increment, fname varchar(50), uname
varchar(50), pass varchar(50));

--User Question Table


CREATE TABLE userQuestions(id int, surveycode varchar(5), total int);

--Questions table
CREATE TABLE questions(surveycode varchar(5), question varchar(255), option1
varchar(255), option2 varchar(255), option3 varchar(255), option4 varchar(255));

--Survey Answer table


CREATE TABLE surveyquestions(surveycode varchar(5), qno int, opno int);

3
JAVA CODE :

package survey;

import java.sql.SQLException;

public class Main {


public static void main(String args[]) throws SQLException {
Login login = new Login();
login.loginView();
}
}
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;

public class Login {

int id;

public void loginView() throws SQLException {


SQLManage manage = new SQLManage();
JFrame frame = new JFrame();
frame.setSize(450, 450);
frame.setLayout(null);
frame.setLocationRelativeTo(null);

JLabel heading = new JLabel("SURVEY SYSTEM");


heading.setBounds(0, 50, 450, 50);

4
heading.setHorizontalAlignment(JLabel.CENTER);
heading.setFont(new Font("Times New Roman", Font.BOLD, 40));
frame.add(heading);

JLabel uname = new JLabel("Username : ");


uname.setBounds(50, 130, 150, 50);
frame.add(uname);

JTextField name = new JTextField();


name.setBounds(50, 170, 350, 30);
frame.add(name);

JLabel upass = new JLabel("Password : ");


upass.setBounds(50, 200, 150, 50);
frame.add(upass);

JPasswordField pass = new JPasswordField();


pass.setBounds(50, 240, 350, 30);
frame.add(pass);

JButton login = new JButton("LOGIN");


login.setBounds(100, 300, 100, 40);
frame.add(login);
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = name.getText();
String password = pass.getText();
if(username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Please Enter All
Details!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else {
try {
SQLManage manage= new SQLManage();
id = manage.authUser(username, password);
} catch (SQLException e1) {
e1.printStackTrace();
}
if (id == -1) {

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();
}
}
}
});

JButton signUp = new JButton("SIGNUP");


signUp.setBounds(250, 300, 100, 40);
frame.add(signUp);
signUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SignUp signup = new SignUp();
signup.signUpView();
}
});

JButton attend = new JButton("ATTEND A SURVEY (GUEST)");


attend.setBounds(100, 350, 250, 40);
frame.add(attend);
attend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String surveyCode = JOptionPane.showInputDialog("Enter the
Survey Code : ");
try {

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);

JLabel heading = new JLabel("SURVEY SYSTEM");


heading.setBounds(0, 50, 450, 50);
heading.setHorizontalAlignment(JLabel.CENTER);
heading.setFont(new Font("Times New Roman", Font.BOLD, 40));
frame.add(heading);

JLabel fName = new JLabel("Name : ");


fName.setBounds(50, 120, 150, 50);
frame.add(fName);

JTextField fNameField = new JTextField();


fNameField.setBounds(50, 160, 350, 30);
frame.add(fNameField);

JLabel uName = new JLabel("Username : ");


uName.setBounds(50, 190, 150, 50);
frame.add(uName);

JTextField uNameField = new JTextField();


uNameField.setBounds(50, 230, 350, 30);
frame.add(uNameField);

JLabel uPass = new JLabel("Password : ");


uPass.setBounds(50, 260, 150, 50);
frame.add(uPass);

JPasswordField uPassField = new JPasswordField();


uPassField.setBounds(50, 300, 150, 30);
frame.add(uPassField);

JLabel uPass2 = new JLabel("Confirm Password : ");


uPass2.setBounds(250, 260, 150, 50);

8
frame.add(uPass2);

JPasswordField uPassField2 = new JPasswordField();


uPassField2.setBounds(250, 300, 150, 30);
frame.add(uPassField2);

JButton submit = new JButton("SUBMIT");


submit.setBounds(175, 350, 100, 40);
frame.add(submit);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fname = fNameField.getText();
String uname = uNameField.getText();
String pass1 = uPassField.getText();
String pass2 = uPassField2.getText();
if(fname.isEmpty() || uname.isEmpty() || pass1.isEmpty() ||
pass2.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Please Enter All
Details!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
else {
if(pass1.equals(pass2)) {
try {
SQLManage manage = new SQLManage();
manage.newUser(fname, uname, pass1);
fNameField.setText("");
uNameField.setText("");
uPassField.setText("");
uPassField2.setText("");
JOptionPane.showMessageDialog(frame,
"User Created Succesfully!!!", "User Created", JOptionPane.PLAIN_MESSAGE);
frame.dispose();

} catch (SQLException e1) {


JOptionPane.showMessageDialog(frame,
"Something went wrong!!!", "Warning Message", JOptionPane.WARNING_MESSAGE);
}

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;

public class MainPage {

10
SQLManage manage;
JButton submit;
String[] questionsArray, option1Array, option2Array, option3Array, option4Array;
static DefaultTableModel model;
String cd;

int i=0, h=0;


String[] queStr = new String[50];
String[] op1Str = new String[50];
String[] op2Str = new String[50];
String[] op3Str = new String[50];
String[] op4Str = new String[50];
int id;
public void mainPageView(int id) throws SQLException {
this.id=id;
questionsArray = new String[25];
option1Array = new String[25];
option2Array = new String[25];
option3Array = new String[25];
option4Array = new String[25];

manage = new SQLManage();

JFrame frame = new JFrame();


frame.setSize(800, 600);
frame.setLayout(null);
frame.setLocationRelativeTo(null);

//-------------------------------------ADD PANEL--------------------------------------------
------
JPanel addPanel = new JPanel();
addPanel.setBounds(250, 0, 550, 600);
addPanel.setLayout(null);

JLabel question = new JLabel("Question : ");


question.setBounds(50, 100, 100, 20);
addPanel.add(question);
JTextField questionField = new JTextField();
questionField.setBounds(50, 125, 450, 30);
addPanel.add(questionField);

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);

JLabel option2 = new JLabel("Option 2 : ");


option2.setBounds(50, 230, 100, 20);
addPanel.add(option2);
JTextField option2Field = new JTextField();
option2Field.setBounds(50, 255, 200, 30);
addPanel.add(option2Field);

JLabel option3 = new JLabel("Option 3 : ");


option3.setBounds(50, 295, 100, 20);
addPanel.add(option3);
JTextField option3Field = new JTextField();
option3Field.setBounds(50, 320, 200, 30);
addPanel.add(option3Field);

JLabel option4 = new JLabel("Option 4 : ");


option4.setBounds(50, 360, 100, 20);
addPanel.add(option4);
JTextField option4Field = new JTextField();
option4Field.setBounds(50, 385, 200, 30);
addPanel.add(option4Field);

JLabel start = new JLabel("CREATE A SURVEY");


start.setBounds(0, 10, 550, 50);
start.setHorizontalAlignment(JLabel.CENTER);
start.setFont(new Font("Times New Roman", Font.BOLD, 40));
addPanel.add(start);

JButton next = new JButton("ADD QUESTION >");


next.setBounds(50, 440, 450, 35);
addPanel.add(next);
next.addActionListener(new ActionListener( ) {
@Override

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);
}
}
});

submit = new JButton("SUBMIT");


submit.setBounds(50, 490, 200, 50);
submit.setEnabled(false);
addPanel.add(submit);
submit.addActionListener(new ActionListener( ) {
@Override
public void actionPerformed(ActionEvent e) {
String code = stringGenerator();
String qn = questionField.getText();
String op1 = option1Field.getText();
String op2 = option2Field.getText();;
String op3 = option3Field.getText();;

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]);
}

JOptionPane.showMessageDialog(frame, "Survey Added. Survey Code : " + code,


"Congradulations", JOptionPane.PLAIN_MESSAGE);
}
catch (SQLException e1) {
e1.printStackTrace();
}
}
}
submit.setEnabled(false);
}
});

JButton cancel = new JButton("CANCEL");


cancel.setBounds(300, 490, 200, 50);
addPanel.add(cancel);
cancel.addActionListener(new ActionListener( ) {
@Override
public void actionPerformed(ActionEvent e) {
questionField.setText("");
option1Field.setText("");

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);

JTextField search = new JTextField();


search.setBounds(160, 30, 290, 30);
viewPanel.add(search);
search.addKeyListener(new KeyListener() {
@Override
public void keyReleased(KeyEvent e) {
tblupdt(search.getText());
}

@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);

JLabel quesView = new JLabel();


quesView.setBounds(50, 340, 150, 50);
viewPanel.add(quesView);

JLabel op1View = new JLabel();


op1View.setBounds(70, 380, 150, 50);
viewPanel.add(op1View);

JLabel op2View = new JLabel();


op2View.setBounds(70, 420, 150, 50);
viewPanel.add(op2View);

JLabel op3View = new JLabel();


op3View.setBounds(70, 460, 150, 50);
viewPanel.add(op3View);

JLabel op4View = new JLabel();


op4View.setBounds(70, 500, 150, 50);
viewPanel.add(op4View);

JLabel op1Select = new JLabel();


op1Select.setBounds(100, 400, 150, 50);
viewPanel.add(op1Select);

JLabel op2Select = new JLabel();


op2Select.setBounds(100, 440, 150, 50);
viewPanel.add(op2Select);

16
JLabel op3Select = new JLabel();
op3Select.setBounds(100, 480, 150, 50);
viewPanel.add(op3Select);

JLabel op4Select = new JLabel();


op4Select.setBounds(100, 520, 150, 50);
viewPanel.add(op4Select);

JButton delete = new JButton("DELETE");


delete.setBounds(210, 300, 130, 50);
delete.setEnabled(false);
viewPanel.add(delete);

JButton viewPrev = new JButton("PREVIOUS");


viewPrev.setBounds(100, 300, 100, 50);
viewPrev.setEnabled(false);
viewPanel.add(viewPrev);
viewPrev.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(h>=0) {
h--;
quesView.setText(questionsArray[h]);
op1View.setText(option1Array[h]);
op2View.setText(option2Array[h]);
op3View.setText(option3Array[h]);
op4View.setText(option4Array[h]);
try {

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());
}
});

JButton viewNext = new JButton("NEXT");


viewNext.setBounds(350, 300, 100, 50);
viewNext.setEnabled(false);
viewPanel.add(viewNext);
viewNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
h++;
quesView.setText(questionsArray[h]);
op1View.setText(option1Array[h]);
op2View.setText(option2Array[h]);
op3View.setText(option3Array[h]);
op4View.setText(option4Array[h]);
try {
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();

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);

JButton addSurvey = new JButton("ADD SURVEY");


addSurvey.setBounds(50, 113, 150, 50);
optionPanel.add(addSurvey);
addSurvey.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewPanel.setVisible(false);
addPanel.setVisible(true);
}
});

JButton viewSurvey = new JButton("VIEW SURVEY");


viewSurvey.setBounds(50, 276, 150, 50);
optionPanel.add(viewSurvey);
viewSurvey.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tblupdt(search.getText());
viewPanel.setVisible(true);
addPanel.setVisible(false);
}
});

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);
}

public String stringGenerator() {


String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String sb = "";
for (int i = 0; i < 5; i++) {
int index = (int)(AlphaNumericString.length() * Math.random());
sb += (AlphaNumericString.charAt(index));
}
return sb;
}

public void tblupdt(String str) {


try {
SQLManage man = new SQLManage();
ResultSet res = man.surveys(id, str);
int rowCount = model.getRowCount();
int i;
for (i = rowCount - 1; i >= 0; i--)

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;

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+"'";

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 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;

23
}

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 {

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

You might also like