public class examination extends javax.swing.JFrame {
    public Connection con;
    public Statement stmt;
    public ResultSet rs;
    public String sname,sid,sub;

    /** Creates new form examination */
    public examination() {
        welcome wl=new welcome();
        String stname=wl.regdno.getSelectedText();
        
        try
                {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection("jdbc:odbc:exam");
                    Statement stmt=con.createStatement();
                    ResultSet rs=stmt.executeQuery("SELECT * FROM student where sno='"+stname+"'");
                    while(rs.next())
                    {
                    sid=rs.getString(1);
                    sname=rs.getString(2);
                    }

                }
                catch(ClassNotFoundException e)  {
 			System.err.println("Failed to load driver");
 			e.printStackTrace();
                }catch(SQLException e){
 			System.err.println("Unable to connect");
 			e.printStackTrace();}
                initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        stname = new javax.swing.JLabel("Student Name:"+sname);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(stname, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(174, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(stname, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(271, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void ex() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new examination().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel stname;
    // End of variables declaration//GEN-END:variables

}

Where welcome is another class where I have a text field named "regdno". Now I want to get the data what I put into the textfield to this class and get student name from database and show in a label. In this code there is no error but in the particular label its shows null value. If I write the SQL query without where clause its work perfectly but I want to use with where clause.

Thank You

Of course you get null because the query doesn't return anything! Try to print the argument of the query or better yet the query itself:

System.out.println("SELECT * FROM student where sno='"+stname+"'");

You will also need to close the connection, the statement and the resultset before proceeding.
I also don't agree with your logic not do I know what the welcome class has inside. Does the regdno.getSelectedText() has any value when you call the (new welcome()) ? Because I believe that when you do the regdno is empty. And even if you do enter value to that text, you need to "send" that value to your query by clicking a button or something.

For starters, fix your DB connection.
Create a separate class like this:

public class Student {
  private String sid = null;
  private String sname = null;

  public Student() {

  }

  // add get, set methods or make the above public (Better go with the get/set methods):
  // example:
  public String getSid() {
    return sid;
  }
}

Then

public Student getStudent(String stname) throws Exception {
Student S = null;
Statement stmt = null;
ResultSet rs = null;
Connection con = null;
  try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:exam");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM student where sno='"+stname+"'");
                    
if(rs.next()) {
 S = new Student();

 S.setSid(rs.getString(1));
 S.setSname(rs.getString(2));
}

  } catch (Exception e) {
    throw e;
  } finally {
    if (rs!=null) rs.close();
    if (stmt!=null) stmt.close();
    if (con!=null) con.close();
  }
return S;
}

I would suggest that you call that method whenever you want to get data from the DB and use that methodology for other cases.
Also I would suggest to remove the (new welcome()) from that class.
I would suggest, in the welcome class to have a text and a button; when you click the button, take the value of the text and pass it as argument to the examination class:
IN THE welcome CLASS

public void buttonclicked(..) {
  // take the text value
  String stname = ...
  examination ex = new examination(stname);
  ex.setVisible(true);
}

And:
IN THE examination CLASS

Student student = null;

// constructor of the examination
public examination(String stname) {
  try {
    student = getStudent(stname);
    if (student==null) {
      // student not found
    }
  } catch (Exception e) {
   // display error message
  }
}
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.