Avance Computacion
Avance Computacion
JscrollPane
cargamos el
espacio
cn=DriverManager.getConnection(url);
ps=cn.prepareStatement("select * from
tblEspecialidad");
rs=ps.executeQuery();
while (rs.next()) {
model.addRow(new Object [] {
rs.getInt(1),
rs.getString(2),
rs.getString(3)
});
}
grilla.setModel(model);
cn.close();
en el foquito y luego
1) conectarse a la DB
2) preparar la sentencia
3) configurar los parametros de la sentencia (Hacer el
binding)
4) ejecutar la sentencia
5) desconectar de la DB
ps=cn.prepareStatement("INSERT INTO
tblEspecialidad(Nombre,Descripcion) VALUES (?,?);");
ahora
dentro del listener escribiremos este codigo
txtIDespecialidad.setText(String.valueOf(grilla.getValueAt(gri
lla.getSelectedRow(), 0)));
txtNombre.setText(String.valueOf(grilla.getValueAt(grilla.getS
electedRow(), 1)));
txtDescripcion.setText(String.valueOf(grilla.getValueAt(grilla
.getSelectedRow(), 2)));
}
Para
borrar el
codigo
que
pondremos
sera el
siguiente
JButton btnBorrar = new JButton("BORRAR");
btnBorrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Codigo para borrar
try {
borrarEspe();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
escribimos
ps=cn.prepareStatement(url);
ps.setInt(1,Integer.parseInt(txtIDespecialidad.getText()));
//c
ps.execute(); //ejecutar
cn.close(); //Cerrar
JOptionPane.showMessageDialog(null,
"DatosBorrados");
cargarEspe(); //Cargar BD
}
==============================================================
}
//Siempre debajo del ultimo boton
//Creamos 3 metodos
AHORA METODO 3
//metodo 2
private void cargarEspe() throws SQLException {
@Override
public String toString() { //metodo para obtener solo el texto
(implicito)
// TODO Auto-generated method stub
return this.texto;
}
//metodo 2
private void cargarEspe() throws SQLException {
//model.addElement(rs.getString(2));
// instancia texto(nombre)
valor(idCurso)
model.addElement(new ObjetoCombo(rs.getString(2),
rs.getInt(1)));
}
cbxidEspecialidad.setModel(model);
cbxidEspecialidad.setSelectedIndex(0);
cn.close();
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Especialidad frame = new Especialidad();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Especialidad() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 666, 439);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
/*System.out.println(grilla.getValueAt(grilla.getSelectedRow(),
0));
System.out.println(grilla.getValueAt(grilla.getSelectedRow(), 1));
System.out.println(grilla.getValueAt(grilla.getSelectedRow(),
2));*/
txtIDespecialidad.setText(String.valueOf(grilla.getValueAt(grilla.getSele
ctedRow(), 0)));
txtNombre.setText(String.valueOf(grilla.getValueAt(grilla.getSelectedRow(
), 1)));
txtDescripcion.setText(String.valueOf(grilla.getValueAt(grilla.getSelecte
dRow(), 2)));
}
});
scrollPane.setViewportView(grilla);
}
});
btnGuardar.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnGuardar.setBounds(32, 198, 136, 34);
contentPane.add(btnGuardar);
ps.setInt(1,Integer.parseInt(txtIDespecialidad.getText()));
ps.execute();
cn.close();
JOptionPane.showMessageDialog(null,
"DatosBorrados");
cargarEspe();
}
});
btnBorrar.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnBorrar.setBounds(405, 198, 136, 30);
contentPane.add(btnBorrar);
//Llamada al método de carga
try {
cargarEspe();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=DriverManager.getConnection(url);
//ps=cn.prepareStatement("select * from tblEspecialidad");
cs = cn.prepareCall("{call CargarEspecialidades}");
//rs=ps.executeQuery();
rs=cs.executeQuery();
while (rs.next()) {
model.addRow(new Object [] {
rs.getInt(1),
rs.getString(2),
rs.getString(3)
});
}
grilla.setModel(model);
cn.close();
}
}