表格模型绑定数据库--实验一
主界面类:TableModel package Test;
import java.awt.BorderLayout;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableModel {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ResultSetFrame frame = new ResultSetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ResultSetFrame extends JFrame {
private JScrollPane scrollPane_1;
private ResultSetTableModel model;
public ResultSetFrame() {
setTitle("表格模型绑定数据库");
setSize(400, 300);
dbsql db = new dbsql();
db.connect();
String sql = "Select Id,zxh,zbh from zc";
ResultSet rs = db.query(sql);
model = new ResultSetTableModel(rs);
getContentPane().add(getScrollPane_1(), BorderLayout.CENTER);
}
/**
* @return
*/
protected JScrollPane getScrollPane_1() {
if (scrollPane_1 == null) {
scrollPane_1 = new JScrollPane();
JTable table = new JTable(model);
scrollPane_1.setViewportView(table);
}
return scrollPane_1;
}
}
表格模型类---ResultSetTableModel
package Test;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
public class ResultSetTableModel extends AbstractTableModel {
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet aResultSet) {
rs = aResultSet;
try {
rsmd = rs.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
}
}
public String getColumnName(int c) {
try {
return rsmd.getColumnName(c + 1);
} catch (SQLException e) {
e.printStackTrace();
return "";
}
}
public int getColumnCount() {
try {
return rsmd.getColumnCount();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
public Object getValueAt(int r, int c) {
try {
rs.absolute(r + 1);
return rs.getObject(c + 1);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public int getRowCount() {
try {
rs.last();
return rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
}
数据库类--JavaBean模式
package Test;
import java.sql.Connection;
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class dbsql {
Connection con = null;
String user = "sa";
String pwd = "";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=zcmt";
public boolean connect() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
return true;
}
public ResultSet query(String sql) {
ResultSet rs = null;
Statement st;
if (con == null) {
connect();
}
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(sql);
return rs;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
return null;
}
}
public boolean update(String sql) {
Statement st;
int mcount;
if (con == null) {
connect();
}
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
mcount = st.executeUpdate(sql);
if (mcount < 1) {
return false;
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
return true;
}
public boolean close() {
try {
con.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
con = null;
return false;
}
}