2008-11-28 23:04:45 UTC
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.Vector;
public class SimpleTable extends JFrame {
public SimpleTable() {
super("Simple JTable Test");
setSize(350, 200);
setLocation(250,300);
int ColCount;
int i;
int h;
String[] headers;
try
{
Class.forName(
"com.mysql.jdbc.Driver").newInstance() ;
Connection conDB
=DriverManager.getConnection(
"jdbc:mysql://localhost/matx?user=root&password=");
ResultSet rset1 = null;
Statement stmt1 = null;
Statement stmt2 = null;
String ItemCode = null;
String Description = null;
String ItemGroup = null;
stmt1 = conDB.createStatement( );
rset1 = stmt1.executeQuery("select * from tmatx");
Vector cache;
cache = new Vector();
ResultSetMetaData meta =
rset1.getMetaData();
ColCount=meta.getColumnCount();
while (rset1.next())
{
String [] record= new
String[ColCount];
for (i=0; i
record[i]=rset1.getString(i+1);
}
ItemCode = rset1.getString("ItemCode");
Description = rset1.getString( "Description");
ItemGroup = rset1.getString( "ItemGroup");
}
JTable jt = new JTable(new String[][]{
{ItemCode,Description}}, new String[]{"ItemCode","Description"});
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp,
BorderLayout.CENTER);
}
catch (Exception e)
{
System.out.println("failure to connect " + e);
return; //(exit(1));
}
}
public static void main(String args[]) {
SimpleTable st = new SimpleTable();
st.setVisible(true);
}
}