我正在尝试在JTable
中显示数据库中的数据。
这是我班主任ActionListener
的一部分......
if(e.getSource()==jjb)
{
String j[] = null;
Vector r = new Vector();
Vector finl = new Vector();
try
{
d10.connection();
int b = d10.getclmncnt();
for(int g = 1; g<=b+1;g++)
{
j = new String[g];
}
for(int g = 1; g<=b;g++)
{
j[g] = d10.gettableclmn(g);
}
Vector v = new Vector();
for(int g = 1; g<=b;g++)
{
v.addElement(j[g]);
}
int a = d10.getrwcnt();
System.out.println("no of rows are :::: "+a);
r = d10.getalldata(b);
JTable t = new JTable(r,v);
t.setLayout(null);
TableColumn tc;
for(int hh = 0;hh<t.getColumnCount();hh++)
{
tc = t.getColumnModel().getColumn(hh);
tc.setWidth(75);
}
JScrollPane js = new JScrollPane(t);
js.setBounds(10, 100, 1000, 500);
p5.add(js);
p5.repaint();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
这是我与数据库连接的类...
public void connection()throws SQLException, ClassNotFoundException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded");
Properties p = new Properties();
p.setProperty("user","system");
p.setProperty("password", "oracle10");
p.setProperty("url","jdbc:oracle:thin:@Suneel-PC:1521:XE");
c = DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"),p.getProperty("password"));
System.out.println("connected");
}
public String gettableclmn(int b2)
{
String s = null;
int fh = b2;
try
{
Statement ss = c.createStatement();
ResultSet rs = ss.executeQuery("select * from data");
ResultSetMetaData rsmd = rs.getMetaData();
s = rsmd.getColumnName(fh);
System.out.println("columns names are "+rsmd.getColumnName(fh));
}
catch (SQLException e)
{
e.printStackTrace();
}
return s;
}
public int getclmncnt()
{
int h = 0;
ResultSetMetaData rsmd;
try {
Statement ss = c.createStatement();
ResultSet rs = ss.executeQuery("select * from data");
rsmd = rs.getMetaData();
h = rsmd.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
}
return h;
}
public Vector getalldata(int b)
{
int d = b;
String j = null;
Vector rr = new Vector();
Vector v = new Vector(d);
try
{
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from data");
while(r.next())
{
v.add(r.getInt(1));
v.add(r.getInt(2));
v.add(r.getInt(3));
v.add(r.getInt(4));
v.add(r.getInt(5));
v.add(r.getInt(6));
v.add(r.getInt(7));
v.add(r.getInt(8));
v.add(r.getInt(9));
v.add(r.getInt(10));
v.add(r.getInt(11));
v.add(r.getString(12));
}
rr.add(v);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rr;
}
public int getrwcnt()
{
ResultSet r;
int j = 0;
try
{
Statement s = c.createStatement();
r = s.executeQuery("select count (*) from data");
r.next();
j = r.getInt(1);
}
catch (SQLException e)
{
e.printStackTrace();
}
return j;
}
当我运行此代码时,JTable
中只会显示第一个结果,但是当我调用function d10.getrwcnt();
时,它会将输出作为79
行。我的代码有什么问题?
答案 0 :(得分:1)
您的问题是getalldata
方法的循环。
Vector v = new Vector(d);
try {
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select * from data");
while(r.next())
//Every time when this loop gets executed you are adding
//data to the same vector v, instead one instance depicts one row in database,
//so you will have to create a new instance everytime.
{
v = new Vector(d); //create an instance for every row in database.
v.add(r.getInt(1));
v.add(r.getInt(2));
v.add(r.getInt(3));
v.add(r.getInt(4));
v.add(r.getInt(5));
v.add(r.getInt(6));
v.add(r.getInt(7));
v.add(r.getInt(8));
v.add(r.getInt(9));
v.add(r.getInt(10));
v.add(r.getInt(11));
v.add(r.getString(12));
rr.add(v); // add the values here
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:1)
移动第一行和最后一行
Vector v = new Vector(d);
....
while(r.next()) {
v.add(r.getInt(1));
v.add(r.getInt(2));
...
v.add(r.getString(12));
}
rr.add(v);
在循环内部,如下所示,
while(r.next()) {
Vector v = new Vector(d);
v.add(r.getInt(1));
v.add(r.getInt(2));
...
v.add(r.getString(12));
rr.add(v);
}
而且,我可以看到你的代码很FUBAR,请不要介意。你可以做几件事,
resultSet
Vector
,改为使用ArrayList
,尤其是当Vector不能用于任何目的时b
,g
,j
等。答案 2 :(得分:1)
只有点,因为你没有搜索关于
的内容Vector如何运作
JTable如何运作
LayotManagers如何运作
阅读相关问题(右下角)
不要重新发明轮子,搜索ResultSetTableModel或TableFromDatabase
您在Swing中遇到Concurency问题