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:
Advertisements
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
Toggle hide/show by clicking same buttonToggle 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 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 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.
<
Hide text by clicking buttonHide 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 databaseinsert
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
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
search from databasesearch 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