Whats wrong? JSP in netbeans?

Status
Not open for further replies.

nithinks

True Techie
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<HTML>
<HEAD>
<%@page import="java.io.*" import="java.lang.*" import="java.sql.*" %>
<TITLE>JSP Example 2</TITLE>
</HEAD>
<BODY>
<H1>JSP Example 3</H1>
<%
String uid, upwd, dpwd ;
Connection dbconn;
ResultSet results;
PreparedStatement sql;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
try
{

dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
uid = request.getParameter("nme");
upwd = request.getParameter("pwd");
sql = dbconn.prepareStatement("SELECT anm FROM login WHERE nm='"+uid+"'");

results = sql.executeQuery();
dpwd="admin";
dpwd = results.getString("anm");

%>
<p><%= dpwd%></p>
<%
}catch (SQLException s)
{
out.println("SQL Error<br>");
}
}
catch (ClassNotFoundException err)
{
out.println("Class loading error");
}
%>

</BODY>
</HTML>
------------------------------------------
The above program gives SQL exception why? can you give me the corrected code?
 

planetcall

Indian by heart
I am not into JSP but I can see that in the exception block you are throwing a custom string. I would suggest you rather print out the Exception message and Stack Trace instead of printing "SQL Exception". I hope that will give you greater insight of what is happening.
 
OP
nithinks

nithinks

True Techie
im getting error for using "where" any way to solve this?
__________
problem solved.. i used LIKE instead of = near 'where'
 
Last edited:

planetcall

Indian by heart
its good you did it yourself. Always remember that Exceptions are meant not just to throw a custom string but they are powerful tools for showing you the precise information about the error/crash.
 

Desi-Tek.com

In the zone
u r mixing business logic with presentation? this is not good

Code:
<HTML>
<HEAD>
<%@ page import="java.sql.*" %>
<TITLE>JSP Example 2</TITLE>
</HEAD>
<BODY>
<H1>JSP Example 3</H1>
<%
  String uid, upwd, dpwd;
  String Connection = dbconn;
  String ResultSet = results;
  String PreparedStatement = sql;
  try {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "");
    uid = request.getParameter("nme");
    upwd = request.getParameter("pwd");
    sql = dbconn.prepareStatement("SELECT anm FROM login WHERE nm='" + uid + "'");
    results = sql.executeQuery();
    dpwd = "admin";
    dpwd = results.getString("anm");
%>
<p><%= dpwd%></p>
<%
  } catch (SQLException s) {
    out.println("SQL Error<br>");
  }
  catch (ClassNotFoundException err) {
    out.println("Class loading error");
  }
  }
%>
</BODY>
</HTML>
 
Status
Not open for further replies.
Top Bottom