Converting request.getParameterValues() to int array - java

I tried looking for help on the web to solve my problem but to no avail. I wish to convert verify[] to int, so it can be processed in the query.
verified column has a data type of string
staff_id column has a data type of autonumber
I'm getting an error of
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
VerifyStaff.jsp
<!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=utf-8">
<title>Verification of Staff Accounts</title>
</head>
<body>
<div align="center">
<%-- Imports --%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%-- HTTP header --%>
<%response.addHeader("Cache-Control","no-cache");
response.addHeader("Pragma","no-cache");
response.addHeader("Expires","0");
%>
<%-- Retrieving Staff Accounts - Reading --%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conURL= "jdbc:odbc:HOD_DATA";
Connection con = DriverManager.getConnection(conURL);
Statement st = con.createStatement();
String query = "select staff_id, username, password, user_group, verified from Staff";
ResultSet rs = st.executeQuery(query);
%>
<form action="VerifyStaffAuth.jsp">
<table width="200" border="1">
<tr>
<td>Staff_ID</td>
<td>Username</td>
<td>Password</td>
<td>User Group</td>
<td>Verified?</td>
<td>Verify/Un-verify</td>
</tr>
<%
while(rs.next()){
int staff = rs.getInt("staff_id");
%>
<tr>
<td><%= staff %></td>
<td><%= rs.getString("username") %></td>
<td><%= rs.getString("password") %></td>
<td><%= rs.getString("user_group") %></td>
<td><%= rs.getString("verified") %></td>
<td><label>
<input type="checkbox" name="CheckboxGroup" value="<%= staff %>">
</label></td>
</tr>
<%
}
rs.close();
st.close();
con.close();
}
catch(Exception e){
out.println(e);
}
%>
</table>
<input type="submit" VALUE="submit">
</form>
</div>
</body>
</html>
VerifyStaffAuth.jsp
<!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=utf-8">
<title></title>
</head>
<body>
<%-- Imports --%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%-- HTTP header --%>
<%response.addHeader("Cache-Control","no-cache");
response.addHeader("Pragma","no-cache");
response.addHeader("Expires","0"); %>
<% String[] verify = request.getParameterValues("CheckboxGroup");
int[] verify2 = new int[verify.length];
for(int i=0;i<verify.length;i++){
verify2[i]=Integer.parseInt(verify[i]);
}
if(verify != null){
for(int i=0; i<verify.length; i++){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conURL= "jdbc:odbc:HOD_DATA";
Connection con = DriverManager.getConnection(conURL);
Statement st = con.createStatement();
int status = st.executeUpdate("update Staff set verified = 'yes' where Staff_id = '"+verify[i]+"'");
if(status>0){
//response.sendRedirect("SuccessfulReg1.html");
}
else{
//out.println("Update unsuccessful");
}
st.close();
con.close();
}
catch(Exception e){
out.println(e);
}
}
}
%>
</body>
</html>

Change your UPDATE query to
st.executeUpdate("update Staff set verified = 'yes' where Staff_id = " + verify[i]);
Since, staff_id column has a data type of autonumber, its value should not be specified in quotes since it's numeric in nature.
I would also suggest you to use the SQL IN clause and fire just one single UPDATE instead of firing it multiple times, one for each staff_id value.
Your UPDATE query with IN clause should look something like
UPDATE staff SET verified = 'yes' WHERE staff_id IN (1, 2, 3)
Use Arrays.toString() to get the comma-delimited string as
String inValues = Arrays.toString(verify); // "[1, 2, 3]"
inValues = inValues.substring(1, inValues.length() - 1)); // "1, 2, 3"

Related

how to write td in side the jsp code . I had written the but result merged

