
import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JOptionPane;
import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.table.DefaultTableModel;
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
/**
* * @author GB15 */ public class purchase extends javax.swing.JFrame { JTable table; DefaultTableModel model; JScrollPane sc1; ResultSet rs; Connection con; Statement st;
/**
* Creates new form purchase
*/
public purchase() {
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsn4");
System.out.println("Connected");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(Exception e)
{
e.printStackTrace();
}
//table creation
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
model = new DefaultTableModel();
table = new JTable(model);
model.addColumn(" INVO "); //1
model.addColumn(" INV DATE "); //2
model.addColumn("DESCRIPTION"); //3
model.addColumn(" BILL TYPE "); //4
model.addColumn(" TRN TYPE "); //5
model.addColumn(" AMOUNT "); //6
model.addRow(new Object[]{"","","","","",""});
table.addKeyListener(
new KeyListener()
{
public void keyPressed(KeyEvent e)
{
try{
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
model.addRow(new Object[]{"", "","","","",""});
}
if (e.getKeyCode()==KeyEvent.VK_DELETE)
{
ListSelectionModel list = table.getSelectionModel();
int row = list.getLeadSelectionIndex();
if(row>0)
{
model.removeRow(row);
}
else
{
JOptionPane.showMessageDialog(null, "Table must Contain atleast one Row...so You can't DELETE");
}
}
}catch(ArrayIndexOutOfBoundsException ai ){
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }});
sc1 = new JScrollPane(table,JScrollPane.VERTICALSCROLLBARALWAYS, JScrollPane.HORIZONTALSCROLLBARALWAYS);
cp.add(sc1);
setDefaultCloseOperation(DISPOSEONCLOSE);
setSize(1300,400);
setVisible(true);
initComponents();
try
{
rs= st.executeQuery("SELECT cname FROM pur");
while(rs.next())
{
cb1.addItem(rs.getString(1));
System.out.println(rs.getString(1));
}
}
catch(Exception e)
{
}
getContentPane().add(sc1);
sc1.setBounds(20, 160, 620, 380);
}
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
l2 = new javax.swing.JLabel();
cb1 = new javax.swing.JComboBox();
l3 = new javax.swing.JLabel();
l4 = new javax.swing.JLabel();
l5 = new javax.swing.JLabel();
l6 = new javax.swing.JLabel();
l7 = new javax.swing.JLabel();
l8 = new javax.swing.JLabel();
l1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(700, 700));
getContentPane().setLayout(null);
l2.setText(" Customer name");
l2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(l2);
l2.setBounds(20, 80, 220, 30);
cb1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select" }));
cb1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
cb1ItemStateChanged(evt);
}
});
cb1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cb1ActionPerformed(evt);
}
});
getContentPane().add(cb1);
cb1.setBounds(20, 120, 220, 20);
l3.setText("A/C CODE");
l3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(l3);
l3.setBounds(300, 50, 70, 18);
l4.setText("TOWN/CITY");
l4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(l4);
l4.setBounds(300, 80, 70, 18);
l5.setText("CURRENT BALANCE");
l5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
getContentPane().add(l5);
l5.setBounds(300, 120, 130, 18);
l6.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
getContentPane().add(l6);
l6.setBounds(450, 46, 80, 20);
l7.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
getContentPane().add(l7);
l7.setBounds(450, 80, 80, 20);
l8.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
getContentPane().add(l8);
l8.setBounds(450, 120, 80, 20);
l1.setForeground(new java.awt.Color(204, 0, 0));
l1.setText(" Please select the customer name");
l1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
getContentPane().add(l1);
l1.setBounds(20, 30, 220, 40);
jButton1.setText("ADD");
getContentPane().add(jButton1);
jButton1.setBounds(40, 560, 53, 23);
jButton2.setText("DELETE");
getContentPane().add(jButton2);
jButton2.setBounds(180, 560, 80, 23);
jButton3.setText("EXIT");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(350, 560, 80, 23);
pack();
}// </editor-fold>
private void cb1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void cb1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
try{
String s="%";
if(cb1.getSelectedIndex()>=0)
s=cb1.getSelectedItem().toString();
String query="select * from pur where cname like '"+s+"' ";
Statement de=con.createStatement();
ResultSet demo=de.executeQuery(query);
while(demo.next()){
l6.setText(""+demo.getString(1));
l7.setText(demo.getString(3));
l8.setText(""+demo.getDouble(4));
model.addRow(new Object[]{demo.getString(5),demo.getString(6),demo.getString(7),demo.getString(8),demo.getString(9),demo.getString(10)});
}
}
catch(Exception e){
e.printStackTrace();
//System.out.println("Error : "+e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(purchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(purchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(purchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(purchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new purchase().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox cb1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel l1;
private javax.swing.JLabel l2;
private javax.swing.JLabel l3;
private javax.swing.JLabel l4;
private javax.swing.JLabel l5;
private javax.swing.JLabel l6;
private javax.swing.JLabel l7;
private javax.swing.JLabel l8;
// End of variables declaration
}

Here is a simple example of resetting the table content on selecting the value from the combobox.
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class SearchFromCombo{
public static void main(String[] args) {
final Vector columnNames = new Vector();
final Vector data = new Vector();
JLabel lab=new JLabel("Select:");
final JComboBox t=new JComboBox();
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 name from data");
while(rs.next()){
t.addItem(rs.getString("name"));
}
}
catch(Exception e){}
JButton b = new JButton("Search");
final JTable table=new JTable();
final JScrollPane pane=new JScrollPane(table);
JFrame f = new JFrame();
f.setLayout(null);
lab.setBounds(10,10,100,20);
t.setBounds(120,10,100,20);
b.setBounds(120,40,80,20);
pane.setBounds(10,70,480,170);
pane.setVisible(false);
f.add(lab);
f.add(t);
f.add(b);
f.add(pane);
pane.setVisible(false);
f.setSize(500,300);
f.setVisible(true);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name = String.valueOf(t.getSelectedItem());
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);
}
DefaultTableModel model=new DefaultTableModel(data, columnNames);
table.setModel(model);
pane.setVisible(true);
}
catch(Exception ex){
System.out.println(e);
}
}
});
}
}

what are the values you included in the database?could you please mention it!!!!

i got the solution of the above code its just writing the model code in for loop and the table gets reset.... thanks for the above advice

i got the solution of the above code its just writing the model code in for loop and the table gets reset.... thanks for the above advice

i got the solution of the above code its just writing the model code in for loop and the table gets reset.... thanks for the above advice

now i am facing another problem of the above code..every time i insert a value in data base with the help of the table,values gets duplicated i mean the values are inserted with the new as well as the old ones are again repeated.... Please somebody help me on this.... Thanks in advance....
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.