在這裏,我連我的數據庫:如何在數據庫中刷新gui中的JTable?
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沒有得到更新後刷新一排
修復代碼的格式會真的有所幫助... – 2012-02-10 21:29:39
並且您還有一個巨大的SQL注入案例:\t'String updateq =「update M_department set」+ getColumnName(column)+「='」+ value。 toString()+「'其中 code ='」+ getValueAt(row,0)+「'」;'。你應該使用準備語句而不是構建sql。 – 2012-02-10 21:57:45
您發佈的代碼是混亂的嘗試閱讀和理解的方式。我唯一的建議是''創建組件一次'''。當你想改變模型中的數據時,你重新創建'TableModel',然後執行'table.setModel(...)'。下次我甚至不會嘗試讀取剛纔發佈的未格式化的代碼。 – camickr 2012-02-10 21:58:04