Rana Rajpoot Rana Rajpoot - 4 months ago 7
Java Question

How can I insert data in mysql using jsp? What's wrong in my jsp code

How can I execute this statement using jsp? What is the error in it how can I remove this error from this

rs=st.executeQuery(Sql);
//This query is in statement2.jsp file.

[enter image description here][1]//This error coming.

statement2.jsp

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.mysql.jdbc.Statement"%>
<%@page import="com.mysql.jdbc.Connection"%>
<%@page import=" java.sql.SQLException" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@include file="db conn.jsp" %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
String name=request.getParameter("first");
String abc=request.getParameter("last");
String cde=request.getParameter("user");
String pass=request.getParameter("password");
String confpass=request.getParameter("confirmpass");
String emails=request.getParameter("email");
String months=request.getParameter("month");
String day=request.getParameter("day");
String year=request.getParameter("year");
String gender=request.getParameter("Gender");

String Sql="insert into teacher2(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) values('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";

rs=st.executeQuery(Sql); //`Why this statement is not executed`

if(rs.next())
{
response.sendRedirect("reg.jsp");
}
else
{
response.sendRedirect("index.jsp");
}
%>
</body>
</html>





db conn.jsp

<%@page import="com.mysql.jdbc.Connection"%>
<%@page import="com.mysql.jdbc.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>

<body>
<h1>Hello World!</h1>

<%@ page import ="java.sql.*" %>
<%

Connection c1 = null;
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
{
System.out.println("Couldn't find the driver!");
System.out.println("Couldn't connect: print out a stack trace and exit.");
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");

st = (Statement) c1.createStatement();
System.out.println("Statement Created Successfully");
{
System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
}
if (c1!= null) {
System. out.println("Hooray! We connected to the database!");
} else {
System.out.println("We should never get here.");
}}
%>





index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@include file="db conn.jsp" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="GET " action="reg.jsp" autocomplete="on">
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" >Username:</label>
<input id="username" name="username" required="required" type="text" placeholder="Username"/>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p">Password:</label>
<input id="password" name="password" required="required" type="password" placeholder="Password" />
</p>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<p class="login button">
<input type="submit" value="Login" />
<input type="button" onclick="location.href='reg.jsp';" value="Sign Up"/>
</p>

<input type="radio" name="gender" value="Teach" checked/> Teacher
<input type="radio" name="gender" value="Student"/>Student
</form>
</body>
</html>

Answer

As the error message says, st.executeQuery(Sql) is only for queries which reads data. For Data manipulation (insert/update/delete), you have to use st.executeUpdate(Sql)