RunCodes
Programming & Tech

How to Create Quiz Application in Java Using MySQL Database and NetBeans IDE?[With Source Code]

8 8,879

Hello Guyz, In this tutorial we are gonna learn about how you can create quiz application using java.

Before getting into the topic, let’s understand what is the basic concept, how this is programmatically possible and so on. In general quiz application, user select their answer and click send button to submit their answer to the server database (in case of online) or to local database(in case of offline) and then they check their answer by click other button. So, we need to understand programmatically what happen when the user select their answer and click on submit button and what is the operation behind when user click on the check button. Being the programmer you can imagine what operation need to perform to do this using java.

So, our basic concept is when the user select their answer, their answer need to be inserted into the database and return some value when user click the another button for checking answer. So, for this we are going to create one database in our MySQL database it means you need to instant any server application like WAMP Server or XAMPP Server but i am using WAMP Server. And inside that database we are going to create three table. you can give them any name but for our convenient we give the name of these three table as submittedanswer, answer and compare.

And inside each table we are going to create two column i.e. ID is common in all there table and second column name is: sa in submittedanswer table, ans in answer table and value in compare table. We should store correct answer in answer table by ourself.  So when the user click their answer and click submit button, their answer is store in submittedanswer table and at the same time that submitted answer is compare with the previously stored correct answer which is store in answer table and should store either correct or incorrect value in the third table i.e. in compare table. And when the user click the Check Button the compare table value should be display in the message box. So, This is our Basic concept to do this project. So let’s do it.

So let get into the topic, in this tutorial we are gonna learn about many thing during the completion of this small but interesting tutorial.

Steps:

You can see all the below procedure in the Video:

[sociallocker]

1. I am assuming that you have basic knowledge of java programming. I already told you that you have to install server application in your PC. Now start your wampserver, then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want (In my case javaquiz) and the click OK, then we need to create  three table as i mention earlier having two field. So, when you create table new window will automatically ask for creating table. So enter the table name  submittedanswer, answer and compare each time creating new table and then you have to select field, in that field section enter 2 and click go. Then new window will appear, enter the field name like this ID ans sa in submittedanswer table, ID and ans in answer table and finally ID and value in compare table, then filed length like 50,20 etc and select all table ID as A_I that means auto incremented which automatically indicate primary key, and then select data type all ID as integer and second column as Varchar and then click on save. You can watch the video for this designing part, you may understand better in video.

How to Create Quiz Application in java?
How to Create Quiz Application in java?

NOTE: i am giving you the example of creating submittedanswer table. You see the go button on that popup window, click that go button and then click save. Repeat same procedure for other two table.

2. Now open Netbeans IDE and click on file > new project. Select categories as java and project as java application from that window:

How to Create Quiz Application in java?

Click next and then give your project name and then click finished.

3. Now right click on your project and select new > JFrame Form :

How to Create Quiz Application in java?

NOTE: give the jframe name or leave as it is and click OK.

4. After this new window will appear from this window drag Panel and extend it, and drag one label, one Radio Grop, four radio button text area and two Button and drop to the jframe form. Change the text of radiobutton and label by right clicking. After doing this step your jfram should look like this:

How to Create Quiz Application in java?

5. And also right click each radiobutton click properties and then new window will appear; from that window select button group to the the button group name as:

How to Create Quiz Application in java?
6. Declare following variable inside the class your jframe, for this select source from the top of the jframe.
Connection con =null;
ResultSet rs = null;
PreparedStatement pst =null;
Statement st;
String value;
7. Now double click each radio button and write the following code:
value="8848"; //value inside clicking event of radio button1 i.e. inside clicking event of radiobutton1
value="9848"; // inside clicking event of radiobutton2
value="10848";
value="11848";
8. Now double click the Submit button and write the following code:
try{
String submitQuery= "INSERT INTO submittedanswer"
+"(sa)"
+"VALUES (?)";
con = DriverManager.getConnection("jdbc:mysql://localhost/javaquiz", "root","");
pst=con.prepareStatement(submitQuery);
pst.setString(1, value);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "your answer is subitted successfully");
String extractQuery= "select submittedanswer.sa, answer.ans from answer inner join (select sa from submittedanswer order by id desc limit 1) as submittedanswer on submittedanswer.sa = answer.ans";
pst=con.prepareStatement(extractQuery);
rs=pst.executeQuery();
if(rs.next()){

String valueQuery="INSERT INTO compare(value) VALUES (?)";
pst=con.prepareStatement(valueQuery);
pst.setString(1, "Correct");
pst.executeUpdate();
}
else{
String valueQuery= "INSERT INTO compare(value) VALUES (?)";
pst=con.prepareStatement(valueQuery);
pst.setString(1, "Incorrect");
pst.executeUpdate();
}


}
catch( SQLException | HeadlessException ex){
JOptionPane.showMessageDialog(this, ex.getMessage());

}
9. Double click the check button and write the following code:
try{
String showQuery="select value from compare order by id desc limit 1";
con=DriverManager.getConnection("jdbc:mysql://localhost/javaquiz", "root","");
pst=con.prepareStatement(showQuery);
rs=pst.executeQuery();
if(rs.next()){
String show = rs.getString("value");
JOptionPane.showMessageDialog(null, "your submitted answer is "+show,"INFORMATION",JOptionPane.PLAIN_MESSAGE);
}


}catch(HeadlessException | SQLException ex){
JOptionPane.showMessageDialog(this, ex.getMessage());
}
10. Now you need to have MySQL connector for java so download it. Click Here and Add this jar file in your project libraries folder by right clicking libraries >click add jar. Select downloaded jar file and click open.
How to Create Quiz Application in java?

11. To run this program you need to right click on your jframe and click run file as below:

How to Create Quiz Application in java?

 

Now select any answer and click submit. To check your result click check. That’s it guyz. If you have any confusion you can comment you problem in the comment section.

[/sociallocker]

 

If you like this tutorial share this tutorials with your friends or in any java group so that they will know about this tutorial.

 

If you are confused then you can watch the following video:

Show Comments (8)