Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How to show multiple identicle rows from database on clicking search button to jtable
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vikas pandey
How to show multiple identicle rows from database on clicking search button to jtable
1 Answer(s)      2 years and 5 months ago
Posted in : Swing AWT


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

}


Advertisement
View Answers

July 11, 2012 at 12:11 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 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 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 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 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 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 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 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 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 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 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 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 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
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
JTable Display Data From MySQL Database
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 will describe you the displaying of data of a database table into a JTable
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
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
 

 

 

DMCA.com