Could someone provide me with an example or tutorial on how to import a data from a mysql database within a JTable within the use of a GUI. I tried looking for an example but have not found anything.
Asked
Active
Viewed 4,693 times
0
-
1This question has only been asked and answered 1000 times before. Since you state that previous examples haven't helped but don't tell us how or why they don't help, or what you've tried or how it doesn't work, I doubt that any of our answers will help you any more than they did. – Hovercraft Full Of Eels Mar 07 '12 at 15:11
-
possible duplicate of [How to fill data in a JTable with database?](http://stackoverflow.com/questions/2192764/how-to-fill-data-in-a-jtable-with-database) – Hovercraft Full Of Eels Mar 07 '12 at 15:13
1 Answers
3
Hopefully we can put this question to rest
Connection db = DriverManager.getConnection( jdbc:mysql://192.168.0.3:3306,<user>,<password>);
Statement stmt = db.createStatement();
PreparedStatement psmt = con.prepareStatement("SELECT * FROM DB");
ResultSet rs = psmt.executeQuery();
// get column names
int len = rs.getMetaData().getColumnCount();
Vector cols= new Vector(len);
for(int i=1; i<=len; i++) // Note starting at 1
cols.add(rs.getMetaData().getColumnName(i));
// Add Data
Vector data = new Vector();
while(rs.next())
{
Vector row; = new Vector(len);
for(int i=1; i<=len; i++)
{
row.add(rs.getString(i));
}
data.add(row);
}
// Now create the table
JTable table = new JTable(data, cols);

chollida
- 7,834
- 11
- 55
- 85