Question:
How to show data from database into JTable.Plz give me its code.I will be thankfull?
2008-11-28 23:04:45 UTC
import java.sql.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.Vector;

public class SimpleTable extends JFrame {
public SimpleTable() {
super("Simple JTable Test");
setSize(350, 200);
setLocation(250,300);
int ColCount;
int i;
int h;
String[] headers;

try
{
Class.forName(
"com.mysql.jdbc.Driver").newInstance() ;
Connection conDB
=DriverManager.getConnection(

"jdbc:mysql://localhost/matx?user=root&password=");
ResultSet rset1 = null;
Statement stmt1 = null;
Statement stmt2 = null;
String ItemCode = null;
String Description = null;
String ItemGroup = null;
stmt1 = conDB.createStatement( );

rset1 = stmt1.executeQuery("select * from tmatx");

Vector cache;
cache = new Vector();
ResultSetMetaData meta =
rset1.getMetaData();
ColCount=meta.getColumnCount();

while (rset1.next())
{

String [] record= new
String[ColCount];
for (i=0; i{

record[i]=rset1.getString(i+1);
}

ItemCode = rset1.getString("ItemCode");
Description = rset1.getString( "Description");
ItemGroup = rset1.getString( "ItemGroup");
}

JTable jt = new JTable(new String[][]{
{ItemCode,Description}}, new String[]{"ItemCode","Description"});

JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp,
BorderLayout.CENTER);
}
catch (Exception e)
{
System.out.println("failure to connect " + e);
return; //(exit(1));
}
}
public static void main(String args[]) {
SimpleTable st = new SimpleTable();
st.setVisible(true);
}
}
Three answers:
deonejuan
2008-11-29 02:56:22 UTC
You will have to use AbstractTableModel.

You should use a Vector;

The Query will make Objects to populate Vector



Vector v = new Vector();

for(int i = 0; i
v.add( new StockItem( stmtOne,StmtTwo,1.12)...



With a loaded Vector, you setData on Model. Model will have the following...

class StockItemModel extends AbstractTableModel {

// add custom columns, for width and headers

//

public int getRowCount() {

return v==null ? 0 : v.size();

}

public int getColumnCount() {

return m_columns.length;

}

public String getColumnName( int column ) {

return m_columns(column).m_title;

}

// and the most important one

public Object getValueAt( int nRow, int nCol) {

if( nRow<0 || or nRow >= getRowCount() )

return "";

StockItem row = (StockItem)v.get(nRow);

switch( nCol ) {

case 0: return statementOne;

case 1: return statemenetTwo;

case 3; return pricePerUnit;

}

// you can also setValueAt

// you can also setEditable

// you can also add a CustomCellRenderer to the JTable



That is a lot of information that has to mesh correctly. I would suggest you download a free eBook from Manning Publications demonstrating this with a good code example. "Swing" chapter 18. The first edition of the book is free as a Word97 doc.



The Sun tutorials use an array in their model examples. That is too static for what you are trying to do.



Good luck. It is at first complicated, with a lot of reading, but is not hard.
hawk
2016-10-25 10:31:49 UTC
i position self belief in united statesa. peoples finger prints are taken in case you be conscious for a using licence or a alternative licence. this training is kept on a database.. i imagine what you're suggesting is planned. using RFID radio frequency implants is likewise being studied. Many imagine that is a conspiracy idea or something yet there are distinct large Banking giants pushing for using those quite mastercard giants.. the point is to reduce fraud.. in my opinion i imagine we are able to make sure some style of implant and critical storage of archives collectively with economic employer expenditures and so on interior the nearish destiny. truly it will be a threat to visual reveal unit genuinely everyone. So lets say to illustrate you entered a authorities construction.. they could be able to make sure the identity of each and every man or woman interior the construction and everyone who isn't stated may be flagged and arrested likely as a a threat terrorist. I doubt there'll be cameras everywhere 1984 form.. there would properly be some in significant metropolitan factors as there are literally. an superior segree of surveillence will likely come from satelite variety technologies. even as to many this may seem a gross invasion of privateness and so on.. the safe practices reward will look to the 1000's that are mind-washed into the conflict on terror concern lifestyle that has risen. What you're searching at actually is a device of authorities not that dissimilar to that envisaged via a guy time-honored Adolph Hitler. Very sturdy and managed with decreased liberty in favour of safe practices.
?
2015-04-22 23:19:47 UTC
kindly see this link.



Tutorails-http://naveenrajput10.blogspot.in/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...