how to fill JTable with data from MySql table

Posted on 2010-09-13
Last Modified: 2013-11-23
I am having a JTable and a MySql table.

I knew how to fill the JTable with the array data.  But how can I fill the JTable with MySql table in the backend.

Can anyone provide me some sample code...

Question by:shragi
  • 2
  • 2
LVL 26

Expert Comment

ID: 33669562
you need to use custom table model, check this step by step example,
LVL 26

Expert Comment

ID: 33669581
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33671916

Author Comment

ID: 33672931
I have done it with the below code.

But the problem is there were many many rows in the table and I don't want to show all of them at once.

I want to show only may be around 300 rows each time and have an option below the table like previous page and next page. So that on clicking the next page option it will display the next 300 rows.

How should I do this?
String driverClass = "com.mysql.jdbc.Driver";


			String url  = "jdbc:mysql://localhost:3306/test";

			String username = "root";

			String password = "admin";

			Connection conn = DriverManager.getConnection(url,username,password);

			Statement st = conn.createStatement();

			String query = "select * from " + tableName;

			ResultSet result = st.executeQuery(query);

			ResultSetMetaData md = result.getMetaData();

			int columnCount = md.getColumnCount();

			Vector columns = new Vector(columnCount);

			//store column names

			for(int i=1; i<=columnCount; i++)


			Vector dataV = new Vector();

			Vector row;

			//store row data



			row = new Vector(columnCount);

			for(int i=1; i<=columnCount; i++)









			table = new JTable(dataV, columns);


			 JFrame d = new JFrame(frame, false);

	         d.getContentPane().add(new JScrollPane(table));



Open in new window

LVL 25

Accepted Solution

Tomas Helgi Johannsson earned 500 total points
ID: 33673379

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 32
couple of eclipse 5 25
table example 4 24
How to convert String matching to regex in java 4 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now