Incorrect output,shows Class.forName as output - java

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>

Related

jsp isn't returning anything, just a blank screen

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");

Resultset rs cannot be resolved

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>

Unable to compile class for JSP error tomcat,mysql

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.

Access DB query not showing in Apache, here's the scriplet in Netbeans i have

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>

How to redirect the code below after delete to a previours web page?

After the delete below how can this page be redirected to the previours page and do a refresh to not show the data deleted?
I tried some options like:
<%
String redirectURL = "Index.jsp";
response.sendRedirect(redirectURL);
%>
but did not work. Should it just work with complete urls?
am I doing this right also or this is not the best practice?
Thanks you!
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page language="java"%>
<%#page import="java.lang.*" import="org.postgresql.*"
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></title>
</head>
<body>
<%
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/postgres";
Connection connection = DriverManager.getConnection(url,
"postgres", "admin");
String strquery = "DELETE FROM projects WHERE project = '"
+ request.getParameter("project") + "'";
Statement st = connection.createStatement();
//System.out.println("Connecting to database...");
ResultSet rs = st.executeQuery(strquery);
System.out.println(strquery);
pageContext.forward("Index.jsp");
rs.close();
st.close();
} catch (Exception e) {
System.out.println(e);
}
%>
</body>
</html>

Categories