JSP中导出的数据库表中每行添加删除按钮,能实现删除功能并且显示更新后的表格

JSP中导出的数据库表中每行添加删除按钮,能实现删除功能并且显示更新后的表格,第1张

功能很简单的,但写起来从前台到后台有点繁琐,我告诉你思路,你自己写写啊,这东西主要靠练。

首先你要把数据库里面的数据查询出来,然后在jsp中以表格的形式显示,在每条数据后面添加一个删除按钮

当你点击删除按钮的时候向服务器端传递一个id,通过这个id来删除。服务器端可以用servlet也可以用struts等等,访问数据库可以用jdbc也可以用hibernate,做一个查询和删除的方法就可以了。

java web在jtable中添加按钮的示例如下:

import javaawtBorderLayout;

import javaawtColor;

import javaawtComponent;

import javaawtDimension;

import javaawteventMouseAdapter;

import javaawteventMouseEvent;

import javaioFile;

import javaxswingBorderFactory;

import javaxswingImageIcon;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingJScrollPane;

import javaxswingJTable;

import javaxswingUIManager;

import javaxswingborderBorder;

import javaxswingborderEmptyBorder;

import javaxswingtableAbstractTableModel;

import javaxswingtableTableCellRenderer;

public class JTableButton extends JPanel {

 private JTable table;

 private JScrollPane scrollPane;

 private JButton[] buttons;

 private String path = SystemgetProperty("userdir") + Fileseparator

 + "images" + Fileseparator;

 

 public JTableButton() {

  setBorder(BorderFactorycreateLineBorder(Colorred, 1));

  init();

 }

 private void init() {

  String headName[] = { "Name", "age", "sex", "adress", "image" };

  

  buttons = new JButton[5];

  for(int i=0;i<buttonslength;i++){

   buttons[i] = new JButton(""+i);

  }

  Object obj[][] = {

    { "LiMing", 23, BooleanTRUE, buttons[0],

      new ImageIcon(path + "iconpng") },

    { "ZhangSan", 25, BooleanTRUE,buttons[1],

      new ImageIcon(path + "iconpng") },

    { "WangWu", 21, BooleanFALSE, buttons[2],

      new ImageIcon(path + "iconpng") },

    { "LiSi", 28, BooleanTRUE, buttons[3],

      new ImageIcon(path + "iconpng") },

    { "LuBo", 20, BooleanFALSE, buttons[4],

      new ImageIcon(path + "iconpng") }, };

  

  table = new JTable(new MyTableModel(headName,obj));

  tablesetDefaultRenderer(JButtonclass, new ComboBoxCellRenderer());

  scrollPane = new JScrollPane(table);

  setLayout(new BorderLayout());

  add(scrollPane, BorderLayoutCENTER);

  addHandler();

 }

 private void addHandler(){

  //添加事件

  tableaddMouseListener(new MouseAdapter(){

   public void mouseClicked(MouseEvent e) {

    Systemoutprintln("table");

    int row = tablegetSelectedRow();

    int column = tablegetSelectedColumn();

    Systemoutprintln("row="+row+":"+"column="+column);

    if(column==3){

     //处理button事件写在这里

     Systemoutprintln(((JButton)tablegetValueAt(row, column))getText());

    }

   }

  });

 }

 public static void main(String[] args) {

  JFrame frame = new JFrame();

  frameadd(new JTableButton());

  framesetSize(new Dimension(800, 400));

  framesetVisible(true);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

 }

 class MyTableModel extends AbstractTableModel {

  private String headName[];

  private Object obj[][];

  

  public MyTableModel() {

   super();

  }

  

  public MyTableModel(String[] headName, Object[][] obj) {

   this();

   thisheadName = headName;

   thisobj = obj;

  }

  public int getColumnCount() {

   return headNamelength;

  }

  public int getRowCount() {

   return objlength;

  }

  public Object getValueAt(int r, int c) {

   return obj[r][c];

  }

  public String getColumnName(int c) {

   return headName[c];

  }

  public Class<> getColumnClass(int columnIndex) {

   return obj[0][columnIndex]getClass();

  }

  @Override

  public boolean isCellEditable(int rowIndex, int columnIndex) {

   if (columnIndex == 3 || columnIndex == 4) {

    return false;

   }

   return true;

  }

 }

}

class ComboBoxCellRenderer implements TableCellRenderer {

 public Component getTableCellRendererComponent(JTable table, Object value,

   boolean isSelected, boolean hasFocus, int row, int column) {

  JButton cmb = (JButton) value;

  if (isSelected) {

   cmbsetForeground(tablegetSelectionForeground());

   cmbsetBackground(tablegetSelectionBackground());

  } else {

   cmb

     setForeground((unselectedForeground != null)  unselectedForeground

       : tablegetForeground());

   cmb

     setBackground((unselectedBackground != null)  unselectedBackground

       : tablegetBackground());

  }

  cmbsetFont(tablegetFont());

  if (hasFocus) {

   cmb

     setBorder(UIManager

       getBorder("TablefocusCellHighlightBorder"));

   if (!isSelected && tableisCellEditable(row, column)) {

    Color col;

    col = UIManagergetColor("TablefocusCellForeground");

    if (col != null) {

     cmbsetForeground(col);

    }

    col = UIManagergetColor("TablefocusCellBackground");

    if (col != null) {

     cmbsetBackground(col);

    }

   }

  } else {

   cmbsetBorder(noFocusBorder);

  }

  return cmb;

 }

 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

 private Color unselectedForeground;

 private Color unselectedBackground;

}

gridview中有个onrowcommand的事件,可以在这个事件中处理,我看你的gridview中有两个buttonfield而且commandname是一样的,应该改成不一样的,这样才能在后台rowcommand事件中判断是哪个按钮的点击事件被触发了,在该事件中可以用ecommandname判断。

以上就是关于JSP中导出的数据库表中每行添加删除按钮,能实现删除功能并且显示更新后的表格全部的内容,包括:JSP中导出的数据库表中每行添加删除按钮,能实现删除功能并且显示更新后的表格、java web 怎么在jtable中添加按钮、asp.net中我想通过在gridview中添加button,点击button实现往数据库中添加对应改行的内容。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10199638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存