You are on page 1of 4

:::Conexion::: package untitled1; import java.sql.

*; class conexion { Statement s; PreparedStatement pt; ResultSet rs; Connection cn; conexion(){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.print(":: Se Cargo Correctamente ::"); cn = DriverManager.getConnection("Jdbc:Odbc:miservidor"); }catch(Exception ex){ System.out.print(":: No se pudo Cargar el DRIVER ::"); } try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public ResultSet Mostrar(){ try{ String cadena = "SELECT * FROM TERRENOS"; s = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CO NCUR_UPDATABLE); rs = s.executeQuery(cadena); return rs; }catch(Exception ex){ return null; } } public ResultSet Consulta (int cod){ try { pt = cn.prepareStatement("SELECT * FROM TERRENOS where COD_TERRENO=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); pt.setInt(1,cod); rs = pt.executeQuery(); return rs; }catch(Exception ex){ return null; } } private void jbInit() throws Exception { } }

:::Consulta1::: package untitled1; import java.awt.BorderLayout; import java.sql.*; import javax.swing.JFrame; import javax.swing.JLabel;

import java.awt.*; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class consulta1 extends JFrame { ResultSet rs; conexion cnn; JLabel jLabel1 = new JLabel(); JTextField txt_cod = new JTextField(); JButton jButton1 = new JButton(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JButton bot_mostrar = new JButton(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); public consulta1() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(null); jLabel1.setFont(new java.awt.Font("Arial Black", Font.PLAIN, 11)); jLabel1.setForeground(Color.white); jLabel1.setText("=CODIGO="); jLabel1.setBounds(new Rectangle(26, 17, 84, 15)); jButton1.setBounds(new Rectangle(247, 9, 91, 25)); jButton1.setText("Consulta"); jButton1.addActionListener(new FConsulta01_jButton1_actionAdapter(this)) ; jScrollPane1.setBounds(new Rectangle(14, 59, 371, 194)); jTextArea1.setText(""); bot_mostrar.setBounds(new Rectangle(242, 266, 101, 23)); bot_mostrar.setText("= Mostrar ="); bot_mostrar.addActionListener(new FConsulta01_bot_mostrar_actionAdapter( this)); txt_cod.addActionListener(new FConsulta01_txt_cod_actionAdapter(this)); this.getContentPane().setBackground(Color.black); jLabel2.setFont(new java.awt.Font("Arial", Font.PLAIN, 8)); jLabel2.setForeground(Color.white); jLabel2.setText("COD_TERRENO"); jLabel2.setBounds(new Rectangle(17, 45, 84, 15)); jLabel3.setFont(new java.awt.Font("Arial", Font.PLAIN, 8)); jLabel3.setForeground(Color.white); jLabel3.setText("NOMBRE_TE"); jLabel3.setBounds(new Rectangle(91, 44, 68, 15)); jLabel4.setFont(new java.awt.Font("Arial", Font.PLAIN, 8)); jLabel4.setForeground(Color.white); jLabel4.setText("AREA"); jLabel4.setBounds(new Rectangle(164, 40, 34, 20)); jLabel5.setFont(new java.awt.Font("Arial", Font.PLAIN, 8)); jLabel5.setForeground(Color.white);

jLabel5.setText("COD_DISTRITO"); jLabel5.setBounds(new Rectangle(210, 44, 82, 15)); jLabel6.setFont(new java.awt.Font("Arial", Font.PLAIN, 8)); jLabel6.setForeground(Color.white); jLabel6.setText("COD_AGRICULTOR"); jLabel6.setBounds(new Rectangle(296, 44, 96, 15)); this.getContentPane().add(jScrollPane1); this.getContentPane().add(bot_mostrar); this.getContentPane().add(jLabel2); this.getContentPane().add(jLabel1, null); this.getContentPane().add(txt_cod); this.getContentPane().add(jButton1); this.getContentPane().add(jLabel3); this.getContentPane().add(jLabel4); this.getContentPane().add(jLabel6); this.getContentPane().add(jLabel5); jScrollPane1.getViewport().add(jTextArea1); txt_cod.setBounds(new Rectangle(101, 11, 134, 21)); } public static void main(String arg[]){ consulta1 CN = new consulta1(); CN.setSize(400,400); CN.setTitle("MOSTRAR TERRENOS"); CN.setVisible(true); } public void bot_mostrar_actionPerformed(ActionEvent e) { try{ cnn = new conexion(); rs = cnn.Mostrar(); while(rs.next()){ jTextArea1.append(rs.getString("COD_TERRENO")+" "+r s.getString("NOMBRE_TE")+" "+rs.getString("AREA")+" "+rs.g etString("COD_DISTRITO")+" "+rs.getString("COD_AGRICULTOR")+ "\n"); } }catch(Exception ex){ System.out.print("Error en la Consulta"+ex.getMessage()); } } public void jButton1_actionPerformed(ActionEvent e) { int codigo; codigo=Integer.parseInt(txt_cod.getText()); try{ cnn = new conexion(); rs = cnn.Consulta(codigo); while (rs.next()){ jTextArea1.append(rs.getString("COD_TERRENO")+" "+ rs.getString("NOMBRE_TE")+" "+rs.getString("AREA")+" "+rs .getString("COD_DISTRITO")+" "+rs.getString("COD_AGRICULTOR" )+"\n"); } }catch(Exception ex){ System.out.println("Error"); } } public void txt_cod_actionPerformed(ActionEvent e) { } } class FConsulta01_txt_cod_actionAdapter implements ActionListener { private consulta1 adaptee;

FConsulta01_txt_cod_actionAdapter(consulta1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.txt_cod_actionPerformed(e); } } class FConsulta01_jButton1_actionAdapter implements ActionListener { private consulta1 adaptee; FConsulta01_jButton1_actionAdapter(consulta1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } } class FConsulta01_bot_mostrar_actionAdapter implements ActionListener { private consulta1 adaptee; FConsulta01_bot_mostrar_actionAdapter(consulta1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.bot_mostrar_actionPerformed(e); } }

You might also like