Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to display data on JTable after clicking a button.....?? help needed

843806Sep 29 2008 — edited Mar 4 2009
i am working on a project.....
my program hav database connectivity..
i hav added search button on a frame...

now wat i want that when user enter the data in a textField and click search Button ....it search data from the database matching the Textfield content with the data of the column ...if it is matched show all data of that user from database on JTable
using JDBC i can store all content in a string[][] ...... but how to show that content on the Jtable..

below is the code....this is not my actual code i edited so as to make code smaller.....

now when user enter an "user -id" in the below code Textfield and click search button ..
show the user details in the Jtable...

i hav stored it into a String value[][]....next i dont know how to display it on Table

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;

public class project extends JFrame implements ActionListener{
    JButton bsrch=new JButton("Search");
    JButton bclr=new JButton("Clear");
     
   
    JTextField tuid = new JTextField(10);
    JTextField ttele = new JTextField(10);
   
    JLabel uid=new JLabel(" USER ID  NO.");
    JLabel tele=new JLabel("Telephone NO.");
    JLabel nul= new JLabel("");
    
    Font style3=new Font("Courier", Font.BOLD, 15);
    Font style=new Font("Courier",Font.BOLD,20);
    Font style2=new Font("Helvetica", Font.BOLD,20);
   
    public project(){

 
	Icon icon = new ImageIcon("blue.jpg");
	JLabel label = new JLabel(icon);
         add(label);
   
           this.setSize(750,750);
          this.setResizable(false);
         this.setVisible(true);


     
    label.add(uid);
    tuid.setBounds(250,550,150,25);
    tuid.setBackground(Color.white);
    tuid.setFont(style);
    label.add(tuid); 
       
    bsrch.setBounds(240,650,100,35);
    bsrch.setFont(style2);
    bsrch.setForeground(Color.black);
    this.add(bsrch);
       this.add(nul); 
        String data[][]=new String[10][10];
        String[] col = { "Name", "L-Name","Gender","Zone","Address", "Birth"};
    JTable table = new JTable(data,col);
    table.setRowHeight(50);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    table.setFont(new Font("Dialog", Font.BOLD, 20));
    table.setBackground(Color.green);
    header.setFont(new Font("Courier",Font.BOLD,20));
    table.getColumnModel().getColumn(0).setPreferredWidth(130);
    table.getColumnModel().getColumn(1).setPreferredWidth(130);
    table.getColumnModel().getColumn(2).setPreferredWidth(100);
    table.getColumnModel().getColumn(3).setPreferredWidth(100);
    table.getColumnModel().getColumn(4).setPreferredWidth(250);
     table.getColumnModel().getColumn(5).setPreferredWidth(150);
       JScrollPane pane = new JScrollPane(table);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    this.add(pane);
    
    
 

   
   

}
public void actionPerformed(ActionEvent e2){


if(e2.getActionCommand()=="Search"){
 String id=tuid.getText();
 String value[][]=new String[6][6];

                        try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:conf", "","");
                           
        String sql="SELECT  * FROM tds WHERE uid=?";
	
		PreparedStatement stmt=con.prepareStatement(sql);

		ResultSet rs;
	
		stmt.setString(1,id);

		rs=stmt.executeQuery();
                
                int i=0;
                int k=1;
		while(rs.next())
		{
                    k=1;
		 for(int j=0;j<5;j++){
                       value[i][j]=rs.getString(k);
                         k++;
                          }
                     i++;   
			
		}
                con.close();
           
       }
      catch(Exception s){
        System.out.println("Oops!!!There is no data to display.\nContact the DBA");
      }

}



}

public static void main(String args[]){
project obj=new project();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
plzz help me...i am finding using JTabel difficult
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 1 2009
Added on Sep 29 2008
6 comments
3,011 views