How to show multiple identicle rows from database on clicking search button to jtable

How to show multiple identicle rows from database on clicking search button to jtable

Hello Sir, I made search button with textfield .when i enter search value in the search field only one row is displayed in the jtable .Suppose i enter name in search field which has two rows in the database table so i want to display both or any number of identicle rows in the jtable .I am posting my code .plz see and give me the remaning code .

Will always be gratefull. Waiting for ur reply .Thanks

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

class Search {

public static void main(String[] args) {
    JLabel lab = new JLabel("Enter Name:");
    final JTextField t = new JTextField(20);
    JButton b = new JButton("Search");
    JPanel p = new JPanel(new GridLayout(1, 1));
    p.add(lab);
    p.add(t);
    p.add(b);
    JFrame f = new JFrame();
    f.getContentPane().add(p);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.pack();
    f.setVisible(true);

    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String name = t.getText();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/techsoft", "root", "techsoft");
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("select * from ankur where name='" + name + "'");
                String n = "", add = "", contact = "", email = "";

                if (rs.next()) {
                    n = rs.getString("name");
                    add = rs.getString("address");
                    contact = rs.getString("contactNo");
                    email = rs.getString("email");
                }
                String data[][] = new String[1][4];
                data[0][0] = n;
                data[0][1] = add;
                data[0][2] = contact;
                data[0][3] = email;
                JFrame frame = new JFrame();
                String labels[] = {"Name", "Address", "Contact No", "Email"};
                JTable table = new JTable(data, labels);
                JScrollPane pane = new JScrollPane(table);

                frame.add(pane);
                frame.setSize(500, 100);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                //frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                // frame.pack();

            } catch (Exception ex) {
                System.out.println(e);
            }
        }
    });
}

}

View Answers

July 11, 2012 at 12:12 PM

Here is a code that search the data from the database and show multiple identical rows from database on clicking search button to jtable.

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

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

        JLabel lab=new JLabel("Enter Name:");
        final JTextField t=new JTextField(20);
        JButton b = new JButton("Search");
        JPanel p = new JPanel(new GridLayout(2,2));
        p.add(lab);
        p.add(t);
        p.add(b);
        JFrame f = new JFrame();
        f.getContentPane().add(p);
        f.pack();
        f.setVisible(true);

        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            String name=t.getText();
            try{
           Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from data where name='"+name+"'");
           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 );
           }
           JFrame frame=new JFrame();
           JTable table = new JTable(data, columnNames);

           JScrollPane pane=new JScrollPane(table);

            frame.add(pane);
            frame.setVisible(true);
            frame.pack();
            }
            catch(Exception ex){
            System.out.println(e);
            }
          }
        });
    }
}









