I want to display result when a student enter their rollno in a text box and click on the submit button.
I know there are errors as i have tried many times before and each time i have changed it so
Please help me in locating errors.I need it early.
Thanks.

import java.awt.*;
import java.awt.TextField;
import java.awt.Label;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Statement;

 class res extends Frame implements ActionListener

{  
        Connection con;
        Statement st;
        ResultSet rs;

        List lP1=new List();
        List lP2=new List();
        List lP3=new List();
        List lP4=new List();
        List lP5=new List();

        Label res=new Label("Result");

        Label P1=new Label("P1");
        Label P2=new Label("P2");
        Label P3=new Label("P3");
        Label P4=new Label("P4");
	Label P5=new Label("P5");


        Button b1=new Button("Submit");
        Button b2=new Button("Cancel");


        TextField msg=new TextField(200);

        public res()
        {
                setLayout(null);
                setVisible(true);
                setSize(900,900);

               res.setBounds(320,50,650,20);

                P1.setBounds(200,80,80,30);
		P2.setBounds(280,80,80,30);
                P3.setBounds(370,80,80,30);
                P4.setBounds(440,80,80,30);
                P5.setBounds(520,80,80,30);

                lP1.setBounds(200,130,80,150);
		lP2.setBounds(280,130,80,150);
                lP3.setBounds(360,130,80,150);
                lP4.setBounds(440,130,80,150);
                lP5.setBounds(520,130,80,150);

                msg.setBounds(100,400,300,20);

                b1.setBounds(320,370,60,20);
                b2.setBounds(250,300,50,20);

                add(res);
                add(P1);
		add(P2);
                add(P3);
                add(P4);
                add(P5);

                add(lP1);
                add(lP2);
		add(lP3);
                add(lP4);
                add(lP5);

                add(b1);
                add(b2);

                add(msg);

                b1.addActionListener(this);
                b2.addActionListener(this);


                try
		{
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con=DriverManager.getConnection("jdbc:odbc:student");
                        st=con.createStatement();
                 }
                catch(Exception e)
                {
                System.out.println(e);
                } 

       }
       public void actionPerformed(ActionEvent ae)
       {
        try
               {
                  if(ae.getSource()==b1)
                  {


                   ResultSet rest=st.executeQuery("select * from result where result='"+ b1 + "'");
                   if(rest.next())
                  {
                      msg.setText("Result");
                      return;
                  }
                  else if(ae.getSource()==b2)
                        { 
	                   dispose();
                        }
		   
                  }
              }
              catch(Exception e)
              {
              System.out.println(e);
              }
       }


        public void keyTyped(KeyEvent ke)
        {

        }
        public void keyPressed(KeyEvent ke)
        {    
        } 
        public void keyReleased(KeyEvent ke)
        {

        }

}
class result
{

        public static void main(String as[])
        {
	       res demo=new res();
        }
}

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

Post what error messages you get.

Well, a couple of things. This query makes little sense

executeQuery("select * from result where result='"+ b1 + "'");

The field name in the where clause is the same as the table name.

Second, after you run the query and call .next(), you need to retrieve the field value like so

String value = rest.getString("theFieldNameYouWant");
msg.setText(value);

If you have other errors, post them.

commented: Nice job +5

yeah i want to know little more
I want to create a page with a Rollno text box
and student have to enter their rollno in that textbox to get their result.
i am not getting that how to execute the query.
and applying the condition if the rollno entered by student doesnot exist.

Now this coding is working but i want to display output in Frame not in Command Prompt.
Plz solve it out.

import java.awt.*;
//import java.applet.*;
import java.awt.event.*;
import java.lang.*;
import java.sql.*;

