Anda di halaman 1dari 4

/* * To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates * and open the template in the editor. */ package manempsa; import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.Statement; javax.swing.JOptionPane;

/** * * @author sinf7 */ public class manempsa extends javax.swing.JFrame { Connection conexion; Statement sentencia; /** * Creates new form manempsa */ public manempsa() { initComponents(); PrepararBaseDatos(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { botonverdato = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); botonverdato.setText("Ver Datos Trabajador"); botonverdato.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { botonverdatoActionPerformed(evt); } }); jButton1.setText("Salir"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP

ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addGap(24, 24, 24) .addComponent(botonverdato) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createS equentialGroup() .addContainerGap(192, Short.MAX_VALUE) .addComponent(jButton1) .addGap(155, 155, 155)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(botonverdato) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED, 192, Short.MAX_VALUE) .addComponent(jButton1) .addGap(51, 51, 51)) ); pack(); }// </editor-fold> private void botonverdatoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String info=""; double totalsu=0; // total sueldo try { ResultSet r=sentencia.executeQuery("select * from trabajadores order by Nombres "); r.beforeFirst(); while (r.next()){ info=info+r.getString("nombres")+""+r.getString("apellidos")+" "+r.getSt ring("sueldo")+"\n"; totalsu=totalsu+r.getDouble("sueldo"); } JOptionPane.showMessageDialog(null,info); JOptionPane.showMessageDialog(null,"la suma de los sueldos es "+totalsu) ; } catch (Exception e){ JOptionPane.showMessageDialog(null,info); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

this.dispose();// TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting cod e (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the d efault look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lo okandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana ger.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(manempsa.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(manempsa.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(manempsa.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(manempsa.class.getName()).log(jav a.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new manempsa().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton botonverdato; private javax.swing.JButton jButton1; // End of variables declaration private void PrepararBaseDatos() { try { String controlador="oracle.jdbc.driver.OracleDriver"; Class.forName(controlador).newInstance(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"error al cargar el controlador. " +e); }

try { String DSN="jdbc:oracle:thin:@localhost:1521:XE"; String user="manempsa"; String password="manempsa"; conexion=DriverManager.getConnection(DSN,user,password); } catch (Exception e) { JOptionPane.showMessageDialog(null,"error al realizar la conexion"+e ); } try { sentencia=conexion.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } catch (Exception e) { JOptionPane.showMessageDialog(null,"error alcrear objeto de sentenci a"); } } }

Anda mungkin juga menyukai