ConnectionManager类 /* * ConnectionMange.java * * Created on 2007年9月19日, 下午3:00 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package databasetest.server.action;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;/** * * @author qhj */ public class ConnectionMange {
private static Connection con = null;
/** Creates a new instance of ConnectionMange */ public ConnectionMange() { }
Frame的代码, 用了一个DataTable类, 那个类是继承JTable 的, 后面有/*
* Frame1.java
*
* Created on 2007年9月19日, 下午5:31
*/package databasetest;import databasetest.datatable.DataTable;
import databasetest.server.action.ConnectionMange;
import javax.swing.JTable;/**
*
* @author qhj
*/
public class Frame1 extends javax.swing.JFrame {
private DataTable dataTable1 = null;
/** Creates new form Frame1 */
public Frame1() {
initComponents();
dataTable1 = new DataTable(ConnectionMange.getConnection("AssetManage"), "Select * from borrowTable");
dataTable1.setColumnName(0, "卡 号");
dataTable1.setColumnName(1, "密 码");
dataTable1.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
dataTable1.doLayout();
jScrollPane1.setViewportView(dataTable1);
setTitle("数据表格");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setAlignmentX(2.0F);
jScrollPane1.setAlignmentY(2.0F);
jScrollPane1.setViewport(null); jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
.addGap(10, 10, 10))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame1().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
// 变量声明结束
}
* DataTable.java
*
* Created on 2007年9月19日, 下午5:48
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package databasetest.datatable;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;/**
* 继承自 JTable的1个类,构造方法需要2个参数
* <I>Connection conn</I>
* <I>String strSql</I>
* @author qhj
*/
public class DataTable extends JTable{
private Connection conn = null;
private ResultSet rst = null;
private Statement stmt = null;
private String strSQL;
private innerTableModel myTableModel= null;
/** Creates a new instance of DataTable
*@param Connection SQL连接
*@param strSQL 查询字符串
*/
public DataTable(Connection conn, String strSQL) {
super();
this.conn = conn;
this.strSQL = strSQL;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rst = stmt.executeQuery(strSQL);
myTableModel = new innerTableModel();
setModel(myTableModel);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private DataTable()
{
super();
}
public void setColumnName(int index, String name)
{
myTableModel.setColumnName(index, name);
createDefaultColumnsFromModel(); }
/*----------------------------------------------------
*class innerTableModel
*extends AbstractTableModel
*
----------------------------------------------------*/
class innerTableModel extends AbstractTableModel
{
String [] columnName = null;
boolean isCustomedColumnName = false;
public innerTableModel()
{
try
{
int columnCount = rst.getMetaData().getColumnCount();
this.columnName = new String[columnCount];
for(int i = 0; i < columnCount; i++)
{
columnName[i] = rst.getMetaData().getColumnName(i+1);
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public int getRowCount() {
int rows = 0;
if(rst != null)
{
try {
if(rst.last())
{
rows = rst.getRow();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return rows;
} public int getColumnCount() {
if(rst != null)
{
try {
return rst.getMetaData().getColumnCount();
} catch (SQLException ex) {
ex.printStackTrace();
return 0;
}
}else
{
return 0;
}
} public Object getValueAt(int rowIndex, int columnIndex) {
Object result = null;
try{
rst.absolute(rowIndex +1);
result = rst.getObject(columnIndex + 1);
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
} public String getColumnName(int column) {
String retValue;
retValue = super.getColumnName(column);
try{
retValue = columnName[column];
}
catch(Exception ex)
{
ex.printStackTrace();
}
return retValue;
}
public void setColumnName(int index, String colName)
{
try {
this.columnName[index] = colName;
isCustomedColumnName = true;
} catch(Exception ex){
ex.printStackTrace();
}
}
}
}
/*
* ConnectionMange.java
*
* Created on 2007年9月19日, 下午3:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package databasetest.server.action;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;/**
*
* @author qhj
*/
public class ConnectionMange {
private static Connection con = null;
/** Creates a new instance of ConnectionMange */
public ConnectionMange() {
}
public static Connection getConnection() {
if(con == null) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:odbc:AssetsLogin");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return con;
}
public static Connection getConnection(String odbcName) {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:odbc:"+odbcName.trim());
} catch (SQLException ex) {
ex.printStackTrace();
}
return con;
}
}
惭愧. 分层没做好. DataTable的更新数据没写.
只是求个分, 给别人参考下大概