Stack Exchange
help needed (corejava)
I m getting except1 on running this code.Please see if there is any mistake within the try block....
Try
{ 
    pst=con.prepareStatement("SELECT Name,Roll,Semester,Address,Phoneno," +
        "E-mailId,Gender,DOB,Result FROM stud WHERE Roll = ?");
    pst.setString(1,s2);
    ResultSet rs=pst.executeQuery();
    while(rs.next())
    {
        s2=rs.getString("Roll");
        String s1=rs.getString("Name");
        String s3=rs.getString("Semester");
        String s4=rs.getString("Address");
        String s5=rs.getString("Phoneno");
        String s6=rs.getString("E-mailId");
        String s7=rs.getString("Gender");
        String s8=rs.getString("DOB");
        String s9=rs.getString("Result");
        t1.setText(s1);
        t2.setText(s2);
        t3.setText(s3);
        t4.setText(s4);
        t5.setText(s5);
        t6.setText(s6);
        t7.setText(s7);
        t8.setText(s8);
        t9.setText(s9);
    }
    con.commit();
    con.close();
}
catch(SQLException e2)
{
    System.out.println("except1");
}


The Generic SQLException could be any number of things from connection issues, to SQL errors. you will need to print out the exception.  try e2.printStackTrace() after your System.out line. Edit that stacktrace into your question and you will get a better answer from the community.

on a side note, move your con.close() to the finally {} block of your try/catch. That way if an exception is thrown from the above block, you will always close the connection.

If you change the System.out.println("except1") to e2.printStackTrace() you will get more information about what the problem is.

caveat: my Java is rusty -

don't know if field names can contain hypens, depends on the database
print the exact exception that you're getting
why are you doing a commit on a SELECT?


Do e2.printStackTrace(); in the exception block.

Make Sure data type of  "Roll" Atrribute in Database is Character(n).
if it is integer/number then use this
        pst.setInt(1,Integer.parseInt(s2));

Make sure you use VARCHAR data type for all attributes in database. If it is not the case then change your code according to these data types.
And print exception stack trace (e2.printStackTrace()) in catch block for getting exact reason for the exception.
Thanks

    Try
    { 
        pst=con.prepareStatement("SELECT Name,Roll,Semester,Address,Phoneno," +
            "E-mailId,Gender,DOB,Result FROM stud WHERE Roll = ?");
        pst.setString(1,s2);
        string s2 = '123123'; //pass the required value to Query
        ResultSet rs=pst.executeQuery();
        while(rs.next())
        {
            //String s2=rs.getString("Roll");
            String s1=rs.getString("Name");
            String s3=rs.getString("Semester");
            String s4=rs.getString("Address");
            String s5=rs.getString("Phoneno");
            String s6=rs.getString("E-mailId");
            String s7=rs.getString("Gender");
            String s8=rs.getString("DOB");
            String s9=rs.getString("Result");

            t1.setText(s1);
            t2.setText(s2);
            t3.setText(s3);
            t4.setText(s4);
            t5.setText(s5);
            t6.setText(s6);
            t7.setText(s7);
            t8.setText(s8);
            t9.setText(s9);
        }
        con.commit(); // use commit only when you are doing create/update operations
        con.close();
    }
    catch(SQLException e2)
    {
        System.out.println("Error Information");
        e2.printStackTrace();// this method display the error information
    }

