JTable.getModel() TableModel.getValueAt(int row, int col)getValueAt public Object getValueAt(int rowIndex, int columnIndex) Returns the value for the cell at columnIndex and rowIndex. Parameters: rowIndex - the row whose value is to be queried columnIndex - the column whose value is to be queried Returns: the value Object at the specified cell
如果是直接填好多,可以试试: http://www.finereport.com
//给你个类; //在你主程序中把 DefaultTableModel 直接改成 MyTableModel 就行了; //例:MyTableModel model = new MyTableModel(jTable); import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector;import javax.swing.JOptionPane; import javax.swing.table.*;public class MyTableModel extends DefaultTableModel { private static final long serialVersionUID = 8789179349316472284L; Vector body = null; public MyTableModel(Vector body) { this.body = body; } public void setValueAt(Object aValue, int row, int column) { Vector selectedRow = (Vector) body.get(row); String name = (String) selectedRow.get(0); String phone = (String) selectedRow.get(1); super.setValueAt(aValue,row,column); String toBeUpdate = null; String newValue = (String)aValue;
if(column==1){ toBeUpdate = "phone"; } else if(column ==2) { toBeUpdate = "address"; } try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=mydata;user=sa;password=sa"); Statement sta = con.createStatement(); int i = sta.executeUpdate("update my set "+toBeUpdate+" = '"+newValue+"' where name='"+name+"' and phone='"+phone+"'"); System.out.println(name+phone); sta.close(); con.close(); JOptionPane.showMessageDialog(null,"成功修改了"+i+"行!");
TableModel.getValueAt(int row, int col)getValueAt
public Object getValueAt(int rowIndex,
int columnIndex)
Returns the value for the cell at columnIndex and rowIndex. Parameters:
rowIndex - the row whose value is to be queried
columnIndex - the column whose value is to be queried
Returns:
the value Object at the specified cell
//在你主程序中把 DefaultTableModel 直接改成 MyTableModel 就行了;
//例:MyTableModel model = new MyTableModel(jTable);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;import javax.swing.JOptionPane;
import javax.swing.table.*;public class MyTableModel extends DefaultTableModel {
private static final long serialVersionUID = 8789179349316472284L;
Vector body = null;
public MyTableModel(Vector body) {
this.body = body;
}
public void setValueAt(Object aValue, int row, int column) {
Vector selectedRow = (Vector) body.get(row);
String name = (String) selectedRow.get(0);
String phone = (String) selectedRow.get(1);
super.setValueAt(aValue,row,column);
String toBeUpdate = null;
String newValue = (String)aValue;
if(column==1){
toBeUpdate = "phone";
} else if(column ==2) {
toBeUpdate = "address";
}
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=mydata;user=sa;password=sa");
Statement sta = con.createStatement();
int i = sta.executeUpdate("update my set "+toBeUpdate+" = '"+newValue+"' where name='"+name+"' and phone='"+phone+"'");
System.out.println(name+phone);
sta.close();
con.close();
JOptionPane.showMessageDialog(null,"成功修改了"+i+"行!");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public boolean isCellEditable(int row, int column) {
if(column==0)
return false;
else
return true;
}
}