<%#page import="java.util.List"%>
<%# 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>
<%# page import="java.sql.*" %>
<%# page import= "myExam.quest" %>
</head>
<body>
<%! int index=0; %>
<%! int pos=0; %>
<% String c,a;
String[] CA =(String[])session.getAttribute("CA");
String[] AS =(String[])session.getAttribute("AS");
List<quest> l=(List<quest>)session.getAttribute("myquestion");
quest[] question = new quest[l.size()];
l.toArray(question);
%>
<table border=1>
<tr>
<th colspan="2" width="500">Questions</th>
<th>CorrectAnswer</th>
<th>Answer sheet</th>
</tr>
<tr>
<%
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>
<td>Question <%=pos+1%></td>
<td width="500"><%= ques %></td>
<td><%=c %></td>
<td><%= a%>
</tr>
</table>
<%} %>
</body>
</html>
I had written every thing is write please ignored the session
the output come is
output of the program
only the first line comes in the table format
please suggest some work to perform or how to modify it
It is a very bad practice to write code using scriplets. You should avoid using scriplets. Anywayas, try with below code:
Insert title here
<% String c,a;
String[] CA =(String[])session.getAttribute("CA");
String[] AS =(String[])session.getAttribute("AS");
List<quest> l=(List<quest>)session.getAttribute("myquestion");
quest[] question = new quest[l.size()];
l.toArray(question);
%>
<table border=1>
<tr>
<th colspan="2" width="500">Questions</th>
<th>CorrectAnswer</th>
<th>Answer sheet</th>
</tr>
<%
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>
<tr><!-- moved tr inside for loop-->
<td>Question <%=pos+1%></td>
<td width="500"><%= ques %></td>
<td><%=c %></td>
<td><%= a%></td><!-- added missing /td-->
</tr>
<%} %>
</table><!-- moved /table outside of for loop-->
</body>
Problem is your table html is not well nested within the loop.
<%
for(pos=0;pos<question.length;pos++){
String ques=question[pos].ques;
String opa=question[pos].opa;
String opb=question[pos].opb;
String opc=question[pos].opc;
String opd=question[pos].opd;
c=CA[pos];
a=AS[pos];
%>
<tr> <!-- moved inside -->
<td>Question <%=pos+1%></td>
<td width="500"><%=ques%></td>
<td><%=c%></td>
<td><%=a%></td> <!-- added missing closing tag -->
</tr>
<%} %>
</table><!-- moved outside -->
P.S. Also consider using JSTL Core tags for such kind of implementation.

response.sendRedirect in JSP

This is my current JSP code:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<jsp:useBean id="user" class= "uts.wsd.User" scope="session" ></jsp:useBean>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String color = request.getParameter("favcol");
user.setName(name);
user.setEmail(email);
user.setPassword(password);
user.setGender(gender);
user.setFavouriteColour(color);
%>
<body style="background: <%= color %>;">
<% if (request.getParameter("tos") == null ) {%>
<p>
Sorry, you must agree to the Terms of Service.</p>
<p>Click <a href="register.jsp" > here </a> to go back.
</p>
<%} else { %>
<jsp:forward page="index.jsp" />
<% } %>
</html>
Here I use jsp:forward page="index.jsp" to redirect to index.jsp page. Then, if I want to use response.sendRedirect("index.jsp")? How can I proceed?
I tried this:
<% if (request.getParameter("tos") == null ) {%>
<p>
Sorry, you must agree to the Terms of Service.</p>
<p>Click <a href="register.jsp" > here </a> to go back.
</p>
<%} else { %>
<response.sendRedirect("index.jsp")>
<% } %>
</html>
But it failed. Please help! Thank you!!
response.sendRedirect() is Java code not a tag, so you should not close the scriptlet tags before typing it, and it is not to be preceded by < and closed with >...its just Java code:
<%
}
else
{
response.sendRedirect("index.jsp");
return; //this is to redirect immediately so it doesn't
//run any code below this point before redirecting
}
%>

User name is not showing in web page from database?

