在这里我连接我的数据库:
package org.connect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class dbconnect {
public static Connection getConnectionvalue() {
Statement stmt = null;
Connection con = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
con = DriverManager.getConnection(url, "system", "kingdom");
con.setAutoCommit(true);
} catch (Exception e) {
System.out.println("Connection Error" + e);
}
return con;
}
public static Statement
getStatementvalue() {
Statement stmt = null;
Connection con = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
con = DriverManager.getConnection(url, "system", "kingdom");
con.setAutoCommit(true);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println("Connection Error" + e);
}
return stmt;
}
}
这里我创建了框架并获取了数据库表值。然后我将表值的结果集传递给Deptmodeltable
package org.frames.src;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.connect.dbconnect;
import org.reports.master.TESTJASPER;
import org.table.model.DeptTableModel;
import java.awt.event.ActionListener;
import java.io.IOException;
public class DeptFrame extends JFrame implements RowSetListener, ActionListener {
Statement stmt = dbconnect.getStatementvalue();
Connection con = dbconnect.getConnectionvalue();
public static JTable table; // The table for displaying data
public static JScrollPane scrollPane;
public static JPanel mainpanel, panel, panel1;
public static JButton button;
DeptTableModel depttm;
public DeptFrame() throws SQLException {
super("The Masters: Department");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
con.close();
} catch (SQLException sqle) {
System.out.println("windowClosing" + sqle);
}
System.exit(0);
}
}
);
ResultSet rstable = getContentsOfTable();
depttm = new DeptTableModel(rstable);
table = new JTable(); // Displays the table
table.setModel(depttm);
table.setForeground(Color.gray);
table.setBackground(Color.ORANGE);
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
table.setSize(300, 300);
scrollPane = new JScrollPane(table);
button = new JButton("ViewReport");
button.addActionListener(this);
mainpanel = new JPanel();
panel1 = new JPanel();
panel1.add(button);
mainpanel.setLayout(new BorderLayout());
mainpanel.add(scrollPane);
mainpanel.add("South", panel1);
}
/**
* @param args * @throws SQLException
*/
public static void main(String[] args)
throws SQLException {
DeptFrame df = new DeptFrame();
df.add(mainpanel);
df.setSize(700, 700);
df.getContentPane().add(mainpanel);
df.setVisible(true);
}
public ResultSet getContentsOfTable() throws SQLException {
ResultSet rs = null;
try {
rs = stmt.executeQuery("select * from M_department");
} catch (SQLException e) {
System.out.println("Query" + e);
}
return rs;
}
@Override
public void cursorMoved(RowSetEvent arg0) {
}
@Override
public void rowChanged(RowSetEvent arg0) {
}
@Override
public void rowSetChanged(RowSetEvent event) {
}
@Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String action =
e.getActionCommand();
if (action.equals("ViewReport")) {
String[] args = null;
TESTJASPER.main(args);
try {
TESTJASPER.openPdf();
} catch (IOException e1) { // TODO
Auto - generated catch block e1.printStackTrace();
} catch
(InterruptedException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
这里我实现了表模型监听器并设置了JTable值
package org.table.model;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSet;
import javax.sql.RowSetListener;
import javax.sql.rowset.CachedRowSet;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import oracle.jdbc.rowset.OracleCachedRowSet;
import org.connect.dbconnect;
import org.frames.src.DeptFrame;
public class DeptTableModel implements TableModel {
Statement stmt = dbconnect.getStatementvalue();
Connection con = dbconnect.getConnectionvalue();
static ResultSet rsdept;
ResultSetMetaData metadata; // Additional information about the results
int numcols, numrows;
public OracleCachedRowSet ocrs;
public ResultSet getDeptRowSet() {
return rsdept;
}
public DeptTableModel(ResultSet rsarg) throws SQLException {
this.rsdept = rsarg;
this.metadata = this.rsdept.getMetaData();
this.numcols = metadata.getColumnCount();
ocrs = new OracleCachedRowSet();
ocrs.populate(this.rsdept);
ocrs.beforeFirst();
this.numrows = 0;
while (ocrs.next()) {
this.numrows++;
}
ocrs.beforeFirst();
//System.out.println(numrows); //System.out.println(numcols); }
public void close() {
try {
rsdept.getStatement().close();
} catch (SQLException e) {
System.out.print(e);
}
}
/**
* Automatically close when we're garbage collected
*/
protected void finalize() {
close();
}
@Override
public void addTableModelListener(TableModelListener arg0) { // TODO Auto - generated method stub
}
@Override
public Class
getColumnClass(int column) {
return String.class;
}
@Override
public int getColumnCount() {
return numcols;
}
@Override
public String
getColumnName(int column) {
try {
return this.metadata.getColumnLabel(column + 1);
} catch (SQLException e) {
return e.toString();
}
}
@Override
public int getRowCount() {
return numrows;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
ocrs.absolute(rowIndex + 1);
Object o = ocrs.getObject(columnIndex + 1);
if (o == null)
return null;
else
return o.toString();
} catch (SQLException e) {
System.out.print(e);
return e.toString();
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex != 0) {
return true;
} else {
return false;
}
}
@Override
public void
removeTableModelListener(TableModelListener arg0) { // TODO Auto - generated method stub
}
@Override
public void setValueAt(Object value, int row, int column) {
System.out.println("Calling setValueAt row " + row + ", column " + column + " value is" + value.toString());
System.out.println(getColumnName(column));
System.out.println(getValueAt(row, 0));
String updateq = "update M_department set " + getColumnName(column) + "='" + value.toString()
+ "' where code = '" + getValueAt(row, 0) + "' ";
System.out.println(updateq);
try {
stmt.executeUpdate(updateq);
DeptFrame x = new DeptFrame();
new DeptTableModel(x.getContentsOfTable());
} catch (SQLException e) { // TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error" + e);
}
}
}
一切正常,但我的jtable在更新行后没有刷新
答案 0 :(得分:0)
你靠近底部
stmt.executeUpdate(updateq);
DeptFrame x=new DeptFrame();
new DeptTableModel(x.getContentsOfTable());
1)我看到stmt.executeUpdate
来电,但我没有看到任何commit
来电。你有自动提交功能吗?我认为oracle默认关闭了,所以执行正在发生,但是新的框架+模型有旧的内容,因为新内容不会被提交?
2)你为什么要创建*另一个DeptTableModel
而没有参考?看起来DeptFrame
已经在内部创建了一个,那么第二个是什么?