我想实现的功能就是根据名字查找数据库的相应记录并把它从JTable显示出来,然后编辑JTable里的记录后写入数据库。
但是编辑JTable里的记录后写入数据库这个功能不会做
请各位高手指点
代码如下:
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.Box;public class FinalTable
{
JFrame jf = null;
JPanel northpanel = null;
JLabel jlName = new JLabel("名字");
JLabel slName = new JLabel("模糊搜索");
//名字的JTextField
JTextField tfName = new JTextField(10);
//模糊搜索JTextField
JTextField stfName = new JTextField(10);
//名字按钮
JButton bName = new JButton("search");
//模糊搜索
JButton sbName = new JButton("search");
JTable table ;
JScrollPane scrollPane ; ResultSetTableModel model; String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root";
String password = "nintendo";
Connection con;
PreparedStatement pstmt;
ResultSet rs;
Statement stmt; String tableName; Box h = Box.createHorizontalBox(); public FinalTable()
{
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
}
catch (Exception e)
{
e.printStackTrace();
}
} public JFrame getFrame()
{
if (jf == null)
{
jf = new JFrame("table");
jf.add(getNorthPanel(), BorderLayout.NORTH);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
try
{
if (con != null)
{
con.close();
System.out.println("con close");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// jf.setResizable(false);
jf.setVisible(true); }
return jf;
} public JScrollPane getScrollPane()
{
return this.scrollPane;
}
public JPanel getNorthPanel()
{
if (northpanel == null)
{
northpanel = new JPanel();
northpanel.add(jlName);
northpanel.add(tfName);
//添加名字按钮搜索事件监听器
bName.addActionListener(new SearchName());
northpanel.add(bName);
northpanel.add(h.add(Box.createHorizontalStrut(90)));
northpanel.add(slName);
northpanel.add(stfName);
//添加模糊搜索按钮事件监听器
sbName.addActionListener(new AboutSearchName());
northpanel.add(sbName);
}
return northpanel;
} //名字搜索按钮事件监听类
class SearchName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if (scrollPane != null)
{
getFrame().remove(scrollPane);
}
if (rs != null)
{
rs.close();
}
String temp = tfName.getText();
String sql = "select * from test where name = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, temp);
ResultSet rs = pstmt.executeQuery();
model = new ResultSetTableModel(rs);
table = new JTable(model);
scrollPane = new JScrollPane(table);
getFrame().add(scrollPane, BorderLayout.CENTER);
//调用validate()显示更新后的容器
getFrame().validate();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//模糊搜索时件监听类
class AboutSearchName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if (scrollPane != null)
{
getFrame().remove(scrollPane);
}
if (rs != null)
{
rs.close();
}
String temp = stfName.getText();
System.out.println(temp);
String sql = "select * from test where name like ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "%" + temp + "%");
ResultSet rs = pstmt.executeQuery(); model = new ResultSetTableModel(rs);
table = new JTable(model);
scrollPane = new JScrollPane(table);
getFrame().add(scrollPane, BorderLayout.CENTER);
getFrame().validate();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
FinalTable ft = new FinalTable();
ft.getFrame();
}
}class ResultSetTableModel extends AbstractTableModel
{
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet rs)
{
this.rs = rs;
try
{
rsmd = rs.getMetaData();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getColumnName(int c)
{
String[] str = {"id","名字","年龄","整型num","字符型num"};
try
{
return str[c];
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public Object getValueAt(int r, int c)
{
try
{
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public boolean isCellEditable(int row, int column)
{
return true;
}
public void setValueAt(Object aValue, int row, int column)
{
try
{
rs.absolute(row + 1);
rs.updateObject(column + 1, aValue);
rs.updateRow();
fireTableCellUpdated(row, column);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
但是编辑JTable里的记录后写入数据库这个功能不会做
请各位高手指点
代码如下:
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.Box;public class FinalTable
{
JFrame jf = null;
JPanel northpanel = null;
JLabel jlName = new JLabel("名字");
JLabel slName = new JLabel("模糊搜索");
//名字的JTextField
JTextField tfName = new JTextField(10);
//模糊搜索JTextField
JTextField stfName = new JTextField(10);
//名字按钮
JButton bName = new JButton("search");
//模糊搜索
JButton sbName = new JButton("search");
JTable table ;
JScrollPane scrollPane ; ResultSetTableModel model; String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root";
String password = "nintendo";
Connection con;
PreparedStatement pstmt;
ResultSet rs;
Statement stmt; String tableName; Box h = Box.createHorizontalBox(); public FinalTable()
{
try
{
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
}
catch (Exception e)
{
e.printStackTrace();
}
} public JFrame getFrame()
{
if (jf == null)
{
jf = new JFrame("table");
jf.add(getNorthPanel(), BorderLayout.NORTH);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
try
{
if (con != null)
{
con.close();
System.out.println("con close");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// jf.setResizable(false);
jf.setVisible(true); }
return jf;
} public JScrollPane getScrollPane()
{
return this.scrollPane;
}
public JPanel getNorthPanel()
{
if (northpanel == null)
{
northpanel = new JPanel();
northpanel.add(jlName);
northpanel.add(tfName);
//添加名字按钮搜索事件监听器
bName.addActionListener(new SearchName());
northpanel.add(bName);
northpanel.add(h.add(Box.createHorizontalStrut(90)));
northpanel.add(slName);
northpanel.add(stfName);
//添加模糊搜索按钮事件监听器
sbName.addActionListener(new AboutSearchName());
northpanel.add(sbName);
}
return northpanel;
} //名字搜索按钮事件监听类
class SearchName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if (scrollPane != null)
{
getFrame().remove(scrollPane);
}
if (rs != null)
{
rs.close();
}
String temp = tfName.getText();
String sql = "select * from test where name = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, temp);
ResultSet rs = pstmt.executeQuery();
model = new ResultSetTableModel(rs);
table = new JTable(model);
scrollPane = new JScrollPane(table);
getFrame().add(scrollPane, BorderLayout.CENTER);
//调用validate()显示更新后的容器
getFrame().validate();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//模糊搜索时件监听类
class AboutSearchName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if (scrollPane != null)
{
getFrame().remove(scrollPane);
}
if (rs != null)
{
rs.close();
}
String temp = stfName.getText();
System.out.println(temp);
String sql = "select * from test where name like ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "%" + temp + "%");
ResultSet rs = pstmt.executeQuery(); model = new ResultSetTableModel(rs);
table = new JTable(model);
scrollPane = new JScrollPane(table);
getFrame().add(scrollPane, BorderLayout.CENTER);
getFrame().validate();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
FinalTable ft = new FinalTable();
ft.getFrame();
}
}class ResultSetTableModel extends AbstractTableModel
{
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet rs)
{
this.rs = rs;
try
{
rsmd = rs.getMetaData();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getColumnName(int c)
{
String[] str = {"id","名字","年龄","整型num","字符型num"};
try
{
return str[c];
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public Object getValueAt(int r, int c)
{
try
{
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public boolean isCellEditable(int row, int column)
{
return true;
}
public void setValueAt(Object aValue, int row, int column)
{
try
{
rs.absolute(row + 1);
rs.updateObject(column + 1, aValue);
rs.updateRow();
fireTableCellUpdated(row, column);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
code=SQL]
create table test
(
id int primary key auto_increment,
name varchar(255),
age int,
i_num int,
s_num varchar(255)
);insert into test values(null, '123', 13, 123)[
[/code]
this.jTable1.getModel().addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) {
int col = e.getColumn();
int row = e.getLastRow();
Object obj = jTable1.getModel().getValueAt(row, col);
System.out.println("row:"+row);
System.out.println("col:"+col);
String message = obj.toString()+",row:"+row+",col:"+col;
JOptionPane.showMessageDialog(rootPane, message);
//throw new UnsupportedOperationException("Not supported yet.");
}
});
Vector v=null;
***Dao dao = new ***Dao();
Vector v = dao.***()方法
Iterator i = v.iterator();
model = (DefaultTableModel)(所在Frame).table.getModel();
***************************************
int row = model.getRowCount();
for(int i = row-1;i>=0;i--){ //更新行
model.romove(i);
}
while(i.hasNext()){
**Vo vo = new **Vo();
Object[] data = {vo.getid(),*****}; //vo里的get方法
model.add(data);
}