Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

To Get Data In Jtable From Database


7 replies to this topic

#1 tinoymalayil

    Premium Member

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 153 posts
  • Gender:Male
  • Location:India
  • myCENT:14.1

Posted 20 July 2009 - 08:25 AM

How to get data from a database table to a java JTable...
I am doin a project in Java.I need this..
Please replyif anyone knows

#2 sonesay

    |||[ n00b King ]|||

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 958 posts
  • Gender:Male
  • Location:Auckland
  • myCENT:52.71

Posted 20 July 2009 - 10:40 AM

Search Jtable and Database in google and you should come up with plenty of links and examples. Try them out and let us know how it goes.

Good luck.

#3 tinoymalayil

    Premium Member

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 153 posts
  • Gender:Male
  • Location:India
  • myCENT:14.1

Posted 01 August 2009 - 05:00 AM

use jav.lang.Vector class is used to hold the data from the database.but in some cases all of the data will be inserted in a single row.How to split the data into different rows?

#4 mahesh2k

    Trap Double Mocha Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,347 posts
  • Gender:Male
  • Location:Valley of Darkness
  • myCENT:67.95
  • Spam Patrol

Posted 01 August 2009 - 06:22 PM

Some pointers for you, JDBC + JTable Google results


* Hacking Swing: A JDBC Table Model
* Mastering the JTable
* Making SQL Queries with JDBC and Displaying Results with Swing


As suggested earlier if you're working with small set of data then you can use java.lang.Vector or you can TableModel and then you can show it on JTable. Hope this will help.

#5 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 23 February 2010 - 08:12 AM

Query about jdbc connectivityTo Get Data In Jtable From Database

Hi, I am really in a need of jdbc connectivity in net beans. Actually I hav created the login form module using Jframe and created , connected the database by Mysql, now I need by using which methodology I can retrieve data into my modle from database.. Pl pl reply soon if anybody know it!

-reply by Vandhena.K

#6 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 03 March 2010 - 11:25 PM

Query about jdbc connectivityTo Get Data In Jtable From Database

Not sure what your actual question is, or if you are lost and just want someone to give you complete code, probably that - here is an example from the web.  If you want to be effective with JDBC and Swing, there is no substitute for learning it though.

http://www.Java2s.Co...tandJTable.Htm 

-reply by Barry

 



#7 Guest_Jitendra Kumar_*

  • Guests

Posted 29 July 2010 - 07:23 AM

View Posttinoymalayil, on 20 July 2009 - 08:25 AM, said:

How to get data from a database table to a java JTable...
I am doin a project in Java.I need this..
Please replyif anyone knows

Contect to me by Email

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.Vector;
import java.sql.*;

public class GetDataFromDatabaseinJtable extends JFrame
{
public static void main( String [] argv ) throws Exception
{
GetDataFromDatabaseinJtable frame = new GetDataFromDatabaseinJtable();
frame.setSize(1000,700);
frame.setLocation(200,100);
frame.setTitle("Stock Report");

frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel( new BorderLayout() );

//Connection establishment to the database
String username = "";
String password = "";
String Database = "jdbc:odbc:DBMS";
Connection Conn;

Conn = DriverManager.getConnection( Database, username, password );
System.out.println("*** Connect to the database ***");

//Access Rows and Columns from database
Vector<String> Columns = new Vector<String>();
Vector<Vector<Object>> Rows = new Vector<Vector<Object>>();
String Query = "Select * from Stock";

Statement smnt = Conn.createStatement();
ResultSet results = smnt.executeQuery( Query );
ResultSetMetaData metaDt = results.getMetaData();

int cols = metaDt.getColumnCount();
Columns.clear(); // clear unwanted value if exist any in Columns variable.

//Get Columns From database
for (int i = 1; i <= cols; i++)
{
Columns.addElement(metaDt.getColumnName(i));
}

Rows.clear(); // clear unwanted value if exist any in Rows variable.
//Get RowsNames From database
while( results.next())
{
Vector<Object> row = new Vector<Object>(cols);
for (int i = 1; i <= cols; i++)
{
row.addElement(results.getObject(i));
}
Rows.addElement(row);
}

results.close(); //close Resultset.
smnt.close(); //close Statement.

//Define TableModel
TableModel tmodel = new DefaultTableModel(Rows, Columns){ };

// JTable Creation
JTable Table = new JTable(tmodel);
JScrollPane scroll = new JScrollPane(Table);
jPanel.add(scroll);
frame.add(jPanel, BorderLayout.CENTER);
frame.setVisible(true);
Conn.commit();
Conn.close(); //Close Connection to the database

}
}

#8 Guest_John-Paul(Ghana)_*

  • Guests

Posted 18 February 2011 - 03:57 PM

I used netbeans IDE to designed the form and the jtable. I used drag and drop in netbeans
private void getConnection()
{

//your connection code here

}
Connection con = getConnection();
            try {
                ResultSet rows;
                Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                String select = "Select * from project";
                rows = s.executeQuery(select);
                int i = 0;
                while (rows.next()){
                    String projName = rows.getString("projectName");
                    String status = rows.getString("status");
                    String date = rows.getString("date");
                    String timeAllocated = rows.getString("timeAllocated");
                    String timeSpent = rows.getString("timeSpent");
                    String billing = rows.getString("billing");
                    
                    //calling data into jtable for display
                    projectTable.setValueAt(projName, i, 0);
                    projectTable.setValueAt(status, i, 1);
                    projectTable.setValueAt(date, i, 2);
                    projectTable.setValueAt(timeAllocated, i, 3);
                    projectTable.setValueAt(timeSpent, i, 4);
                    projectTable.setValueAt(billing, i, 5);
                    i++;
                }
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }

    }





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users