Como inserir dados de uma tabela em um JTable que está em um Applet?

0 respostas
V

Pessoal to com um problema, to tentando inserir dados de uma tabela em um JTable que vai ficar em um applet, a tabela aparece no applet mas os dados não, sou leigo ainda no assunto, mas to tentando aprender, se alguém puder dar uma olhada no fonte e me dizer o que devo fazer eu agradeço e muito. Por favor me ajudem, Víctor Castro

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;

import java.util.<em>;

import java.sql.</em>;

import java.awt.<em>;

import javax.swing.</em>;
public class AppletBD extends JApplet {

public Connection conexao;

JLabel label;

JTable tabela;

JList list;

DefaultListModel NovaLista = new DefaultListModel();
public static void main(String args[]) {
	AppletBD Principal = new AppletBD();
	Principal.connect();
	Principal.criarTabela();
}

public void init() {
	Container container = getContentPane();
	container.setLayout( new FlowLayout() );
	
	label = new JLabel( "TESTE" );
	container.add( label );
	
	tabela = new JTable();
	container.add( new JScrollPane( tabela ) );
}

public void connect() {
	String url = "jdbc:odbc:Db_Tool";
	String login = "";
	String senha = "";
	
	try {
		Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
		
		conexao = DriverManager.getConnection(url, login, senha);
	} 
	
	catch (ClassNotFoundException ref) {
		System.err.println("Falha ao carregar o driver JDBC/ODBC");
		System.exit(1);
	}

	catch (SQLException sq) {
		sq.printStackTrace();
		System.err.println("Impossível conectar");
	}
}

public void criarTabela() {
	Statement st;
	ResultSet resultado;

	try {
		st = conexao.createStatement();

		String consulta = "SELECT * FROM Tabela";

		resultado = st.executeQuery( consulta );
		mostraResultado( resultado );
		st.close();
	}
	
	catch( SQLException sq ) {
		sq.printStackTrace();
		System.err.println( "Caught SQLException : \n" + sq.toString() );
	}
}

public void mostraResultado( ResultSet rs ) throws SQLException {
	//posiciona no primeiro registro dos resultados obtidos
	boolean reg = rs.next();

	//verifica se há registros a serem exibidos
	if( !reg ) {
		JOptionPane.showMessageDialog( this, "Não há mais registros" );
		//setTitle( "Não há registros para serem exibidos" );
		return;
	}
	
	Vector coluna = new Vector();
	Vector linha = new Vector();
	
	try {
		ResultSetMetaData info = rs.getMetaData();

		for( int i = 1; i <= info.getColumnCount(); i++ )
			coluna.addElement( info.getColumnName( i ) );
			
		do {
			linha.addElement( proxLinha( rs, info ) );
		} while ( rs.next() );
		
		tabela = new JTable( linha, coluna );
	}

	catch( SQLException sq ) {
		sq.printStackTrace();
	}
}

public Vector proxLinha( ResultSet rs, ResultSetMetaData rm ) throws SQLException
{

	Vector lin = new Vector();

	for( int i = 1; i <= rm.getColumnCount(); i++ )
		switch( rm.getColumnType( i ) )
		{
			case Types.TIMESTAMP:
				lin.addElement( rs.getTimestamp(i) );
				break;
			case Types.CHAR:
			case Types.VARCHAR:
				lin.addElement( rs.getString(i) );
				break;
			case Types.INTEGER:
				lin.addElement( new Long( rs.getLong( i ) ) );
				break;
			default:
				System.out.println( "O tipo era: " + rm.getColumnTypeName( i ) );
		}
			return lin;
}

}

Criado 17 de março de 2005
Respostas 0
Participantes 1