Please follow given guidelines before using this code
Note: Using these codes the admin can create a SUBJECT and later on admin can add a QUESTION in the subject (as per the admin need)
Note: Using these codes the admin can create a SUBJECT and later on admin can add a QUESTION in the subject (as per the admin need)
A. Create a database in MySQL server
>> create database admin;
B. Create a Dynamic Web Project in Eclipse IDE and following some jsp files inside the Web Content folder.
Names of jsp must be following
1. admin.jsp
2. add_subject.jsp
3. add_subject_handler.jsp
4. add_questions.jsp
5. add_question_handler.jsp
C. Copy and paste mysqlconnector.jar inside lib folder (lib is inside WEB-INF folder)
<< code for admin.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<ul>
<li><a href="add_subject.jsp">To add a subject click here</a></li>
<li><a href="add_questions.jsp">To add a question click here</a></li>
</ul>
</body>
</html>
4. add_questions.jsp
5. add_question_handler.jsp
C. Copy and paste mysqlconnector.jar inside lib folder (lib is inside WEB-INF folder)
<< code for admin.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<ul>
<li><a href="add_subject.jsp">To add a subject click here</a></li>
<li><a href="add_questions.jsp">To add a question click here</a></li>
</ul>
</body>
</html>
<< code for admin.jsp ends >>
<< code for add_subject.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<fieldset>
<legend>Add-Subject</legend>
<form action="add_subject_handler.jsp" method="post">
Input the title for subject<br>
<input type="text" name="subject" required><br>
<input type="submit" value="add-subject">
</form>
</fieldset>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String subject = request.getParameter("subject");
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
String sql = "create table admin."+subject+"(id int,question varchar(255),answer1 varchar(255),"+
"answer2 varchar(255),answer3 varchar(255),answer4 varchar(255),correct varchar(255), primary key(id))";
System.out.println(">>>>"+sql);
int nora = st.executeUpdate(sql);
if(nora >= 0)
{
out.println("<i style='color : blue'>subject has been saved</i>");
out.println("<a href=''>show dashboard</a>");
out.println("<a href='add_questions.jsp'>add a question</a>");
}
%>
</body>
</html>
<< code for add_subject_handler.jsp ends >>
<< code for add_questions.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
Statement st2 = co.createStatement();
String sql = "show tables in admin";
ResultSet rs = st.executeQuery(sql);
ResultSet rs2 = st2.executeQuery(sql);
int records = 0;
while(rs.next())
{
records++;
}
String[] tables = new String[records];
int i = 0;
while(rs2.next())
{
tables[i] = rs2.getString(1);
i++;
}
co.close();
%>
<fieldset>
<legend>Add-Question</legend>
<form action="add_question_handler.jsp" method="post">
<select name="subject">
<%
for(int index = 0; index < tables.length; index++)
{
%>
<option><%=tables[index]%></option>
<%
}
%>
</select>
<br>
Input a question id<br>
<input type="text" name="id" required><br>
Input a question<br>
<textarea rows="3" cols="40" name="question"></textarea><br>
Input answer one<br>
<textarea rows="3" cols="40" name="ans1"></textarea><br>
Input answer two<br>
<textarea rows="3" cols="40" name="ans2"></textarea><br>
Input answer three<br>
<textarea rows="3" cols="40" name="ans3"></textarea><br>
Input answer four<br>
<textarea rows="3" cols="40" name="ans4"></textarea><br>
Input correct answer<br>
<textarea rows="3" cols="40" name="correct_ans"></textarea><br>
<input type="submit" value="add-a-question">
</form>
</fieldset>
</body>
</html>
<< code for add_questions.jsp ends >>
<< code for add_subject.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<fieldset>
<legend>Add-Subject</legend>
<form action="add_subject_handler.jsp" method="post">
Input the title for subject<br>
<input type="text" name="subject" required><br>
<input type="submit" value="add-subject">
</form>
</fieldset>
</body>
</html>
<< code for add_subject.jsp ends >>
<< code for add_subject_handler.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String subject = request.getParameter("subject");
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
String sql = "create table admin."+subject+"(id int,question varchar(255),answer1 varchar(255),"+
"answer2 varchar(255),answer3 varchar(255),answer4 varchar(255),correct varchar(255), primary key(id))";
System.out.println(">>>>"+sql);
int nora = st.executeUpdate(sql);
if(nora >= 0)
{
out.println("<i style='color : blue'>subject has been saved</i>");
out.println("<a href=''>show dashboard</a>");
out.println("<a href='add_questions.jsp'>add a question</a>");
}
%>
</body>
</html>
<< code for add_subject_handler.jsp ends >>
<< code for add_questions.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
Statement st2 = co.createStatement();
String sql = "show tables in admin";
ResultSet rs = st.executeQuery(sql);
ResultSet rs2 = st2.executeQuery(sql);
int records = 0;
while(rs.next())
{
records++;
}
String[] tables = new String[records];
int i = 0;
while(rs2.next())
{
tables[i] = rs2.getString(1);
i++;
}
co.close();
%>
<fieldset>
<legend>Add-Question</legend>
<form action="add_question_handler.jsp" method="post">
<select name="subject">
<%
for(int index = 0; index < tables.length; index++)
{
%>
<option><%=tables[index]%></option>
<%
}
%>
</select>
<br>
Input a question id<br>
<input type="text" name="id" required><br>
Input a question<br>
<textarea rows="3" cols="40" name="question"></textarea><br>
Input answer one<br>
<textarea rows="3" cols="40" name="ans1"></textarea><br>
Input answer two<br>
<textarea rows="3" cols="40" name="ans2"></textarea><br>
Input answer three<br>
<textarea rows="3" cols="40" name="ans3"></textarea><br>
Input answer four<br>
<textarea rows="3" cols="40" name="ans4"></textarea><br>
Input correct answer<br>
<textarea rows="3" cols="40" name="correct_ans"></textarea><br>
<input type="submit" value="add-a-question">
</form>
</fieldset>
</body>
</html>
<< code for add_questions.jsp ends >>
<< code for add_question_handler.jsp starts >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String subject = request.getParameter("subject"),
ques = request.getParameter("question"),
a1 = request.getParameter("ans1"),
a2 = request.getParameter("ans2"),
a3 = request.getParameter("ans3"),
a4 = request.getParameter("ans4"),
ca = request.getParameter("correct_ans");
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
String sql = "insert into admin."+subject+" values('"+id+"','"+ques+"','"+a1+"','"+a2+"','"+a3+"','"+a4+"','"+ca+"')";
int rec = st.executeUpdate(sql);
if(rec > 0)
{
out.println("<i>question added</i>");
out.println("<a href='add_questions.jsp'>add more questions</a>");
}
co.close();
%>
<< code for add_question_handler.jsp ends >>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String subject = request.getParameter("subject"),
ques = request.getParameter("question"),
a1 = request.getParameter("ans1"),
a2 = request.getParameter("ans2"),
a3 = request.getParameter("ans3"),
a4 = request.getParameter("ans4"),
ca = request.getParameter("correct_ans");
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/admin","root","root");
Statement st = co.createStatement();
String sql = "insert into admin."+subject+" values('"+id+"','"+ques+"','"+a1+"','"+a2+"','"+a3+"','"+a4+"','"+ca+"')";
int rec = st.executeUpdate(sql);
if(rec > 0)
{
out.println("<i>question added</i>");
out.println("<a href='add_questions.jsp'>add more questions</a>");
}
co.close();
%>
<< code for add_question_handler.jsp ends >>
sr bahut acha or clean code kia h.
ReplyDeleteAllah aap ko tarakki de ...or hme aapki duao m shamil hone ka moka de.
AWESOME SIR,HATS OFF SIR THIS SHOWS YOUR BRAIN CONTROL ON JAVA.
ReplyDelete