Aqui esta el codigo de todas las clases que tienes que crear.
Consejo: No lo digo yo, lo decimos todos los programadores. Cuando codificas un codigo, no siempre funciona a la primera, y no desesperes, eso forma parte de este mundo de la programacion. A veces, simplemente es una coma, un punto, una mayuscula donde tenia que ir una minuscula, un espacio, si si, un espacio, comillas dobles, comillas simples. Todo esto te puede llevar a errores en el codigo y a la desesperacion de no encontrar solucion.
Mi consejo, no te enfusques demasiado, levantate de la silla, vete a tomar unas cervezas y seguro que tomando las cervezas te viene la solucion a la cabeza, o inclusive, aunque parezca magia, pues si, has visto el error. Y si tomando las cervezas no encuentras el error, vete a dormir, que seguro que con la mente despejada, encontraras la solucion.
Ahora si, sin mas, te dejo con el proyecto.
Inicio
package formularios;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Inicio extends JFrame implements ActionListener{
JButton btnAgregar, btnModificar, btnListar, btnBuscar, btnEliminar;
public Inicio(){
this("Menu inicio",500,100,300,450);
}
public Inicio(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBounds(x, y, w, h);
this.getContentPane().setBackground(new Color(200,200,250));
this.setVisible(true);
btnAgregar = new JButton("Nuevo Producto");
btnAgregar.setBounds(50, 60, 180, 30);
btnAgregar.setIcon(new ImageIcon(Inicio.class.getResource("/img/add.png")));
this.getContentPane().add(btnAgregar);
btnModificar = new JButton("Modificar Producto");
btnModificar.setBounds(50, 110, 180, 30);
btnModificar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Modify.png")));
this.getContentPane().add(btnModificar);
btnListar = new JButton("Listar Producto");
btnListar.setBounds(50, 160, 180, 30);
btnListar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Line Chart.png")));
this.getContentPane().add(btnListar);
btnBuscar = new JButton("Buscar Producto");
btnBuscar.setBounds(50, 210, 180, 30);
btnBuscar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Search.png")));
this.getContentPane().add(btnBuscar);
btnEliminar = new JButton("Eliminar Producto");
btnEliminar.setBounds(50, 260, 180, 30);
btnEliminar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Delete.png")));
this.getContentPane().add(btnEliminar);
btnAgregar.addActionListener(this);
btnModificar.addActionListener(this);
btnListar.addActionListener(this);
btnBuscar.addActionListener(this);
btnEliminar.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnAgregar){
NuevoProducto np = new NuevoProducto();
}
if(event.getSource()==btnModificar){
ModificarProducto mp = new ModificarProducto();
}
if(event.getSource()==btnListar){
ListarProducto lp = new ListarProducto();
}
if(event.getSource()==btnBuscar){
BuscarProducto bp = new BuscarProducto();
}
if(event.getSource()==btnEliminar){
EliminarProducto ep = new EliminarProducto();
}
}
public static void main(String[]args){
Inicio inicio = new Inicio();
}
}
Conectar
package formularios;
import java.sql.*;
public class Conectar {
public Connection conexion = null;
public Statement st = null;
public ResultSet rs = null;
public void Conecta(){
try{
Class.forName("com.mysql.jdbc.Driver");
String ruta = "jdbc:mysql://localhost:3306/basepapeleria";
conexion = DriverManager.getConnection(ruta,"root","root");
st = conexion.createStatement();
}catch(Exception ex){
System.out.println("Error de conexion | "+ex.getMessage());
}
}
}
NuevoProducto
package formularios;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class NuevoProducto extends JFrame implements ActionListener{
JTextField cmpCod, cmpProducto, cmpFamilia, cmpPrecio;
JButton btnAgregar, btnReset;
JLabel lblAviso;
public NuevoProducto(){
this("Nuevo producto",450,100,400,300);
}
public NuevoProducto(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(x, y, w, h);
this.setVisible(true);
JLabel lblCod = new JLabel("Codigo:");
lblCod.setBounds(30, 20, 100, 20);
this.getContentPane().add(lblCod);
cmpCod = new JTextField();
cmpCod.setBounds(110, 20, 80, 20);
this.getContentPane().add(cmpCod);
JLabel lblProducto = new JLabel("Producto:");
lblProducto.setBounds(30, 50, 100, 20);
this.getContentPane().add(lblProducto);
cmpProducto = new JTextField();
cmpProducto.setBounds(110, 50, 200, 20);
this.getContentPane().add(cmpProducto);
JLabel lblFamilia = new JLabel("Familia:");
lblFamilia.setBounds(30, 80, 100, 20);
this.getContentPane().add(lblFamilia);
cmpFamilia = new JTextField();
cmpFamilia.setBounds(110, 80, 150, 20);
this.getContentPane().add(cmpFamilia);
JLabel lblPrecio = new JLabel("Precio:");
lblPrecio.setBounds(30, 110, 100, 20);
this.getContentPane().add(lblPrecio);
cmpPrecio = new JTextField();
cmpPrecio.setBounds(110, 110, 80, 20);
this.getContentPane().add(cmpPrecio);
lblAviso = new JLabel("Rellena todos los campos y pulsa 'Guardar'");
lblAviso.setBounds(30, 140, 300, 20);
lblAviso.setHorizontalAlignment(SwingConstants.CENTER);
lblAviso.setForeground(Color.lightGray);
this.getContentPane().add(lblAviso);
btnAgregar = new JButton("Nuevo Producto");
btnAgregar.setBounds(20, 200, 180, 30);
btnAgregar.setIcon(new ImageIcon(Inicio.class.getResource("/img/add.png")));
this.getContentPane().add(btnAgregar);
btnReset = new JButton("Reset");
btnReset.setBounds(280, 200, 80, 30);
this.getContentPane().add(btnReset);
btnAgregar.addActionListener(this);
btnReset.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnAgregar){
String codigo = cmpCod.getText();
String producto = cmpProducto.getText();
String familia = cmpFamilia.getText();
double precio = Double.parseDouble(cmpPrecio.getText());
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "INSERT INTO tablapapeleria (codigo, producto, familia, precio)";
sql += " VALUES ('"+codigo+"','"+producto+"','"+familia+"',"+precio+")";
con.st.execute(sql);
lblAviso.setText("Producto ingresado satisfactoriamente!!");
}catch(Exception ex){
System.out.println("Error en el ingreso de datos!"+ex.getMessage());
lblAviso.setText("Error en el ingreso de datos!!");
}finally{
try{
con.conexion.close();
con.st.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnReset){
cmpCod.setText("");
cmpProducto.setText("");
cmpFamilia.setText("");
cmpPrecio.setText("");
lblAviso.setText("Rellena todos los campos y pulsa 'Guardar'");
}
}
}
ModificarProducto
package formularios;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ModificarProducto extends JFrame implements ActionListener{
JTextField cmpCod, cmpProducto, cmpFamilia, cmpPrecio;
JButton btnBuscar, btnModificar, btnReset;
JLabel lblAviso;
private ResultSet rs = null;
public ModificarProducto(){
this("Modificar producto",450,100,400,300);
}
public ModificarProducto(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(x, y, w, h);
this.setVisible(true);
JLabel lblCod = new JLabel("Codigo:");
lblCod.setBounds(30, 20, 100, 20);
this.getContentPane().add(lblCod);
cmpCod = new JTextField();
cmpCod.setBounds(110, 20, 80, 20);
this.getContentPane().add(cmpCod);
JLabel lblProducto = new JLabel("Producto:");
lblProducto.setBounds(30, 50, 100, 20);
this.getContentPane().add(lblProducto);
cmpProducto = new JTextField();
cmpProducto.setBounds(110, 50, 200, 20);
this.getContentPane().add(cmpProducto);
JLabel lblFamilia = new JLabel("Familia:");
lblFamilia.setBounds(30, 80, 100, 20);
this.getContentPane().add(lblFamilia);
cmpFamilia = new JTextField();
cmpFamilia.setBounds(110, 80, 150, 20);
this.getContentPane().add(cmpFamilia);
JLabel lblPrecio = new JLabel("Precio:");
lblPrecio.setBounds(30, 110, 100, 20);
this.getContentPane().add(lblPrecio);
cmpPrecio = new JTextField();
cmpPrecio.setBounds(110, 110, 80, 20);
this.getContentPane().add(cmpPrecio);
lblAviso = new JLabel("Ingresa el codigo de producto a modificar");
lblAviso.setBounds(30, 140, 300, 20);
lblAviso.setHorizontalAlignment(SwingConstants.CENTER);
lblAviso.setForeground(Color.lightGray);
this.getContentPane().add(lblAviso);
btnBuscar = new JButton("Buscar");
btnBuscar.setBounds(250, 20, 120, 20);
btnBuscar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Search.png")));
this.getContentPane().add(btnBuscar);
btnModificar = new JButton("Modifica Producto");
btnModificar.setBounds(20, 200, 180, 30);
btnModificar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Modify.png")));
this.getContentPane().add(btnModificar);
btnReset = new JButton("Reset");
btnReset.setBounds(280, 200, 80, 30);
this.getContentPane().add(btnReset);
cmpProducto.setEditable(false);
cmpFamilia.setEditable(false);
cmpPrecio.setEditable(false);
btnBuscar.addActionListener(this);
btnModificar.addActionListener(this);
btnReset.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnBuscar){
cmpProducto.setEditable(true);
cmpFamilia.setEditable(true);
cmpPrecio.setEditable(true);
String codigo = cmpCod.getText();
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "SELECT * FROM tablapapeleria WHERE codigo ='"+codigo+"'";
rs = con.st.executeQuery(sql);
while(rs.next()){
cmpProducto.setText(rs.getString("producto"));
cmpFamilia.setText(rs.getString("familia"));
cmpPrecio.setText(rs.getString("precio"));
}
}catch(Exception ex){
}finally{
try{
con.conexion.close();
con.st.close();
rs.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnModificar){
String codigo = cmpCod.getText();
String producto = cmpProducto.getText();
String familia = cmpFamilia.getText();
double precio = Double.parseDouble(cmpPrecio.getText());
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "UPDATE tablapapeleria SET codigo='"+codigo+"',";
sql += " producto='"+producto+"', familia='"+familia+"', precio='"+precio+"'";
sql += " WHERE codigo='"+codigo+"'";
con.st.execute(sql);
lblAviso.setText("Producto modificado satisfactoriamente!!");
}catch(Exception ex){
System.out.println("Error en el modificado de datos!"+ex.getMessage());
lblAviso.setText("Error en el modificado de datos!!");
}finally{
try{
con.conexion.close();
con.st.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnReset){
cmpCod.setText("");
cmpProducto.setText("");
cmpFamilia.setText("");
cmpPrecio.setText("");
cmpProducto.setEditable(false);
cmpFamilia.setEditable(false);
cmpPrecio.setEditable(false);
lblAviso.setText("Ingresa el codigo de producto a modificar");
}
}
EliminarProducto
package formularios;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class EliminarProducto extends JFrame implements ActionListener{
JTextField cmpCod, cmpProducto, cmpFamilia, cmpPrecio;
JButton btnBuscar, btnModificar, btnReset;
JLabel lblAviso;
private ResultSet rs = null;
public EliminarProducto(){
this("Eliminar producto",450,100,400,300);
}
public EliminarProducto(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(x, y, w, h);
this.setVisible(true);
JLabel lblCod = new JLabel("Codigo:");
lblCod.setBounds(30, 20, 100, 20);
this.getContentPane().add(lblCod);
cmpCod = new JTextField();
cmpCod.setBounds(110, 20, 80, 20);
this.getContentPane().add(cmpCod);
JLabel lblProducto = new JLabel("Producto:");
lblProducto.setBounds(30, 50, 100, 20);
this.getContentPane().add(lblProducto);
cmpProducto = new JTextField();
cmpProducto.setBounds(110, 50, 200, 20);
this.getContentPane().add(cmpProducto);
JLabel lblFamilia = new JLabel("Familia:");
lblFamilia.setBounds(30, 80, 100, 20);
this.getContentPane().add(lblFamilia);
cmpFamilia = new JTextField();
cmpFamilia.setBounds(110, 80, 150, 20);
this.getContentPane().add(cmpFamilia);
JLabel lblPrecio = new JLabel("Precio:");
lblPrecio.setBounds(30, 110, 100, 20);
this.getContentPane().add(lblPrecio);
cmpPrecio = new JTextField();
cmpPrecio.setBounds(110, 110, 80, 20);
this.getContentPane().add(cmpPrecio);
lblAviso = new JLabel("Ingresa el codigo de producto a eliminar");
lblAviso.setBounds(30, 140, 300, 20);
lblAviso.setHorizontalAlignment(SwingConstants.CENTER);
lblAviso.setForeground(Color.lightGray);
this.getContentPane().add(lblAviso);
btnBuscar = new JButton("Buscar");
btnBuscar.setBounds(250, 20, 120, 20);
btnBuscar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Search.png")));
this.getContentPane().add(btnBuscar);
btnModificar = new JButton("Elimina Producto");
btnModificar.setBounds(20, 200, 180, 30);
btnModificar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Delete.png")));
this.getContentPane().add(btnModificar);
btnReset = new JButton("Reset");
btnReset.setBounds(280, 200, 80, 30);
this.getContentPane().add(btnReset);
cmpProducto.setEditable(false);
cmpFamilia.setEditable(false);
cmpPrecio.setEditable(false);
btnBuscar.addActionListener(this);
btnModificar.addActionListener(this);
btnReset.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnBuscar){
cmpCod.setEditable(false);
String codigo = cmpCod.getText();
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "SELECT * FROM tablapapeleria WHERE codigo ='"+codigo+"'";
rs = con.st.executeQuery(sql);
while(rs.next()){
cmpProducto.setText(rs.getString("producto"));
cmpFamilia.setText(rs.getString("familia"));
cmpPrecio.setText(rs.getString("precio"));
}
lblAviso.setText("Producto a eliminar.¿Esta seguro?");
}catch(Exception ex){
}finally{
try{
con.conexion.close();
con.st.close();
rs.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnModificar){
String codigo = cmpCod.getText();
String producto = cmpProducto.getText();
String familia = cmpFamilia.getText();
double precio = Double.parseDouble(cmpPrecio.getText());
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "DELETE FROM tablapapeleria WHERE codigo='"+codigo+"'";
con.st.execute(sql);
lblAviso.setText("Producto eliminado satisfactoriamente!!");
}catch(Exception ex){
System.out.println("Error en el borrado de datos!"+ex.getMessage());
lblAviso.setText("Error en el borrado de datos!!");
}finally{
try{
con.conexion.close();
con.st.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnReset){
cmpCod.setText("");
cmpProducto.setText("");
cmpFamilia.setText("");
cmpPrecio.setText("");
cmpCod.setEditable(true);
cmpProducto.setEditable(false);
cmpFamilia.setEditable(false);
cmpPrecio.setEditable(false);
lblAviso.setText("Ingresa el codigo de producto a eliminar");
}
}
}
BuscarProducto
package formularios;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BuscarProducto extends JFrame implements ActionListener{
JTextField cmpCod, cmpProducto, cmpFamilia, cmpPrecio;
JButton btnBuscar;
JTextArea cmpArea;
private ResultSet rs = null;
public BuscarProducto(){
this("Buscar producto",450,100,400,300);
}
public BuscarProducto(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(x, y, w, h);
this.setVisible(true);
JLabel lblCod = new JLabel("Codigo:");
lblCod.setBounds(30, 20, 100, 20);
this.getContentPane().add(lblCod);
cmpCod = new JTextField();
cmpCod.setBounds(110, 20, 80, 20);
this.getContentPane().add(cmpCod);
JScrollPane panel = new JScrollPane();
panel.setBounds(10, 50, 360, 200);
this.getContentPane().add(panel);
cmpArea = new JTextArea();
cmpArea.setLineWrap(true);
panel.setViewportView(cmpArea);
btnBuscar = new JButton("Buscar");
btnBuscar.setBounds(250, 20, 120, 20);
btnBuscar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Search.png")));
this.getContentPane().add(btnBuscar);
btnBuscar.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnBuscar){
String codigo = cmpCod.getText();
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "SELECT * FROM tablapapeleria WHERE codigo ='"+codigo+"'";
rs = con.st.executeQuery(sql);
while(rs.next()){
cmpArea.append("\nCodigo: "+rs.getString("codigo"));
cmpArea.append("\nProducto: "+rs.getString("producto"));
cmpArea.append("\nFamilia: "+rs.getString("familia"));
cmpArea.append("\nPrecio: "+rs.getString("precio"));
cmpArea.append("\n_____________________________\n");
}
}catch(Exception ex){
}finally{
try{
con.conexion.close();
con.st.close();
rs.close();
}catch(Exception ex){}
}
}
}
}
ListarProducto
package formularios;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ListarProducto extends JFrame implements ActionListener{
JTextField cmpCod, cmpProducto, cmpFamilia, cmpPrecio;
JButton btnListar, btnReset;
JTextArea cmpArea;
private ResultSet rs = null;
public ListarProducto(){
this("Listar productos",450,100,400,300);
}
public ListarProducto(String titulo, int x, int y, int w, int h){
super(titulo);
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(x, y, w, h);
this.setVisible(true);
JScrollPane panel = new JScrollPane();
panel.setBounds(10, 50, 360, 200);
this.getContentPane().add(panel);
cmpArea = new JTextArea();
cmpArea.setLineWrap(true);
panel.setViewportView(cmpArea);
btnListar = new JButton("Listar");
btnListar.setBounds(250, 20, 120, 20);
btnListar.setIcon(new ImageIcon(Inicio.class.getResource("/img/Line Chart.png")));
this.getContentPane().add(btnListar);
btnReset = new JButton("Reset");
btnReset.setBounds(280, 200, 80, 30);
this.getContentPane().add(btnReset);
btnListar.addActionListener(this);
btnReset.addActionListener(this);
this.paintComponents(getGraphics());
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==btnListar){
Conectar con = new Conectar();
try{
con.Conecta();
String sql = "SELECT * FROM tablapapeleria";
rs = con.st.executeQuery(sql);
while(rs.next()){
cmpArea.append("\nCodigo: "+rs.getString("codigo"));
cmpArea.append("\nProducto: "+rs.getString("producto"));
cmpArea.append("\nFamilia: "+rs.getString("familia"));
cmpArea.append("\nPrecio: "+rs.getString("precio"));
cmpArea.append("\n_____________________________\n");
}
}catch(Exception ex){
}finally{
try{
con.conexion.close();
con.st.close();
rs.close();
}catch(Exception ex){}
}
}
if(event.getSource()==btnReset){
cmpArea.setText(null);
}
}
}
Cuando tengas todas las clases codificadas, ejecuta el proyecto desde la clase "Inicio". Ya tienes tu aplicacion Swing vinculada a una base de datos.