Mantenedor
Enviado por Programa Chuletas y clasificado en Informática y Telecomunicaciones
Escrito el en español con un tamaño de 2,68 KB
package multiplespantallas;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class insertar extends JFrame
{
private JLabel lbl1,lbl2,lbl3;
private JTextField txt1,txt2,txt3;
private JPanel pnl1,pnl2;
private JButton btninsertar,btncancelar;
insertar()
{
lbl1=new JLabel("Codigo");
lbl2=new JLabel("Nombre");
lbl3=new JLabel("N°horas");
txt1=new JTextField(10);
txt2=new JTextField(10);
txt3=new JTextField(10);
btninsertar=new JButton("Insertar");
btncancelar=new JButton("cancelar");
pnl1=new JPanel();
pnl2=new JPanel();
pnl1.setLayout(new GridLayout(3,2));
pnl1.add(lbl1);
pnl1.add(txt1);
pnl1.add(lbl2);
pnl1.add(txt2);
pnl1.add(lbl3);
pnl1.add(txt3);
pnl2. setLayout(new FlowLayout());
pnl2.add(btninsertar);
pnl2.add(btncancelar);
setLayout(new FlowLayout());
add(pnl1);
add(pnl2);
setSize(300,300);
setVisible(true);
btninsertar.addActionListener(new manejador());
btncancelar.addActionListener(new manejador());
}
public static void main(String[] args)
{
insertar a=new insertar();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class manejador implements ActionListener
{
public void actionPerformed(ActionEvent z)
{
if(z.getSource()==btninsertar)
{
try
{
String sql="insert into asignatura values (" +txt1.getText()+",'"+ txt2.getText()+"',"+txt3.getText()+")";
Connection cnn=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn=DriverManager.getConnection("jdbc:odbc:Multipantallas","admin","aqi1975");
JOptionPane.showMessageDialog(null,"conexion creada");
Statement str=cnn.createStatement();
int i = str.executeUpdate(sql);
if(i>0)
JOptionPane.showMessageDialog(null,"se inserto un elemento");
else
JOptionPane.showMessageDialog(null,"no se inserto el elemento");
cnn.close();
}
catch(ClassNotFoundException ex)
{
JOptionPane.showMessageDialog(null,"exception"+ ex.getMessage());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"exception"+ e.getMessage());
}
if(z.getSource()==btncancelar)
{
txt1.setText("");
txt2.setText("");
txt3.setText("");
}