I've made a login script in JSP. It's working fine but the problem as i use
<%=session.getAttribute("first_name")%> it shows Null. Can you please tell me what's wrong here?
Main.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
if ((session.getAttribute("user_email") == null) || (session.getAttribute("user_email") == "")) {
response.sendRedirect("login.jsp");
%>
<%} else
%>
<!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=UTF-8">
<title>Welcome to the job seeker forum.</title>
<link rel="stylesheet" type="text/css" href="CSS_styles/forum_style.css"/>
<link rel='stylesheet' id='open-sans-css' href='//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&subset=latin%2Clatin-ext&ver=4.1-alpha-20141011' type='text/css' media='all' />
<link href='http://fonts.googleapis.com/css?family=Londrina+Solid|Exo:800|Open+Sans:300italic,400,300,600|Roboto:400,100,300,500,700' rel='stylesheet' type='text/css' />
</head>
<body>
<div id="forum">
<h1><b>Welcome <%=session.getAttribute("first_name")%></b>,<br>to Job seeker forum, there are many openings that you can get through here. <a href='logout.jsp'>logout</a></h1>
<form id="forum_form" method="post">
</form>
</div><br><br><br>
<div id="forum2">
<h1 style="text-align: center;">Want to post on going walking,<br>Post your job description here.</h1>
<form id="forum2_form" method="post">
<p>
<label>Job title:</label>
<input type="text" class="" placeholder="e.g XXX Referral program for freshers."/>
</p>
<p>
<label>Your Question:</label>
<textarea class="question_ask_style" rows="3" cols="40" placeholder="Type description here.."></textarea>
</p>
<div id="submit_btn">
<input type="submit" value="Submit Question" />
</div>
</form>
</div>
</body>
</html>
Here is login_process.jsp
<%# page import="java.sql.*" %>
<%
String user_email=request.getParameter("user_email");
String user_password=request.getParameter("user_password");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:8888/login", "root", "1234");
Statement st=con.createStatement();
ResultSet rs;
rs=st.executeQuery("select * from members where email='"+user_email+"' and password='"+user_password+"'");
if(rs.next()){
session.setAttribute("user_email", user_email);
response.sendRedirect("main.jsp");
}else{
out.println("Invalid Password... Try again.. <a href='login.jsp'>login</a>");
}
%>
Here is my created database table:
create table `members`(
`id` int(10) unsigned NOT NULL auto_increment,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
PRIMARY KEY(`id`)
)ENGINE= InnoDB default charset=latin1;
This is what i'm getting error...
SCREENSHOT:
http://i.stack.imgur.com/AuF5M.png
<%=session.getAttribute("first_name")%> shows null because you have not set the first_name in the session.
You have set the only the user_email.
if(rs.next()){
String user_email=rs.getString("first_name");
session.setAttribute("user_email", user_email);
response.sendRedirect("main.jsp");
you can set like the above
P.S
Scriptlets are discouraged over the decades , so it is advised to use EL instead of them . You can simply use , ${sessionScope.user_email} to print it out in the jsp page
In login_process.jsp use <%=session.setAttribute("first_name",first_name)%> and then access it.
eg:
rs=st.executeQuery("select * from members where email='"+user_email+"' and password='"+user_password+"'");
if(rs.next()){
session.setAttribute("first_name", rs.getString("first_name"));//set first_name in session
session.setAttribute("user_email", user_email);
response.sendRedirect("main.jsp");
}
Try this
if(rs.next()){
session.setAttribute("user_email", user_email);
session.setAttribute("first_name", rs.getString(4));//Add this line to your code
response.sendRedirect("main.jsp");
}
Here 4 is column number for first_name
in login_process.jsp set
session.setAttribute("user_email", user_email);
to
session.setAttribute("first_name", rs.getString("first_name"));
because you have set the attribute name user_email and getting first_name from session.

Prevent SQL statement from executing on JSP page load

How can I have a SQL statement that updates data when a button is clicked, not execute when the page loads?
I have a very simple table that has a player name, a player score, and player id attributes and only one row. When I am on this page, I want the button to execute the update query to increase player score, but it's happening every time the page loads.
Is there a boolean or something I can set to prevent it from adding 1 to the player score on every load?
Please don't ask why I'm using a JSP in this context. It's bad to do, I know, but these are my requirements.
Here is my code:
<%# page contentType="text/html" %>
<%# page import="java.text.DecimalFormat" %>
<%# page import="java.sql.Connection" %>
<%# page import="java.sql.DriverManager" %>
<%# page import="java.sql.ResultSet" %>
<%# page import="java.sql.Statement" %>
<%# page import="java.lang*" %>
<!DOCTYPE html>
<html>
<head>
<title>Player scores</title>
<script>
function reloadPage() {
location.reload();
}
</script>
</head>
<body>
<%!
String name = "";
Integer score = 0;
Integer pID = 0;
%>
<form name="form1" method="POST" onsubmit="return false">
<p align="center">
<input type="BUTTON" value="+" onclick="reloadPage();"/>
</p>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
Statement stmnt = conn.createStatement();
stmnt.executeUpdate("update players set playerScore=playerScore+1 where playerID=1");
} catch (Exception e) {
}
%>
</form>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
Statement stmnt = conn.createStatement();
ResultSet rs = stmnt.executeQuery("select playerID, playerName, playerScore from players where playerID=1");
if (rs.next()) {
name = rs.getString("playerName");
score = rs.getInt("playerScore");
pID = rs.getInt("playerID");
} else {
name = "-";
score = 0;
pID = 404;
}
} catch(Exception e) {
}
%>
<table align="center">
<tr>
<th>Player name:</th>
<td id="pID"><%=name%></td>
</tr>
<tr>
<th>Player score:</th>
<td id="pN"><%=score%></td>
</tr>
<tr>
<th>Player id:</th>
<td id="pi"><%=pID%></td>
</tr>
<p class="center">
Go back
</p>
</table>
</body>
</html>
The update code always gets executed because you have your code written that way.
This code:
<form name="form1" method="POST" onsubmit="return false">
<p align="center">
<input type="BUTTON" value="+" onclick="reloadPage();"/>
</p>
that calls:
<script>
function reloadPage() {
location.reload();
}
</script>
is just a refresh with no extra parameters. You always perform a GET.
You need to tell the JSP to do something different when you click the + button. You might try something like this (please have a look at this tutorial first):
<form name="form1" method="POST" action="yourJSPWhateverIsCalled.jsp">
<p align="center">
<input type="submit" value="+" name="increaseScore" />
</p>
Then guard your update code with an if:
<%
if (request.getParameter("increaseScore") != null) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
Statement stmnt = conn.createStatement();
stmnt.executeUpdate("update players set playerScore=playerScore+1 where playerID=1");
} catch (Exception e) {
}
}
%>
Class.forName is needed only once and you could reuse the connection to create the statements but that's another story. Also, a JSP isn't the place to postprocess a form submit (use a plain vanilla servlet).
One other problem with your code is that the JSP is thread UNsafe because of this definition:
<%!
String name = "";
Integer score = 0;
Integer pID = 0;
%>
If you use <%! the code is placed at servlet class level when your JSP gets translated to a servlet and not inside the _jspService method where it belongs. You are basically adding state to your JSP which is thread unsafe because the servlet container can reuse the same servlet instance to handle multiple requests.
Finally, I would strongly suggest you to read a JSP tutorial before continuing. An official tutorial is here.

