Está en la página 1de 8

Nombre: Emil Aníbal Reyes Rodríguez

Matricula: 2021-1173

package Base;

import java.awt.EventQueue;

import java.sql.DriverManager;

import java.sql.Connection;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Connection;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTabbedPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

public class Tarea {

public class clase {


Connection con;

public Connection conectar() {

try {

Class.forName("com.mysql.jdbc.Driver");

con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/prueba","root","");

}catch(Exception e2) {

e2.printStackTrace();

return con;

private JFrame frame;

private JTextField textoid,textn;

private JTable table1;

private JTabbedPane tabbed;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {


try {

Tarea window = new Tarea();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

});

/**

* Create the application.

*/

public Tarea() {

initialize();

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(tabbed);

tabbed = new JTabbedPane(JTabbedPane.TOP);

tabbed.setBounds(0, 0, 570, 336);

frame.getContentPane().add(tabbed);

JPanel panel = new JPanel();


tabbed.addTab("registro", null, panel, null);

JPanel panel1 = new JPanel();

panel1.setBounds(0, 0, 575, 319);

panel.add(panel1);

panel1.setLayout(null);

JLabel label = new JLabel("matricula");

label.setBounds(24, 75, 45, 13);

panel1.add(label);

JLabel label1 = new JLabel("matricula");

label1.setBounds(24, 75, 45, 13);

panel1.add(label1);

textoid = new JTextField();

textoid.setBounds(24, 75, 45, 13);

panel1.add(textoid);

textoid.setColumns(10);

textn = new JTextField();

textn.setBounds(24, 75, 45, 13);

panel1.add(textn);

textn.setColumns(10);

JButton boton = new JButton ("Guardar");

boton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub


try {

clase con = new clase();

Connection conexion = con.conectar();

Statement statement = conexion.createStatement();

String nombre = textn.getText();

int id = Integer.parseInt(textoid.getText());

String query = "INSERT INTO agenda(matricula,nombre)


values('"+id+"'.'"+nombre+"')";

statement.executeUpdate(query);

conexion.close();

}catch(Exception e1) {

e1.printStackTrace();

}}} );

boton.setBounds(36, 130, 85, 21);

panel1.add(boton);

JButton boton1 = new JButton ("buscar");

boton1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

try {

clase con = new clase();

Connection conexion = con.conectar();

Statement statement = conexion.createStatement();

int id = Integer.parseInt(textoid.getText());

ResultSet resultado = statement.executeQuery("select from agenda where


matricula="+id);
if(resultado.next() == true) {

DefaultTableModel model = new DefaultTableModel();

model.addColumn("nombre");

model.addColumn("matricula");

table1.setModel(model);

conexion.close();

}catch(SQLException a) {

a.printStackTrace();

});

boton1.setBounds(36, 130, 85, 21);

panel1.add(boton1);

JButton boton2 = new JButton ("borrar");

boton2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

try {

clase con = new clase();

Connection conexion = con.conectar();

Statement statement = conexion.createStatement();

int id = Integer.parseInt(textoid.getText());

String query ="Delete from agenda where matricula ="+id;

statement.executeUpdate(query);

conexion.close();
}catch(SQLException e4) {

e4.printStackTrace();

});

boton2.setBounds(36, 130, 85, 21);

panel1.add(boton2);

JButton boton3 = new JButton ("actualizar");

boton3.setBounds(36, 130, 85, 21);

panel1.add(boton3);

table1 = new JTable();

table1.setBounds(36, 130, 85, 21);

panel1.add(table1);

clase con = new clase();

Connection conectar = con.conectar();

String sql = "SELECT* FROM agenda";

Statement st;

DefaultTableModel model = new DefaultTableModel();

model.addColumn("matricula");

model.addColumn("nombre");

table1.setModel(model);

String[] dato = new String[2];

try {

st = conectar.createStatement();
ResultSet resultado = st.executeQuery(sql);

while(resultado.next()) {

dato[0] = resultado.getString("matricula");

dato[1] = resultado.getString("nombre");

model.addRow(dato);

}catch(SQLException e3) {

e3.printStackTrace();

También podría gustarte