当前位置: 代码迷 >> Java相关 >> [分享]JTable添加数据库表实例
  详细解决方案

[分享]JTable添加数据库表实例

热度:204   发布时间:2006-12-07 18:03:56.0
[分享]JTable添加数据库表实例

自己搞了好久在高手的帮助下写出来了,和大家一起共享!

package untitled22;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.*;
import javax.swing.JTable;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {

Untitled1 un = new Untitled1();

JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(un);

public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jScrollPane1.getViewport().add(jTable1);
contentPane.add(jScrollPane1, java.awt.BorderLayout.CENTER);
}

public static void main (String [] args)
{
Frame1 f = new Frame1();
f.setVisible(true);
}

}


package untitled22;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;
import java.sql.*;
import javax.swing.JOptionPane;

public class Untitled1 extends AbstractTableModel {

private Vector title;
private Vector data;
Connection con;
Statement st;
ResultSet rs;
ResultSetMetaData rsmd;

public Untitled1() {
super();
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void myTable() {
try {
String str = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(str);
} catch (ClassNotFoundException ce) {
JOptionPane.showMessageDialog(null, ce.toString());
}
try {
con = DriverManager.getConnection("jdbc:odbc:NetBar");
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM Card");
rsmd = rs.getMetaData();

for (int i = 0; i < rsmd.getColumnCount(); i++) {
title.add(rsmd.getColumnLabel(i + 1));
}

while (rs.next()) {
for (int j = 0; j < rsmd.getColumnCount(); j++) {
data.add(rs.getString(j + 1));
}
}
} catch (SQLException se) {
JOptionPane.showMessageDialog(null, se.toString());
}
}

public int getColumnCount() {
return title.size();
}

public int getRowCount() {
return data.size() / getColumnCount();
}

public Object getValueAt(int rowIndex, int columnIndex) {
return data.get((rowIndex * getColumnCount()) + columnIndex);
}

public String getColumnName(int columnIndex) {
String colName = new String();
if (columnIndex <= getColumnCount()) {
colName = (String) title.get(columnIndex);
}
return colName;
}

private void jbInit() throws Exception {
title = new Vector();
data = new Vector();
this.myTable();
}
}

[此贴子已经被作者于2006-12-7 22:21:34编辑过]

搜索更多相关的解决方案: import  数据库  JTable  表实  awt  

----------------解决方案--------------------------------------------------------

晕```没有main方法,怎么运行啊???


----------------解决方案--------------------------------------------------------
这个是直接放在JB里的  我把代码粘出来的!上面那个我改好了!其实这段代码也不全!还有一些功能没有实现!只是放出来让大家指点指点还有什么不足!呵呵

[此贴子已经被作者于2006-12-7 22:23:29编辑过]



----------------解决方案--------------------------------------------------------
  相关解决方案