how I can connect HTML to java to add values in database [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to enter data in database entered from jsp file and dont know how to connect them. Can any one suggest me to connect both files and to add the data entered in jsp form ?
This is my jsp and java files...
test1.java
package P1;
import java.sql.*;
class test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection con = null;
try {
Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection("jdbc:oracle:thin:#192.168.106.87:1521:ORA11G","fuel_db","foel");
Statement statement = con.createStatement();
String command = "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";
statement.executeUpdate(command);
con.close();
}
}
test1.html
**
<%# 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>FORM</title>
<script type="text/javascript">
<%
String name = request.getParameter("name");
String roll = document.getElementById("rollno");
String clas = document.getElementById("class");
String mobile = document.getElementById("mobileno");
test1 myTest = new test1();
myTest.submitData();
%>
function getvalues()
{
var name = document.getElementById("name");
var roll = document.getElementById("rollno");
var clas = document.getElementById("class");
var mobile = document.getElementById("mobileno");
}
function num(e)
{
var k;
document.all ? k = e.keyCode : k = e.which;
return (!((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8));
}
</script>
</head>
<body>
<form action="test1.java" method="post" >
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="name" maxlength="10"></td>
</tr>
<tr>
<td>roll:</td>
<td><input type="text" name="rollno" maxlength="5" onkeypress="return num(event)"></td>
</tr>
<tr>
<td>class:</td>
<td><input type="text" name="class" maxlength="10"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobileno" maxlength="10" onkeypress="return num(event)"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" onclick="getvalues()"></td>
</tr>
</table>
</form>
</body>
</html>
**
I won't go by your code. But the example here will be enough.
The standard way of passing/submitting data to the server in the pure Servlets/JSP world is by using HTML form, i.e. the same way as when use other server side languages for example php. And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST or GET.
Its standard way to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet.
for example:
<form name="something" method="post" action="<servlet-name>"> //if u want to change the action to something else then u need to modify your xml file.
<input type="text" name="username"/>
<input type="submit" name="submitit" value="submited"/>
</form>
now in the servlet under the doPost(...) write
if(request.getParameter("submitit").equals("submitted")){
String username=request.getParameter("username");
//now u can run a query and insert ito to database;
}
in the end you can redirect it another page with
`response.sendRedirect();`
or any other way
may i assume that you are using eclipse Java EE ide for development. then u need not worry about integrating them, eclipse would prepare the xml files for you once you create a new Java EE project. and if not then u have to do it manually, i once tried to do it, but I couldn't succeed.
here is a link: that would interset you, i hope: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
this is bad thing, but i will edit the code for you. by the way, i am removing the javascript. KISS (keeping it simple silly).. :)
ur jsp page would be:
<%# 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>FORM</title>
</head>
<body>
<form action="test1" method="post" >
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="name" maxlength="10"></td>
</tr>
<tr>
<td>roll:</td>
<td><input type="text" name="rollno" maxlength="5"></td>
</tr>
<tr>
<td>class:</td>
<td><input type="text" name="class" maxlength="10"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobileno" maxlength="10"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
and the servlet will be:
import java.sql.*;
class test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection("jdbc:oracle:thin:#192.168.106.87:1521:ORA11G","fuel_db","foel");
String name = request.getParameter("name");
String roll = document.getElementById("rollno");// idk why roll no is string
String clas_s = document.getElementById("class");
String mobile = document.getElementById("mobileno");
try {
String query= "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";
pstmt = con.prepareStatement(query);
pstmt.setString(1,name);
pstmt.setString(2,roll);
pstmt.setString(3,clas_s);
pstmt.setString(4,mobile);
pstmt.executeUpdate();
con.close();
}
catch(Exception e) {
e.printStackTrace();}
response.sendRedirect("confirm.jsp");
}
}
donot ask me about the braces.. fix it yourself.
First create some method like submitData(data) in your Java class.
public class test1 {
public void submitData(String name,String rollno,String classData,String mobileno) throws SQLException, ClassNotFoundException {
Connection con = null;
try {
Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection("jdbc:oracle:thin:#192.168.106.87:1521:ORA11G","fuel_db","foel");
Statement statement = con.createStatement();
String command = "INSERT INTO student (name, rollno, class, mobileno) VALUES (" + name + "," + rollno + "," + classData + "," + mobileno + ");";
statement.executeUpdate(command);
con.close();
}
}
In your HTMl page index.html or index.jsp, you need to put a form and then post it to JSP page which has the logic I have mentioned below.
<FORM NAME="form1" ACTION="ProcessData.jsp" METHOD="POST">
In your JSP page you may get data when a form is submitted using POST method. Get all those variables using request.getParameter("name")
Then in your JSP put that java code in <% %> blocks inside body tag. Remember JSP is Java in HTML!
In your ProcessData.jsp
<%
String name = request.getParameter("name");
//add null checks and all
//Similarly get all datamobileno etc
//then call your submitData() method
test1 myTest = new test1();
myTest.submitData(....)
%>
Also take care of naming conventions.
In java
Classes name start with Caps. So class name must be Test1 and not test1.
Functions, variables must be in Camel case like myTest.

Categories

Resources