very recently i started learning Java Technology right now I'm working on Servlets while i fetch the data from data base it's not resolving resultset rs
here I attached my code
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">
javascript:window.history.forward(1);
</script>
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires","0");
response.setDateHeader("Expires",-1);
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
update
display
logout
<script>
<%
try{
ResultSet rs=null;
String mail=(String)session.getAttribute("mail");
String sql="select * from tempregister where mail=?";
Class.forName("com.ibm.db2.DB2Driver");
Connection con=DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, mail);
rs=ps.executeQuery();
}catch(Exception e){
}
%>
<%
while(rs.next())
{
%>
<%=rs.getString(4)%>
<%
}
}
catch(Exception e)
{
e.printStackTrace();
}%>
</script>
</a>
</body>
</html>
please help me out.. thanks
Your code should be changed at least:
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">
javascript:window.history.forward(1);
</script>
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires","0");
response.setDateHeader("Expires",-1);
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
update
display
logout
<%
ResultSet rs = null;
try {
String mail = (String) session.getAttribute("mail");
String sql = "select * from tempregister where mail=?";
Class.forName("com.ibm.db2.DB2Driver");
Connection con = DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, mail);
rs = ps.executeQuery();
while (rs.next()) {
%>
<%=rs.getString(4)%>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
Related
I have written simple jsp program where I am trying to retrieve username and password from database, here is my jsp program.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import ="java.sql.*" %>
<%# page import ="oracle.sql.*" %>
<%# page import ="oracle.jdbc.driver.*" %>
<%#page import="oracle.jdbc.driver.OracleDriver"%>
<%# page import ="java.util.Date" %>
<!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 driver="oracle.jdbc.driver.OracleDriver";
String userid = request.getParameter("username");
String pwd=request.getParameter("password");
Connection con = null;
%>
<%try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:CCB25", "CISADM", "CISADM");
Statement st= con.createStatement();
String query="select * from CMLOGIN where USERID='"+userid+"'";
ResultSet rs=st.executeQuery(query);
String pass1="";
rs.next();
pass1 = rs.getString("password");
if(pass1.equals(pwd))
{
%>
<%String name=request.getParameter("username");
session.setAttribute("nam",name);%>
<jsp:forward page="admin.jsp"></jsp:forward>
<%}
else
{
String msg="Username or password failed";%>
<center> <p style="font-family:verdana;color:red;"><%=msg %>
<jsp:include page="Login.jsp"></jsp:include>
<%}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
and i have javax.servlet.jar and ojdbc7-12.1.0.2.jar in web-inf/lib folder. I am getting java.sql.SQLException: Result set after last row on the following line "pass1 = rs.getString("password");" Could you please guide me what I am doing wrong.
You first need to check if the ResultSet has any rows in it before calling .next(), in order to prevent this exception.
Use this:
if (rs.next){
//read next row
}
if you want to loop through all the rows of the ResultSet, use a while loop:
while(rs.next){
//do something for every row
}
Here is the same cleaned up a little. You'll have to add back in exception handling and closing of the connection/statement/resultset
Hopefully you never have to use this code for real as it's taking in a password from query string in clear then comparing to a clear version in the db.
Password comparison moved to the sql itself.
Always use PreparedStatements to avoid sql injection
Comments are in the code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import ="java.sql.*" %>
<%# page import ="java.util.Date" %>
<!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 userid = request.getParameter("username");
String pwd = request.getParameter("password");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521/XE", "klrice", "klrice");
//
// USE BINDS ! to avoid sql injection
// push the userid and passwd comparison to the db
// no need to get the password and compare locally
//
String query = "select password from cmlogin where userid=? and password=?";
PreparedStatement st = con.prepareStatement(query);
//
// Binds in the vaules
//
st.setString(1,userid);
st.setString(2,pwd);
ResultSet rs = st.executeQuery();
String pass1;
// .next will advance if the query has any results
//
if ( rs.next() ) {
pass1 = rs.getString("password");
String name =request.getParameter("username");
session.setAttribute("nam",name);
%>
<jsp:forward page="admin.jsp"></jsp:forward>
<%
} else {
String msg="Username or password failed";
%>
<center> <p style="font-family:verdana;color:red;"> <%=msg %>
<jsp:include page="Login.jsp"></jsp:include>
<% } %>
</body>
the following jsp code returns just a blank screen on web browser... what to do?
<%#page import="java.sql.*"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GAURAV GOSWAMI</title>
<%
try
{
Class.forName("java.sql.DriverManager");
Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
Statement stmt = con.createStatement();
String query = "select * from qa";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
%>
<p><br><%rs.getString(0);%></p>
<p><br><%rs.getString(1);%></p>
<p><br><%rs.getString(2);%></p>
<p><br><%rs.getString(3);%></p>
<p><br><%rs.getString(4);%></p>
<%
}
}
catch(Exception e)
{
%>
<br><%e.getMessage();%>
<%
}
%>
</head>
</html>
You're writing the body in the head section.
Move your code in the body section, like following:
<%#page import="java.sql.*"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GAURAV GOSWAMI</title>
</head> <!-- Add this line -->
<body> <!-- Add this line -->
<%
try
{
Class.forName("java.sql.DriverManager");
Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
Statement stmt = con.createStatement();
String query = "select * from qa";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
%>
<p><br><%rs.getString(0);%></p>
<p><br><%rs.getString(1);%></p>
<p><br><%rs.getString(2);%></p>
<p><br><%rs.getString(3);%></p>
<p><br><%rs.getString(4);%></p>
<%
}
}
catch(Exception e)
{
%>
<br><%e.getMessage();%>
<%
}
%>
</body> <!-- Add this line -->
<!-- **** </head> *** Remove this line -->
</html>
I hope it helps you, bye.
PS: I've marked with comments like <!-- Add this line --> the lines to change.
Besides Alessandro's answer(do not write content into head),the jdbc driver you wrote is also incorrect
You need to use com.mysql.jdbc.Driver instead.
So change
Class.forName("java.sql.DriverManager");
to
Class.forName("com.mysql.jdbc.Driver");
The code shows me the output as "com.mysql.jdbc.Driver"
Is it because of the database or something? I am using JSP here and trying to verify details of users.
<%#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>
<%#page import="java.sql.*"%>
<%#page import="javax.sql.*" %>
<%
try{
String username=request.getParameter("user");
String password=request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/tyit1", "root", "sphinx1");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+username+"' and password='"+password+"'");
if(rs.next())
{
String user=rs.getString("username");
String pass=rs.getString("password");
}
else{
out.print("Invalid Credentials!");
}
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
</body>
</html>
I'm trying to upload my jsp project to tomcat.
Under picture is structure of my jsp project.
and when in insert.html's function trying to use insert.jsp, under error evoke.
this code is only insert data to mysql server.
DBConnection.java :
package com.exam;
import java.sql.*;
public class DBConnection {
public static Connection getCon() throws SQLException {
// TODO Auto-generated method stub
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/testdb";
con=DriverManager.getConnection("url", "root", "kcclab");
System.out.println("DB 연결 완료");
con.close();
return con;
}
catch(ClassNotFoundException cnfe){
System.out.println("연결이 안되네요..."+cnfe.getMessage());
return null;
}
}
}
insert.jsp :
<%#page import="java.sql.SQLException" %>
<%#page import="java.sql.PreparedStatement" %>
<%#page language="java" contentType="text/html; charset=euc-kr"
pageEncoding="euc-kr" %>
<%#page import="java.sql.Connection" %>
<%#page import="com.exam.DBConnection" %>
<!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=euc-kr">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("euc-kr");
String id=request.getParameter("ID");
String pwd=request.getParameter("pwd");
Connection con=null;
PreparedStatement pstmt=null;
String sql="insert into members vlaues(?,?,sysdate)";
int n=0;
try{
con=DBConnection.getCon();
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
n=pstmt.executeUpdate();
}
catch(SQLException se){
System.out.println(se.getMessage());
}
finally{
try{
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
}
catch(SQLException se){
System.out.println(se.getMessage());
}
}
%>
<script type="text/javascript">
if(<%=n%>>0){
alert("���������� ȸ�����ԵǾ����ϴ�.");
location.href="../index.html";
}
else{
alert("ȸ�����Կ� �����߽��ϴ�.");
history.go(-1);
}
</script>
</body>
</html>
insert.html
<!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=euc-kr">
<title>Insert title here</title>
<style type="text/css">
#regbox{width:300px;}
#regbox label{display:block; width:100px; float:left; }
</style>
</head>
<body>
<form method="post" action="insert.jsp">
<fieldset id="regbox">
<legend>ȸ������</legend>
<label for="id">���̵�</label>
<input type="text" name="id"/><br/>
<label for="pwd">��й�ȣ</label>
<input type="password" name="pwd"/><br/>
<input type="submit" value="����">
<input type="reset" value="���"/>
</fieldset>
</form>
</body>
</html>
How Can I avoid this error?
As per my understanding this may happen when you are trying to import the class which is not present under the class path.
Can you please have a look at on your tomcat webapps directory ??
The file DBConnection.class just may not be present under WEB-INF/classes folder.
This is what i have, when i run the jsp file on the browser the only thing i get is the "Report" header but not the query result, please guide me to the right way.
<%#page import="java.sql.ResultSet"%>
<%#page import="database.Dba"%>
<%#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>
<center> <h1> Report </h1></center>
<%
//scriplet
try{
Dba db =
new Dba(application.getRealPath("daw.mdb"));
db.conectar();
db.query.execute("select usuario from usuarios");
ResultSet rs = db.query.getResultSet();
while(rs.next()){
out.print(rs.getString(1)+ "<br>");
}
db.desconectar();
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>