How to Extract records from Mysql database using JTable
I have MySQL 5.0 installed on my computer. I am trying to retrieve information from my MySQL Database and inserting it into a JTable displaying multiple rows. However I am able to retrieving the information from the JTable but is only displaying the last record in the database no matter how many records are in the database.I want to display all the records from the database, i have tried out lot of alternative but nothing works.
please help..
Re: How to Extract records from Mysql database using JTable
Create a Java Application Project. From main menu File -> New Projects. Choose Java category, and Java Application.
Now delete the Main.java file under Source Packages. This file was automatically created by the wizard for you. Add a new JFrame Form . Right Click Project node -> New -> JFrame Form.
Drag and Drop JTable from the palette on to the designer.
Now Drag and drop the database Table on to the JTable in the designer. To do this, go to the Services tab , and expand the Databases -> Your MySQL database node -> Table, and select the table you want to bind to the JTable. Drag and drop it on to the JTable on the designer.
Make sure the MySQL driver jar file is under the libraries node of the project. Build and Run the project. when running the project it will ask you to select the main class. This is because we deleted the main class earlier, and we now need to provide the main class to begin execution. Select the default.
You would see all the records imported into the JTable.
Re: How to Extract records from Mysql database using JTable
Use the following code to extract records from the MYsql database..
Code:
ResultSet result = Statement.executeQuery("SELECT * FROM someTable");
ResultSetMetaData md = result.getMetaData();
int columnCount = md.getColumnCount();
Vector columns = new Vector(columnCount);
//store column names
for(int i=1; i<=columnCount; i++)
columns.add(md.getColumnName(i));
Vector data = new Vector();
Vector row;
//store row data
while(result.next())
{
row = new Vector(columnCount);
for(int i=1; i<=columnCount; i++)
{
row.add(result.getString(i));
}
data.add(row);
}
JTable table = new JTable(data, columns);
Re: How to Extract records from Mysql database using JTable
I'm trying to populate a JTable with data from an online MySQL database. Here's what I've done so far:
1) Add the table to the form
2) Right click on table, select Table Contents, select bound, Import data to form (the connection works and the respective table can be viewed in the services window)
3) Right click on table again, select Bind, select Elements, shift Available to Selected.