Solved

how to fill JTable with data from MySql table

Posted on 2010-09-13
5
1,022 Views
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...

Thanks.
0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

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

http://www2.sys-con.com/itsg/virtualcd/Java/archives/0607/hendry/index.html
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33669581
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 33671916
0
 

Author Comment

by:shragi
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";
			Class.forName(driverClass);
			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++)
			columns.add(md.getColumnName(i));

			Vector dataV = 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));
			}
			dataV.add(row);
			}
			st.close();
			conn.close();
			
			table = new JTable(dataV, columns);
			
			 JFrame d = new JFrame(frame, false);
	         d.getContentPane().add(new JScrollPane(table));
	         d.pack();
	         d.setVisible(true);

Open in new window

0
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 33673379
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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