try
{
String ans1=(request.getParameter("R1"));
//String name=(request.getParameter("T2"));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:exam","","");
PreparedStatement s;
ResultSet rs;
String value = (session.getValue("s1")).toString();
String name=(request.getParameter("T1"));
s=con.prepareStatement("select emailid from check where emailid=?");
s.setString(1,value);
rs=s.executeQuery();
if (rs.next())
{
out.println("already reg");
}
else
{
PreparedStatement st=con.prepareStatement("update check set ans1=? where name Like ?");
st.setString(1,ans1);
st.setString(2,value);
st.executeUpdate();
out.println(value);
}
}catch(Exception e)
{
out.println(e);
}
now my code is this and its showing null pointer exception
Which line causing null pointer exception.
you should check/validate for the input parameter(s) null or not.
like
if(!(value == null || value.trim() == "" )){
// do your sql codes here
}else{
// print exception
}
Have added my comments inline, potential places where
NullPointerException can occur. run it and post the result.
try
{
String ans1=(request.getParameter("R1"));
//String name=(request.getParameter("T2"));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:exam","","");
PreparedStatement s;
ResultSet rs;
String value = (session.getValue("s1")).toString(); //AJAY - column
value s1 may be null
String name=(request.getParameter("T1"));
s=con.prepareStatement("select emailid from check where emailid=?");
s.setString(1,value);
rs=s.executeQuery();
if (rs.next())
{
out.println("already reg");
}
else
{
PreparedStatement st=con.prepareStatement("update check set ans1=? where
name Like ?");
st.setString(1,ans1);
st.setString(2,value);
st.executeUpdate();
out.println(value);
}
}catch(Exception e)
{
e.printStackTrace(); // AJAY - will print out the source of error.
}
If you're using Access as your database, your UPDATE won't show up until you close your resources properly. That means ResultSet, Statement, and Connection each closed in individual try/catch blocks in a finally block.
Externalize your connection parameters (e.g., driver, URL, etc.) Those can change, and you don't want to have to recompile your code to do it.
%
> but how can i call a class file in jsp with import
> statement
It works the same way as in any class. You import the class, create an object and call methods on it. If you could add JDBC code directly in the JSP, I am pretty sure you could write the code with your custom class in the same manner (except that you achieve some simplicity by getting the JDBC code out from where it should not be).
> Which line causing null pointer exception.
>
> you should check/validate for the input parameter(s)
> null or not.
>
> like
>
> if(!(value == null || value.trim() == "" )){
> // do your sql codes here
> else{
> // print exception
>
Don't compare String values using ==.
(value.trim() == "")
should be:
(value.trim().length() == 0)
or
(value.trim().equals(""))