| |
|
Welcome to KnowledgeSutra - Dear Guest | |
To Get Data In Jtable From Database
#4
Posted 01 August 2009 - 06:22 PM
* 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
Posted 23 February 2010 - 08:12 AM
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
Posted 03 March 2010 - 11:25 PM
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_*
Posted 29 July 2010 - 07:23 AM
tinoymalayil, on 20 July 2009 - 08:25 AM, said:
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)_*
Posted 18 February 2011 - 03:57 PM
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

1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users















