Crud Java
Crud Java
Crud Java
java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package netosolis.com;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;
import javax.swing.JOptionPane;
/**
*
* @author netosolis.com
*/
public class CRUD {
private Conexion conexion = new Conexion();
private Connection con;
private Statement st;
private ResultSet rs;
public CRUD() {
try{
if((con = conexion.getConexionMYSQL())==null){
JOptionPane.showMessageDialog(null,"Error con la conexion a la BD");
return;
}
st = con.createStatement();
}catch(Exception e){
e.printStackTrace();
}
}
//Devuelve el resultset con los datos de peliculas con select por genero
public ResultSet selectXgenero(String genero){
try {
String query = "SELECT * FROM peliculas WHERE genero = '"+genero+"'";
rs = st.executeQuery(query);
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//Devuelve el resultset con los datos de peliculas con select por pais
public ResultSet selectXpais(String pais){
try {
String query = "SELECT * FROM peliculas WHERE pais = '"+pais+"'";
rs = st.executeQuery(query);
return rs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//Elimina la pelicula
public boolean delete(int id){
try {
String query = "DELETE FROM peliculas WHERE id = '"+id+"'";
st.executeUpdate(query);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
conexión.java
package netosolis.com;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author netosolis.com
*/
public class Conexion {
//Configuracion de los datos de la BD
private String usuario = "root";
private String pass = "";
private String host = "localhost";
private String nombre_BD = "crudjava";
private Connection con = null;
public Conexion() {
}
//Metodo que se devuelve la conexion o null si hubo un error
public Connection getConexionMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance( );
String servidor = "jdbc:mysql://"+host+"/"+nombre_BD;
con = DriverManager.getConnection(servidor,usuario,pass);
return con;
}catch(Exception e){
e.printStackTrace();
return con;
}
}
}
form.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package netosolis.com;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
*
@author netosolis.com
*/
public class Main extends javax.swing.JFrame {
private UIManager.LookAndFeelInfo apariencias[];
private CRUD crud;
private DefaultTableModel modelo = new DefaultTableModel();
/**
* Creates new form Main
*/
public Main() {
initComponents();
apariencias=UIManager.getInstalledLookAndFeels();
try {
UIManager.setLookAndFeel(apariencias[1].getClassName());
SwingUtilities.updateComponentTreeUI(this);
crud = new CRUD();
tabla.setModel(modelo);
modelo.addColumn("Id");
modelo.addColumn("Nombre");
modelo.addColumn("Genero");
modelo.addColumn("Año");
modelo.addColumn("Actor");
modelo.addColumn("Pais");
this.cargarPeliculas();
} catch (Exception e) {
e.printStackTrace();
}
}
//Convierte a mayusculas el contenido de los JTEXTFIELD
private void toUpper(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_toUpper
JTextField ob =(JTextField) evt.getSource();
ob.setText(ob.getText().toUpperCase());
}//GEN-LAST:event_toUpper
}//GEN-LAST:event_EliminarPelicula
}//GEN-LAST:event_CargarDatosPelicula
public Pelicula(int id, String nombre,String genero,String actor, String pais,int anio)
{
this.id = id;
this.nombre = nombre;
this.anio = anio;
this.actor = actor;
this.pais = pais;
this.genero = genero;
}
@Override
public String toString()
{
return nombre;
}
}
/**
* @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
}
//</editor-fold>
TABLA_BASE_DE_DATOS.sql