public class wscreen extends Frame
 implements WindowListener,ActionListener
{
 Button b1;
 TextField t1;
 Label l1,l2;
 String msg,msg1,msg2,msg3,msg4,msg5,msg6,msg7;

 public wscreen()
 {
  super("imp");
  setLayout(null);
 
  b1= new Button("submit");
  
  l1=new Label("Result Declared",Label.CENTER);
  l2=new Label("Enter ROLL NO", Label.LEFT);

  t1=new TextField(5);
/*t2=new TextField(5);
t3=new TextField(5);
t4=new TextField(5);
t5=new TextField(5);
t6=new TextField(5);
t7=new TextField(5);
t8=new TextField(5);*/


  Font f=new Font("Impact", Font.BOLD,25);
  Color c1=new Color(255,200,210);
  Color c2=new Color(200,200,210);

  l1.setFont(f);
  l1.setForeground(c1);
  l1.setBackground(c2);
  l1.setBounds(90,20,200,30);  
  add(l1);

  l2.setBounds(20,70,90,30);  
  add(l2);

  t1.setBounds(200,70,90,20);  
  add(t1);
/*
t2.setBounds(200,200,90,10);  
  add(t2);
t3.setBounds(200,230,90,10);  
  add(t3);
t4.setBounds(200,250,90,20);  
  add(t4);
t5.setBounds(200,270,90,20);  
  add(t5);
t6.setBounds(200,290,90,20);  
  add(t6);
t7.setBounds(200,300,90,20);  
  add(t7);
t8.setBounds(200,310,90,20);  
  add(t8);*/

  b1.setBounds(90,120,200,30);  
  add(b1);

  addWindowListener(this);
  b1.addActionListener(this);
 
 }
 

 public void actionPerformed(ActionEvent ae)
 {
  Object ob=ae.getSource();

  if(ob==b1);
  

 }

 public void paint(Graphics g)
{
msg="ROLLNO  :"+t1.getText();
msg1="NAME   :";
msg2="RESULT :";
msg3="P1     :";
msg4="P2     :";
msg5="P3     :";
msg6="P4     :";
msg7="P5     :";
g.drawString(msg,90,190);
g.drawString(msg1,90,210);
g.drawString(msg2,90,230);
g.drawString(msg3,90,250);
g.drawString(msg4,90,270);
g.drawString(msg5,90,290);
g.drawString(msg6,90,310);
g.drawString(msg7,90,330);
}

 public static void main(String ar[])
 {
  wscreen d=new wscreen();
  d.setSize(400,400);
  d.setVisible(true);

String Sname;
String Sno,m1,m2,m3,m4,m5;


Connection Con=null;
Statement Stmt;
ResultSet ERs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con=DriverManager.getConnection("jdbc:odbc:data");
Stmt=Con.createStatement();
ERs=Stmt.executeQuery("SELECT * FROM s1");

while(ERs.next())
{
Sno=ERs.getString("ROLLNO");
Sname=ERs.getString("NAME");
m1=ERs.getString("P1");
m2=ERs.getString("P2");
m3=ERs.getString("P3");
m4=ERs.getString("P4");
m5=ERs.getString("P5");


System.out.println(Sno+" "+Sname+"  "+m1+" "+m2+" "+m3+" "+m4+""+m5+"\n");

}
Stmt.close();
Con.close();
}
catch(Exception e)
{
System.out.println(e);
}


}

public void windowClosing(WindowEvent we)
{
this.setVisible(false);
System.exit(0);
}

public void windowActivated(WindowEvent we)
{
}

public void windowDeactivated(WindowEvent we)
{
}
public void windowOpened(WindowEvent we)
{
}
public void windowClosed(WindowEvent we)
{
}
public void windowIconified(WindowEvent we)
{
}

public void windowDeiconified(WindowEvent we)
{
}

}

You do not want to be overriding paint on the Frame to display data. If you do need to override painting code, override paintComponent() on the appropriate component and be sure that you call super.paintComponent() if required. In any case, custom painting code is not called for here.

You should use an appropriate component to display your result data such as JTable, JLabels, or JTextArea. The choice will depend on what you are trying to display, as that is not very clear from your code.

Thank you so much Ezzaral for helping me
actually i want to make a frame in which there is a text box in which a student has to enter their rollno to view their result.I am attaching the screenshot of window.Plz check it.

You do not want to be overriding paint on the Frame to display data. If you do need to override painting code, override paintComponent() on the appropriate component and be sure that you call super.paintComponent() if required. In any case, custom painting code is not called for here.

You should use an appropriate component to display your result data such as JTable, JLabels, or JTextArea. The choice will depend on what you are trying to display, as that is not very clear from your code.

Just run your query in the ActionListener as you had in the first listing. You will need to set the roll number as a parameter.

int rollNo = -1;
try {
   rollNo = Integer.parseInt( t1.getText() );
} catch (NumberFormatException nfe){
   JOptionPane.showMessageDialog(wscreen.this,"Not a valid roll number"); 
}
if (rollNo > -1) {
  PreparedStatement pstmt = Con.prepareStatement("SELECT * FROM s1 where RollNo=?);
  pstmt.setInt(1, rollNo);
  ResultSet rs = pstmt.executeQuery();
  if ( rs.next() ){
    // set output here
  } 
  else {
    // no record was found
  }
}

If there is a result (from .next() ), then simply set the data fields from that result. You can use a JLabel with HTML if you want a result that is similar to direct painting on the Frame, without the hassle of custom painting. Look up JLabel in the API docs for more info.

You can also define the prepared statement outside the listener and reuse it with different roll number parameters if you will be submitting it many times. I just showed it in there for convenience. Don't forget to always close statements when you are done with them, as some drivers don't release those on their own very well.

I am new to this webb, how could I send my Question?Opinion or even Chat?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.