Wednesday, 6 November 2019

Swing Delete

Hi All,
Use this code
--------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package placement;

/**
 *
 * @author HUPA
 */
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;
import java.sql.*;

class POview extends JFrame

                {

private JPanel topPanel ;

private JTable table;

private JScrollPane scrollPane;

private String[] columnNames= new String[7];

private String[][] dataValues=new String[10][9] ;
Connection con;
Statement stmt;

public POview()

    {

setTitle("View Reg");

setSize(1000,300);

topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?",
             "Exit Confirmation", JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           setVisible(false);
        }
    }
};
addWindowListener(exitListener);
/*
columnNames=new String[] {"Column 1" , "Column 2" , "Column 3"};

dataValues = new String[][]     {

                                                {"1","2","3"},

                                                {"4","5","6"},

                                                {"7","8","9"}

                                            };
                                            */
   //////
    columnNames = new String[] {
               "pid", "Name", "Address", "PhNo ","Email","Qualification","YoE"

        };
          //create table with data
        try{
    Class.forName("com.mysql.jdbc.Driver");
     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
    //here sonoo is database name, root is username and password
     stmt=con.createStatement();
    //stmt.executeUpdate("insert into parol_request values('"+prison_id+"','123','1973-10-12','hfdjfsdjflejre','out')");
    ResultSet rs=stmt.executeQuery("select * from poreg");
    int i=0;
   while(rs.next()){
  dataValues[i][0]=rs.getString(1);
    dataValues[i][1]=rs.getString(2);
    dataValues[i][2]=rs.getString(3);
    dataValues[i][3]=rs.getString(4);
    dataValues[i][4]=rs.getString(5);
                                                                        dataValues[i][5]=rs.getString(6);
                                                                        dataValues[i][6]=rs.getString(7);

   //System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

    i=i+1;
    }
    //con.close();
    }catch(Exception e1){ System.out.println(e1);}

    ///////////////

 TableModel model=new myTableModel();

 table =new JTable( );

 table.setRowHeight(20);

 table.setModel(model);

 scrollPane=new JScrollPane(table);

  topPanel.add(scrollPane,BorderLayout.CENTER);

  table.addMouseListener(new java.awt.event.MouseAdapter()

            {

public void mouseClicked(java.awt.event.MouseEvent e)

{

int row=table.rowAtPoint(e.getPoint());

int col= table.columnAtPoint(e.getPoint());

JOptionPane.showMessageDialog(null," Value in the cell deleted :"+ " " +table.getValueAt(row,col).toString());
String sql="delete from poreg where POid='"+table.getValueAt(row,0).toString()+"'";
System.out.println(""+sql);
try{
//con.open();
stmt.executeUpdate(sql);
//con.close();
}
catch(Exception e1)
{

}
System.out.println(" Value in the cell deleted :"+ " " +sql+" "+table.getValueAt(row,col).toString());

}

}

);

}

            public class myTableModel extends DefaultTableModel

                                                                                    {

                                                    myTableModel( )

                                                 {

                                                   super(dataValues,columnNames);

                                                   System.out.println("Inside myTableModel");

                                                 }

                                    public boolean isCellEditable(int row,int cols)

                                     {

                                   return false;

                                    }

                                    }

            public static void main(String args[])

            {

            POview mainFrame=new POview();

            mainFrame.setVisible(true);

            }

                        }

No comments:

Post a Comment

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...