Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

VISUALIZAR REGISTROS EN UN JTABLE

3,952 views

Published on

Published in: Education
  • Be the first to comment

VISUALIZAR REGISTROS EN UN JTABLE

  1. 1. VISUALIZAR REGISTROS (BD. NORTHWIND) EN UN JTABLE (NETBEANS – SQL SERVER)<br />CONSULTA<br />select P.ProductID, <br />P.ProductName, <br />C.CategoryName, <br />S.CompanyName, <br />P.UnitPrice, <br />P.UnitsInStock, <br />P.QuantityPerUnit<br />from Products P inner join Categories C <br />on P.CategoryID = C.CategoryID <br /> inner join Suppliers S<br /> on P.SupplierID = S.SupplierID<br />ConnectionManager<br />package examen;<br />import java.sql.Connection;<br />import java.sql.DriverManager;<br />import java.sql.SQLException;<br />public class ConnectionManager {<br /> final private static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";<br /> final private static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=Northwind";<br /> final private static String USER = "sa";<br /> final private static String PASSWORD = "123";<br /> private static Connection cn = null;<br /> private ConnectionManager() {<br /> }<br /> public static Connection getConnection()<br /> throws ClassNotFoundException,<br /> SQLException,<br /> Exception {<br /> if (cn == null) {<br /> try {<br /> Class.forName(DRIVER).newInstance();<br /> cn = DriverManager.getConnection(URL, USER, PASSWORD);<br /> } catch (ClassNotFoundException ex) {<br /> throw ex;<br /> } catch (SQLException ex) {<br /> throw ex;<br /> } catch (Exception ex) {<br /> throw ex;<br /> }<br /> }<br /> return cn;<br /> }<br />}<br />Producto<br />package examen;<br />import java.io.Serializable;<br />public class Producto implements Serializable {<br /> private int ProductId;<br /> private String ProductName;<br /> private String CategoryName;<br /> private String CompanyName;<br /> private double UnitPrice;<br /> private int UnitStock;<br /> private String UnidadMedidad;<br /> public Producto() {<br /> }<br /> public Producto(int ProductId, String ProductName, String CategoryName, String CompanyName, double UnitPrice, int UnitStock, String UnidadMedidad) {<br /> this.ProductId = ProductId;<br /> this.ProductName = ProductName;<br /> this.CategoryName = CategoryName;<br /> this.CompanyName = CompanyName;<br /> this.UnitPrice = UnitPrice;<br /> this.UnitStock = UnitStock;<br /> this.UnidadMedidad = UnidadMedidad;<br /> }<br /> public String getCategoryName() {<br /> return CategoryName;<br /> }<br /> public void setCategoryName(String CategoryName) {<br /> this.CategoryName = CategoryName;<br /> }<br /> public String getCompanyName() {<br /> return CompanyName;<br /> }<br /> public void setCompanyName(String CompanyName) {<br /> this.CompanyName = CompanyName;<br /> }<br /> public int getProductId() {<br /> return ProductId;<br /> }<br /> public void setProductId(int ProductId) {<br /> this.ProductId = ProductId;<br /> }<br /> public String getProductName() {<br /> return ProductName;<br /> }<br /> public void setProductName(String ProductName) {<br /> this.ProductName = ProductName;<br /> }<br /> public String getUnidadMedidad() {<br /> return UnidadMedidad;<br /> }<br /> public void setUnidadMedidad(String UnidadMedidad) {<br /> this.UnidadMedidad = UnidadMedidad;<br /> }<br /> public double getUnitPrice() {<br /> return UnitPrice;<br /> }<br /> public void setUnitPrice(double UnitPrice) {<br /> this.UnitPrice = UnitPrice;<br /> }<br /> public int getUnitStock() {<br /> return UnitStock;<br /> }<br /> public void setUnitStock(int UnitStock) {<br /> this.UnitStock = UnitStock;<br /> }<br />}<br />ProductoDAO <br />package examen;<br />import java.sql.CallableStatement;<br />import java.sql.Connection;<br />import java.sql.PreparedStatement;<br />import java.sql.ResultSet;<br />import java.sql.SQLException;<br />import java.sql.Statement;<br />import java.util.ArrayList;<br />public class ProductoDAO {<br /> private Connection cn = null;<br /> private Statement st = null;<br /> private PreparedStatement ps = null;<br /> private CallableStatement cs = null;<br /> private ResultSet rs = null;<br /> public ArrayList<Producto> getProducto() {<br /> ArrayList<Producto> productos =<br /> new ArrayList<Producto>();<br /> final String QUERY = "select p.ProductID , p.ProductName, C.CategoryName, s.CompanyName, p.UnitPrice, p.UnitsInStock, p.QuantityPerUnit from Products p inner join Categories c on p.CategoryID = C.CategoryID inner join Suppliers s on p.SupplierID = s.SupplierID ";<br /> try {<br /> cn = ConnectionManager.getConnection();<br /> st = cn.createStatement();<br /> rs = st.executeQuery(QUERY);<br /> while (rs.next()) {<br /> Producto c = new Producto(<br /> rs.getInt(1),<br /> rs.getString(2),<br /> rs.getString(3),<br /> rs.getString(4),<br /> rs.getDouble(5),<br /> rs.getInt(6),<br /> rs.getString(7));<br /> productos.add(c);<br /> }<br /> } catch (SQLException ex) {<br /> } catch (Exception ex) {<br /> } finally {<br /> try {<br /> if (rs != null) {<br /> rs.close();<br /> }<br /> if (st != null) {<br /> st.close();<br /> }<br /> if (cn != null) {<br /> cn.close();<br /> }<br /> } catch (Exception ex) {<br /> }<br /> }<br /> return productos;<br /> }<br />}<br />CODIGO FUENTE<br />package examen;<br />import java.util.ArrayList;<br />import java.util.Vector;<br />import javax.swing.table.DefaultTableModel;<br />import javax.swing.table.TableColumn;<br />public class FrmConsultaProductos extends javax.swing.JFrame {<br /> private DefaultTableModel dtm;<br /> public FrmConsultaProductos() {<br /> initComponents();<br /> configurartabla();<br /> cargartabla();<br /> }<br />public static void main(String args[]) {<br /> java.awt.EventQueue.invokeLater(new Runnable() {<br /> public void run() {<br /> new FrmConsultaProductos().setVisible(true);<br /> }<br /> });<br /> }<br /> // Variables declaration - do not modify <br /> private javax.swing.JScrollPane jScrollPane1;<br /> private javax.swing.JTable tblProducto;<br /> // End of variables declaration <br /> private void cargartabla() {<br /> ProductoDAO bo = new ProductoDAO();<br /> ArrayList<Producto> productos =<br /> bo.getProducto();<br /> dtm = (DefaultTableModel) tblProducto.getModel();<br /> for (Producto c : productos) {<br /> Vector fila = new Vector();<br /> fila.add(c.getProductId());<br /> fila.add(c.getProductName());<br /> fila.add(c.getCategoryName());<br /> fila.add(c.getCompanyName());<br /> fila.add(c.getUnitPrice());<br /> fila.add(c.getUnitStock());<br /> fila.add(c.getUnidadMedidad());<br /> dtm.addRow(fila);<br /> }<br /> }<br /> private void configurartabla() {<br /> TableColumn column = null;<br /> column = tblProducto.getColumnModel().getColumn(0);<br /> column.setPreferredWidth(2);<br /> column = tblProducto.getColumnModel().getColumn(1);<br /> column.setPreferredWidth(150);<br /> column = tblProducto.getColumnModel().getColumn(2);<br /> column.setPreferredWidth(150);<br /> }<br />}<br />

×