Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Thumbs down search data from textfield

    how to search data from database using jtextfield and display it in jtable..?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    Don't double post the same question, don't wake old dead threads and don't hijack another poster's thread with an unrelated question. The post you made in JTable vs JTextField - which to use for creating "Search" screen has been removed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    That's a very broad question. You need to break that into several steps and tell us which you know and which you don't know how to do.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default Re: search data from textfield

    I know about the jtable to display the data ....
    but want to try for textbox....

    for example : if i write a sting in text box : like a name..
    the it display all the record in jtable for a particular name......

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    What's a text box?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default Re: search data from textfield

    Quote Originally Posted by DarrylBurke View Post
    What's a text box?

    db

    Ohhh Sorry I Mean JTextField..

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    Do you know how to retrieve the text from a JTextField as a String?

    If you don't break this into several steps and tell us which step(s) you need help with, this is going to take forever.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default Re: search data from textfield

    Quote Originally Posted by DarrylBurke View Post
    Do you know how to retrieve the text from a JTextField as a String?

    If you don't break this into several steps and tell us which step(s) you need help with, this is going to take forever.

    db


    is it like this...?

    // to string
    String text = textField.getText();

    // to JTextField
    textField.setText(text);

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    So, what's the next step?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: search data from textfield

    So show us the JDBC code then.
    Please don't make this the equivalent of getting blood from a stone.
    You want help?
    We need information.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default Re: search data from textfield

    Quote Originally Posted by DarrylBurke View Post

    db
    Hey I m done it...

    Hello Friends I m creating one textfield and retrive data from that textfield and display it in JTable.
    This is the code for it....

    import java.awt.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.table.*;

    public class Search {

    public static void main(String[] args)
    {
    final Vector columnNames = new Vector();
    final Vector data = new Vector();

    final JTextField t=new JTextField(20);
    JButton b = new JButton("Search");

    JPanel p=new JPanel( new GridLayout(2,2));
    p.add(t);
    p.add(b);

    JFrame f = new JFrame();
    f.getContentPane().add(p);
    f.setVisible(true);

    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:person");
    String sql = "select * from person where name='"+t.getText()+"'";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();

    int columns = md.getColumnCount();

    for (int i = 1; i <= columns; i++)
    {

    columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next())
    {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    rs.close();
    stmt.close();
    }
    catch(Exception ex){
    System.out.println(ex);
    }

    JTable table = new JTable(data, columnNames);
    TableColumn col;

    for (int i = 0; i < table.getColumnCount(); i--)
    {
    col = table.getColumnModel().getColumn(i);
    col.setMaxWidth(250);
    }

    JScrollPane scrollPane = new JScrollPane( table );

    JFrame f1=new JFrame();
    f1.add( scrollPane );
    f1.setSize(600,400);
    f1.setVisible(true);
    } });
    }
    }


    ------------------------------------------------

    Note : Here In the above code
    Database Name = person
    Table Name = person.


    Thanx Friends
    McaJavaProgramer.
    Last edited by mcajavaprogramer; 05-04-2012 at 02:52 PM.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: search data from textfield

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Search data for a month
    By victoryo in forum New To Java
    Replies: 5
    Last Post: 12-28-2011, 05:51 PM
  2. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  3. Getting data from textfield into another class
    By aborgeld in forum New To Java
    Replies: 11
    Last Post: 03-20-2011, 08:04 PM
  4. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  5. Replies: 0
    Last Post: 04-04-2008, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •