sathya
sathya

Reputation: 199

How to display the specific values in java swing gui using the db?

I have tried to get the student name and father name from the db using java swing gui.i have used the below source code. when i executed the code i am not getting any error.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Test1  {

   JFrame f;
   JLabel l1,l2,l3,l4;
   JTextField f1,f2;
   JButton b1;
    Connection con;
    Statement st = null;
    ResultSet rs;

    /**
     * Creates new form Register
     */
    public static void main(String args[]) {
       Test1 r=new Test1();
        r.frame();
        r.connect();
    }

    public void connect() {


        try {


            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@//drf9db07:1521/DRF9DB_EDA","EDA_XMLV02_IMP_f9","clob1234");
            st =con.createStatement();

            String str = "select student_name,father_name from studentdb where rollno=? and batchno=?";
            ResultSet rs = st.executeQuery(str);
            System.out.println(rs);

            while (rs.next()) {
            String student_name = rs.getString("student_name");
            String father_name = rs.getString("father_name");
            System.out.println(student_name + "\t" + father_name);
        }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void frame() 
    {
     f=new JFrame ("studentrecord");
     f1=new JTextField(10);
     f2=new JTextField(10);
     l1=new JLabel("rollno");
     l2=new JLabel("batchno");
     b1=new JButton("submit");
     l3=new JLabel("student_name");
     l4=new JLabel("father_name");

     f.setLayout(new GridBagLayout());
     f.add(l1);
     f.add(f1);
     f.add(l2);
     f.add(f2);
     f.add(b1);
     f.add(l3);
     f.add(l4);

     f.setVisible(true);
     f.pack();
     b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            String rollno=l1.getText();
            String batchno=l2.getText();
            String student_name=l3.getText();
            String father_name=l4.getText();
            System.out.println(rollno);
            System.out.println(batchno);
            System.out.println(student_name);
            System.out.println(father_name);

            connect();
        }
    });
    }
    }

When i hardcode the roll no and batch no i could see the results in the console.but when i pass the parameters its not getting executed and i could see no updates in the gui.

now i am getting the below response in the eclipse console when entered the rollno and batchno

oracle.jdbc.driver.OracleResultSetImpl@1811e67 rollno batchno student_name father_name oracle.jdbc.driver.OracleResultSetImpl@16d0c80 Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook

i think the values which i am passing is not posted to the sql query from the gui.

Upvotes: 0

Views: 628

Answers (1)

Manish Sakpal
Manish Sakpal

Reputation: 299

In your connect method add this code after the query in string and make sure you remove the statement:- st.executeQuery(str); ResultSet rs=st.executeQuery(str);

while(rs.hasNext())
{
     String student_name=rs.getString("student_name");
     String father_name=rs.getString("father_name");
}

System.out.println(student_name+" "+father_name);

Also if you want to display these values in swing label just pass these String variables to the label constructors, also make sure you declare these variables i.e: student_name and father_name globally in your class so that it will be accessible to all the methods in your class. Right now in my code these variables are local to the ResultSet block.

Upvotes: 1

Related Questions