Você está na página 1de 16

CODIGO DE LA BASE DE DATOS ALMACEN

use almacen;
create table Articulo(
id_articulo numeric(3) not null,
nombre nvarchar(50) not null,
fecha_llegada date,
constraint articulo_pk primary key (id_articulo)
);
create table Categoria(
id_categoria numeric(3)not null,
nombre_categoria varchar(50),
id_articulo numeric(3) not null,
constraint Categoria_pk primary key (id_categoria),
constraint art_FK foreign key (id_articulo) references Articulo(id_articulo)
);

create table Localizacion(
id_localizacion numeric(3) not null,
nombre_localizacion varchar(50),
id_categoria numeric(3) not null,
constraint Localizacion_pk primary key (id_localizacion),
constraint loca_FK foreign key (id_categoria) references Categoria(id_categoria)
);






CODIGO DEL PROGRAMA EN JAVA
CLASE CONEXIN (conexin entre la base de datos y java)
import java.sql.*;
import javax.swing.JOptionPane;
public class Conexion {

public String usuario = "sa";
public String password = "12345";
public String url = "jdbc:sqlserver://localhost:1433;databaseName=almacen;";

public Connection cn = null;
public Statement st = null;

public Statement Conectar()
{

try
{

Connection cn = DriverManager.getConnection(url,usuario,password);

st=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

} catch (SQLException i)
{
JOptionPane.showMessageDialog(null, i);
}
return st;

}
}



CLASE INICIO DE SESIN

import java.sql.*;
import javax.swing.JOptionPane;
public class Inicio_Sesion extends javax.swing.JFrame {

public Inicio_Sesion() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
TXT1 = new javax.swing.JTextPane();
TXT2 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("USUARIO:");

jLabel2.setText("CONTRASEA:");

jScrollPane1.setViewportView(TXT1);

jButton1.setText("Aceptar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Cancelar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String usuario = TXT1.getText();
String password=TXT2.getText();

if((password.isEmpty() || usuario.isEmpty()))
{
JOptionPane.showMessageDialog(null, "No deben de haber campos vacios","Advertencia",JOptionPane.INFORMATION_MESSAGE);
}
else
{
Conexion conectar = new Conexion();
Statement st = conectar.Conectar();
try

{

ResultSet rs = st.executeQuery("SELECT * FROM Login Where usuario='"+usuario+"'and password='"+password+"'");
rs.last();
int encontrado = rs.getRow();

if (encontrado == 1)
{

JOptionPane.showMessageDialog(null, "Se ha iniciado sesin","Inicio de sesin",JOptionPane.INFORMATION_MESSAGE);

Ventana_Principal pl = new Ventana_Principal();
pl.setVisible(true);
hide();

}
else{
TXT1.setText("");
TXT2.setText("");
JOptionPane.showMessageDialog(null, "Error en Usuario o Contrasea","Advertencia",JOptionPane.INFORMATION_MESSAGE);

}


}
catch (SQLException ex)

{
JOptionPane.showMessageDialog(null, ex);

}
}


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

/**
* @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 code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Inicio_Sesion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Inicio_Sesion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Inicio_Sesion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Inicio_Sesion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Inicio_Sesion().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextPane TXT1;
private javax.swing.JPasswordField TXT2;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}


























CLASE DE VENTANA PRINCIPAL
public class Ventana_Principal extends javax.swing.JFrame {

/**
* Creates new form Ventana_Principal
*/
public Ventana_Principal() {
initComponents();
}

/**
* 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() {

jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(204, 0, 0));
jLabel1.setText("Bienvenido");

jButton1.setText("Editar Informacion");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Salir");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Editar_informacion editar=new Editar_informacion();
editar.setVisible(true);
hide();
}

/**
* @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 code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Ventana_Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Ventana_Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Ventana_Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Ventana_Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Ventana_Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}






























CLASE AGREGAR, EDITAR
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author varptini
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class Agregar_Articulo extends javax.swing.JInternalFrame {

/**
* Creates new form Agregar_Articulo
*/
Articulo articulo = new Articulo();
Registrar_Articulo nuevo = new Registrar_Articulo();
Verificar_Articulo checar = new Verificar_Articulo();
public Agregar_Articulo() {
initComponents();
//this.toFront();
}

/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {

jLabel5 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
Tipo = new javax.swing.JComboBox();
Fabricante = new javax.swing.JTextField();
Articulo = new javax.swing.JTextField();
Estante = new javax.swing.JComboBox();
Seccion = new javax.swing.JComboBox();
jLabel4 = new javax.swing.JLabel();
Descripcion = new javax.swing.JTextField();
Codigo = new javax.swing.JTextField();
Agregar = new javax.swing.JButton();
Cancelar = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
Cantidad = new javax.swing.JFormattedTextField();
Precio = new javax.swing.JTextField();

setBackground(new java.awt.Color(204, 204, 204));
setTitle("Agregar Articulo");
try {
setSelected(true);
} catch (java.beans.PropertyVetoException e1) {
e1.printStackTrace();
}
setVisible(false);

jLabel5.setText("Estante");

jLabel1.setText("Codigo");

jLabel2.setText("Articulo");

jLabel3.setText("Fabricante");

jLabel7.setText("Precio");

jLabel10.setText("Descripcion");

jLabel8.setText("Cantidad");

jLabel9.setText("Categoria");

Tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Domestico", "Industrial" }));

Estante.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7" }));
Estante.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EstanteActionPerformed(evt);
}
});

