import java.sql.*;   //<-- Este pacote é muito importante, pois e´ ele quem possui as classes de conexão
import java.awt.*;

public class TJDBC extends Frame 
{
	public Connection con;
	public ResultSet rs;
	public Statement stmt;
	Panel panelScroll = new Panel();
	public ScrollPane soficinas;

	public  TJDBC()
	{
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
			//Driver JDBC-ODBC
			con  = DriverManager.getConnection("jdbc:odbc:JURL","admin","admin"); //Estes paramentros correspondem a ponte de conexao:DSN do banco que vc colocou no ODBC, o login e a senha.
			stmt = con.createStatement();
			//Isto é uma consulta usando SQL ==> Select count(*) from JTable == Conte quantas linhas tem a tabela
			rs = stmt.executeQuery("Select count(*) from JTable");
			rs.next();  //Dar um next para que o cursor fique posicionado na primeira linha da tabela
			int rows = Integer.parseInt(rs.getString(1));
			panelScroll.setLayout(new FlowLayout(FlowLayout.CENTER));
			Panel panelOficinas = new Panel();
			if (rows > 0)
			{
				panelOficinas.setLayout(new GridLayout((rows+1),3,1,1));
				Label cabec1 = new Label("Nome");
				Label cabec2 = new Label("Endereco");
				Label cabec4 = new Label("Cep");
				cabec1.setBackground(Color.gray);
				cabec2.setBackground(Color.gray);
				cabec4.setBackground(Color.gray);
				cabec1.setForeground(Color.white);
				cabec2.setForeground(Color.white);
				cabec4.setForeground(Color.white);
				panelOficinas.add(cabec1);
				panelOficinas.add(cabec2);
				panelOficinas.add(cabec4);
				rs = null;
				rs = stmt.executeQuery("Select * from JTable");
				while (rs.next())
				{
					panelOficinas.add(new Label(rs.getString("Nome") ));
					panelOficinas.add(new Label(rs.getString("Endereco")));
					panelOficinas.add(new Label(rs.getString("Cep")));
				}
			}
			panelOficinas.setSize(560,140);
			rs = null;
			soficinas = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
			soficinas.setSize(560,160);
			soficinas.setBackground(Color.lightGray);
			soficinas.add(panelOficinas);
			panelScroll.add(soficinas);
			this.add(panelScroll);
		}catch(SQLException ex){
			ex.printStackTrace();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}


	public static void main(String argv[])
	{
		TJDBC J = new TJDBC();
		J.setSize(570,170);
		J.show();
	}
}

