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();
                    Statement stmt=con.createStatement();
                    ResultSet rs=stmt.executeQuery("SELECT * FROM student where sno='"+stname+"'");

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

    /** 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.
    // <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);


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

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                .addComponent(stname, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(174, Short.MAX_VALUE))
                .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());
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

    }// </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

Re: Textfield data to another class 80 80

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;


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

rs=stmt.executeQuery("SELECT * FROM student where sno='"+stname+"'");
if( {
 S = new Student();


  } 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);

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.