我用hibernate将数据库中数据显示在jTable中,请问如何实现更新数据库(其实就是:插入、删除、修改、保存),那位愿意帮我看看!我将tableModel的代码贴出来,我的信箱[email protected],谁能帮我,请留下信箱或联系方式,我将代码传过去!
解决方案 »
- 为什么StringBuffer没有实现Cloneable接口
- 请教:java 程序里如何实现从一个列表中拖动一个元素到另一个列表中?
- 一个return的问题
- JAVA新手问题,在线等
- 急求Java类之间参数传递的解决方法!!!!解决立马结贴
- 新手求助!
- 查找List里的元素,删除重复的,怎么实现
- 关于串口通信问题
- 这段代码错在哪?在测试==与equals的区别时出现的!
- 掌握Merlin新的IO类--javaworld部分原文翻译.
- 急救,SOCKET通讯时DataInputStream.readInt()时偶尔程序死
- java连接数据库表时对其插入所遇到的困难,请各位高手伸出你们的帮助之手!
import java.util.List;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class FTableModel extends AbstractTableModel {
//查询的表名
private String tabName = null;
private String headerName[] = null;
private Vector dataVec = null;
public FTableModel(String headerName[],String tabName){
this.tabName = tabName;
this.headerName = headerName;
init();
}
private void init(){
SessionFactory sessionFactory = new Configuration().configure("hibernateTest/hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
String hql = getHql(headerName,tabName);
System.out.println(hql);
Query query = session.createQuery(hql);
List list = query.list();
dataVec = new Vector();
for(int i=0;i<list.size();i++){
String row[] = new String[headerName.length];
List rowList = (List)list.get(i);
for(int j=0;j<headerName.length;j++){
if(rowList.get(j) == null){
row[j] = "null";
}
else{
row[j] = rowList.get(j).toString();
}
}
dataVec.add(row);
}
session.close();
sessionFactory.close();
}
private String getHql(String headerName[],String tabName){
StringBuffer sb = new StringBuffer();
sb.append("select new list(");
StringBuffer columnString = new StringBuffer();
for(int i=0,len=headerName.length;i<len;i++){
columnString.append(",");
columnString.append(headerName[i]);
}
sb.append(columnString.substring(1));
sb.append(") from ");
if(Character.isUpperCase(tabName.charAt(0)))
sb.append(tabName);
else
sb.append(upperFirstChar(tabName));
return sb.toString();
} private static String upperFirstChar(String orig){
return Character.toUpperCase(orig.charAt(0)) + orig.substring(1);
} public int getColumnCount() {
return headerName.length;
} public int getRowCount() {
return dataVec.size();
} public Object getValueAt(int rowIndex, int columnIndex) {
String row[] = (String[])dataVec.get(rowIndex);
Object rs = row[columnIndex];
return rs;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
String row[] = (String[]) dataVec.elementAt(rowIndex);
row[columnIndex] = aValue.toString();
dataVec.setElementAt(row, rowIndex);
}
public void removeRow(int rowIndex){
dataVec.removeElementAt(rowIndex);
fireTableRowsDeleted(rowIndex,rowIndex);
}
public void insertRow(int rowIndex,String[] everyRow){
dataVec.insertElementAt(everyRow,rowIndex);
fireTableRowsInserted(rowIndex, rowIndex);
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}}
package hibernateTest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Main extends JFrame {
private JPanel jPanelContent = null;
private JTable jTable = null;
private JPopupMenu jPopupMenu = null;
public Main(){
super();
init();
}
private void init(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTable Example");
this.setContentPane(getJPanelContent());
}
private JPanel getJPanelContent(){
if(jPanelContent == null){
jPanelContent = new JPanel();
jPanelContent.setLayout(new BorderLayout());
jPanelContent.add(getJTable(),BorderLayout.CENTER);
}
return jPanelContent;
}
private JTable getJTable(){
if(jTable == null){
jTable = new JTable();
String headerName[] = {"id","mc","demo"};
String tabName = "ZdCqdw";
FTableModel fTableModel = new FTableModel(headerName,tabName);
jTable.setModel(fTableModel);
jTable.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3){
Point pt = e.getPoint();
int currRow = jTable.rowAtPoint(pt);
if(currRow>-1){
jTable.setRowSelectionInterval(currRow,currRow);
getJPopMenu().show(jTable,e.getX(),e.getY());
}
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
return jTable;
}
private JPopupMenu getJPopMenu(){
if(jPopupMenu == null){
jPopupMenu = new JPopupMenu();
JMenuItem jMenuItem_del = new JMenuItem();
jMenuItem_del.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FTableModel fTableModel = (FTableModel)jTable.getModel();
fTableModel.removeRow(jTable.getSelectedRow());
/*
*此处需要删除数据库数据
*/
}
});
jMenuItem_del.setText("删除");
jPopupMenu.add(jMenuItem_del);
JMenuItem jMenuItem_ins = new JMenuItem();
jMenuItem_ins.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String nullRow[] = new String[jTable.getColumnCount()];
FTableModel fTableModel = (FTableModel)jTable.getModel();
fTableModel.insertRow(jTable.getSelectedRow(), nullRow);
/*
*此处需要插入数据
*/
}
});
jMenuItem_ins.setText("插入");
jPopupMenu.add(jMenuItem_ins);
jPopupMenu.addSeparator();
JMenuItem jMenuItem_sav = new JMenuItem();
jMenuItem_sav.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jTable,"保存成功!","Success",JOptionPane.INFORMATION_MESSAGE);
/*
*此处需要保存数据
*/
}
});
jMenuItem_sav.setText("保存");
jPopupMenu.add(jMenuItem_sav);
}
return jPopupMenu;
}
public static void main(String[] args) {
Main main = new Main();
main.show();
main.pack();
}
}