Related Tutorials/Questions & Answers:
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... and show multiple identical rows from database on clicking search button... in the jtable .Suppose i enter name in search field which has two rows in the database
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... and show multiple identical rows from database on clicking search button... in the jtable .Suppose i enter name in search field which has two rows in the database
Advertisements
How to show multiple identicle rows from database on clicking search button to jtable
How to show multiple identicle rows from database on clicking search button... and show multiple identical rows from database on clicking search button... in the jtable .Suppose i enter name in search field which has two rows in the database
Show multiple identical rows into JTable from database
rows from database on clicking search button to jtable. The given code accepts...Show multiple identical rows into JTable from database In this tutorial, you will learn how to display the multiple rows from database to JTable. Here
How to insert rows in jTable?
How to insert rows in jTable?  Hi, I need to take input from user using JTable. I want an empty row to appear after clicking a insert button... not figure out how to. I used DefaultTableModel but wasnt able to insert a row
how to search the string arraylist contains database rows?
how to search the string arraylist contains database rows?  i need to search 2 database. one is excel database and another is SQL database. stored the row values into string arraylist. now i want to print the common rows existed
How to Display Next question from database after clicking Next Button using "Arraylist concept"
How to Display Next question from database after clicking Next Button using "Arraylist concept"  </tr> <%if (pageName.equals("1")) {%> <tr> <td> <div style
Toggle hide/show by clicking same button
Toggle hide/show by clicking same button In this tutorial, we will discuss about how to toggle hide/show by clicking button. In the given below 2 example, there is button ,by clicking on it, the paragraph will hide/show . In first
How to show next question from database on the click of next button..(Struts & Hibernate)
How to show next question from database on the click of next button..(Struts.... Problem.: When m clicking my next button it is not showing another question from "cards" table. passing parameter from viewalllists.jsp to flashPage.jsp
How to show next question from database on the click of next button..(Struts & Hibernate)
How to show next question from database on the click of next button..(Struts.... Problem.: When m clicking my next button it is not showing another question from "cards" table. passing parameter from viewalllists.jsp to flashPage.jsp
How to add a columns with a button set to a Jtable built with database result set
How to add a columns with a button set to a Jtable built with database result... that button column to the table which is built with database result set. i would thank..., there should be another button to get further details of a account such as transactions
how to select random rows from database through servlet
how to select random rows from database through servlet  hello i want to know, how to select random rows from database through servlet
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have to update the cell values from there only means that whatever values i ma entering
how to show search results in the same panel of jframe to where search field and button is present..
how to show search results in the same panel of jframe to where search field... where i have to show the search result in the same panel of where search field... JTextField(20); JButton b = new JButton("Search"); final JTable table=new
How to insert rows from Excel spreadsheet into database by browsing the excel file?
How to insert rows from Excel spreadsheet into database by browsing the excel file?  I want to insert rows from excel sheet to database.for this i... excel file and insert rows into MSSQL database in JSP???   Have a look
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir... from database to jtable .Now as per my requirement i need to update and delete the database records from the table cells by entering new values there only
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete the database
How to show data from database in textbox in jsp
How to show data from database in textbox in jsp   How to show data from database in textbox in jsp   Here is an example that retrieve the particular record from the database and display it in textbox using JSP. <
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... rowToDelete){ // Mark row for a SQL DELETE from the Database
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... rowToDelete){ // Mark row for a SQL DELETE from the Database
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  Hello Sir, I have developed a swing application in which database table... ,update,delete database values from jtable only so i added three buttons add
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
submit button should show the user various textfields on the same panel from...How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
submit button should show the user various textfields on the same panel from...How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
submit button should show the user various textfields on the same panel from...How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all
how to show search results in the same panel of jframe to where search field and button is present..
how to show search results in the same panel of jframe to where search field... application i need to show search results in the same panel of jframe to where search field and button is present . please Sir, send me the code
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read and retrieve jtable row values into jtextfield on clicking... application in which i have to display database records in jtable .now I want... that in textfield below the jtable. plz sir give me the code.... Thank you Sir
how to show effect (visual) on jsp page using value from database
how to show effect (visual) on jsp page using value from database  I... is "not_booked" then the picture shown as seat should be displayed red. Also i dont know how i can implement multiple selection of pictures in case cutomer want to book
how to show image as a link which path coming from database
how to show image as a link which path coming from database   iam not getting proper answer for it. I am using netbeans .the url coming instring from database ,want to display as image on jsp . please help me
Hide text by clicking button
Hide text by clicking button In this tutorial, we will discuss about hide/show text by clicking on button. In the below example, there are two buttons : hide and show .The "hide " button is used to hide text and "show
insert rows from browsed file to sql database
insert rows from browsed file to sql database  i need to insert rows from excel to database by browsing the file in jsp. by connecting both..., content of the file has to go to database. how can i insert record into database
JTable Display Data From MySQL Database
's name and how to add data into the rows of table from database table...JTable Display Data From MySQL Database In this section we will discuss about how to display data of a database table into javax.swing.JTable This section
retrieve the data to text fields from database on clicking the value of combo box
getting data into textarea from database table by clicking on the button...retrieve the data to text fields from database on clicking the value of combo box   retrieve the data to text fields from database on clicking
storing details in database on clicking submit button - JSP-Servlet
database on clicking submit button. I am unable to do this.Can u tell me how to code...storing details in database on clicking submit button  I am using JSP in NetBeans and have developed an application form which has fileds naming
how to show data in database ?
how to show data in database ?  how to show the data in the database to the user in the textbox so that user can modify it and save it again
Deleting All Rows From the database Table
helps us to write on the browser. To delete all the rows from our database table... the PreparedStatement object. If the rows has been deleted from the database table... From the database Table      
how to show value and percentage in piechart sections from database using jfreechart
how to show value and percentage in piechart sections from database using jfreechart  Hii Sir, I made a pie chart from database using... = response.getOutputStream(); try { String query = "SELECT * from satya
How to search the table name in MS SQL Database 2005 from application
How to search the table name in MS SQL Database 2005 from application  How to search the table name in MS SQL Database 2005 from application from our helpdesk application? application might be in html
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  problem in web application : I have a database names tasks in SQL server 2005 having... etc properly in the database,but what and how should i make
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  problem in web application : I have a database names tasks in SQL server 2005 having... etc properly in the database,but what and how should i make
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  problem in web application : I have a database names tasks in SQL server 2005 having... etc properly in the database,but what and how should i make
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  problem in web application : I have a database names tasks in SQL server 2005 having... entered task id,task name ,date etc properly in the database,but what and how
search from database
search from database  DBUtil util = new DBUtil(); try...); }   i want to retrieve values from database and display them in my... it show error what to do please help
Hide/Show paragraph by button click
Hide/Show paragraph by button click In this tutorial, we will discuss about how to hide/show paragraph by clicking on button using jQuery. In the below... displays another button "click to show paragraph " .when we click
Retrieving All Rows from a Database Table
Retrieving All Rows from a Database Table   ... from a database table. You know that table contains the data in rows and columns... APIs and methods. See brief descriptions for retrieving all rows from a database
jtable displays search results
jtable displays search results   hi sir can u send me full source code for displaying search results into jtable from database n jtable n search button must be within same frame but in different Panel and the size of the frame
display multiple images from postgres database in jframe
display multiple images from postgres database in jframe  i just want to display multiple images on jframe by firing a query on postgres database
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve image from mysql database using java and show it in HTML img tag ?  how to retrieve image from mysql database using java and show it in HTML img tag
How To Display both image and data into Swing JTable which is retrieved from ms access database
How To Display both image and data into Swing JTable which is retrieved from ms access database  So far this is my code how can i display both image and data from database.. while (rs.next()) { Vector row = new Vector(columns
fetch and insert multiple rows into mysql database using jsp servlet
fetch and insert multiple rows into mysql database using jsp servlet  hello!!! I am building a attendance sheet in which, I am getting data from one... a problem to insert multiple rows into database using a single insert query
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
into database in multiple rows..means one value for one row  Hai, I need... or .txt) file and get the values from that file and store them into database table in multiple rows(which means one value for one row). eg: my file containes

Ads