Seccion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));
Seccion.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SeccionActionPerformed(evt);
}
});

jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/logo almacen.jpg"))); // NOI18N

Descripcion.setColumns(1);
Descripcion.setHorizontalAlignment(javax.swing.JTextField.LEFT);

Codigo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CodigoActionPerformed(evt);
}
});

Agregar.setText("Guardar");
Agregar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AgregarActionPerformed(evt);
}
});

Cancelar.setText("Cancelar");
Cancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CancelarActionPerformed(evt);
}
});

jLabel6.setText("Seccion");

Cantidad.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(Agregar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Cancelar)
.addGap(75, 75, 75))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel10)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Precio, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9))))
.addGap(0, 164, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(30, 30, 30))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Estante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Seccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(Fabricante, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(Articulo, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(229, Short.MAX_VALUE)))
);

private void AgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AgregarActionPerformed
// TODO add your handling code here:

if(Codigo.getText().equals("")||Articulo.getText().equals("")||Descripcion.getText().equals("")||Fabricante.getText().equals("")||Precio.getText().equals("")||Cantidad.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Los campos deben estar rellenados completamente");
}
else
{
if(esDecimal(String.valueOf(Precio.getText()))==true)
{
if(checar.Comparar(String.valueOf(Codigo.getText()))==false)
{
JOptionPane.showMessageDialog(null,"El Codigo del Articulo ya esta registrado");

}
else
{
articulo.setCodigo(String.valueOf(Codigo.getText()));
articulo.setArticulo(String.valueOf(Articulo.getText()));
articulo.setDescripcion(String.valueOf(Descripcion.getText()));
articulo.setTipo(Tipo.getSelectedItem().toString());
articulo.setFabricante(String.valueOf(Fabricante.getText()));
articulo.setPrecio(Double.parseDouble(String.valueOf(Precio.getText())));
articulo.setEstante(Integer.parseInt(Estante.getSelectedItem().toString()));
articulo.setSeccion(Integer.parseInt(Seccion.getSelectedItem().toString()));
articulo.setCantidad(Integer.parseInt(String.valueOf(Cantidad.getText())));

nuevo.NuevoArticulo(articulo.getObtenerCodigo(),articulo.getObtenerArticulo(),articulo.getObtenerDescripcion(),articulo.getObetenrTipo(),articulo.getObtenerFabricante(),
articulo.getObtenerPrecio(),articulo.getObtenerEstante(),articulo.getObtenerSeccion(),articulo.getObtenerCantidad());
JOptionPane.showMessageDialog(null,"El Articulo se ha registrado correctamente!!!");
limpiarFormulario();
}
}
else
JOptionPane.showMessageDialog(null,"Tiene un error de coincidencia de tipo de datos solicitados");
}

}//GEN-LAST:event_AgregarActionPerformed
private void limpiarFormulario()
{
Codigo.setText("");
Articulo.setText("");
Descripcion.setText("");
Fabricante.setText("");
Precio.setText("");
Cantidad.setText("");
}
//Devuelve true si la cadena que llega es un numero decimal, false en caso contrario
public boolean esDecimal(String cad)
{
try
{
Double.parseDouble(cad);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}
private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
// TODO add your handling code here:
this.dispose();
}//GEN-LAST:event_CancelarActionPerformed

private void EstanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EstanteActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_EstanteActionPerformed

private void SeccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SeccionActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_SeccionActionPerformed

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton Agregar;
private javax.swing.JTextField Articulo;
private javax.swing.JButton Cancelar;
private javax.swing.JFormattedTextField Cantidad;
private javax.swing.JTextField Codigo;
private javax.swing.JTextField Descripcion;
private javax.swing.JComboBox Estante;
private javax.swing.JTextField Fabricante;
private javax.swing.JTextField Precio;
private javax.swing.JComboBox Seccion;
private javax.swing.JComboBox Tipo;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
// End of variables declaration//GEN-END:variables
}

Você também pode gostar