Advertisement
Guest User

Mysql

a guest
Sep 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package tallereslaborales;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.swing.JOptionPane;
  12.  
  13.  
  14. public class Mysql {
  15.     public String nombre,taller,sector,rut,telefono;
  16.     Connection conexion = null;
  17.     Statement comando = null;
  18.     ResultSet registro;
  19.  
  20.     public void ConectarBD() throws SQLException{
  21.             //CARGAR DRIVER JDBC
  22.             try{
  23.                 Class.forName("com.mysql.jdbc.Driver");
  24.             }
  25.             catch (ClassNotFoundException error){
  26.                 JOptionPane.showMessageDialog(null, "ERROR: " + error);
  27.             }
  28.  
  29.         //CONECTAR MYSQL
  30.         String servidor = "jdbc:mysql://localhost:3306/vilcun";
  31.         String usuario="root";
  32.         String password="";
  33.  
  34.         try{
  35.             conexion = DriverManager.getConnection(servidor,usuario,password);
  36.         }
  37.         catch(SQLException ex){
  38.             JOptionPane.showMessageDialog(null, "ERROR: " + ex);
  39.         }
  40.         finally{
  41.             JOptionPane.showMessageDialog(null, "¡CONECTADO A LA BASE DE DATOS!");
  42.  
  43.         }
  44.     }
  45.  
  46.  
  47.  
  48.     public void MostrarDB() throws SQLException{
  49.  
  50.         String NombreTabla = "socias";
  51.         String Query_Mostrar = "SELECT * FROM " + NombreTabla;
  52.  
  53.         comando = conexion.createStatement();
  54.         registro = comando.executeQuery(Query_Mostrar);
  55.  
  56.         while(registro.next()){
  57.  
  58.             System.out.println("NOMBRE: " + registro.getString(1) +
  59.                                 "\nRUT: " + registro.getString(2) +
  60.                              "\nTALLER: " + registro.getString(3) +
  61.                            "\nTELEFONO: " + registro.getString(4) +
  62.                              "\nSECTOR: " + registro.getString(5));
  63.             System.out.println("----------------------------------------");
  64.         }
  65.     }
  66.  
  67.  
  68.     public void GuardarDB()throws SQLException{
  69. //SI DEJO LOS ATRIBUTOS DEFINIDOS EN EL MÉTODO SI ME FUNCIONA        
  70. //        nombre = "JORGE";
  71. //        rut = "195194467";
  72. //        taller = "PIÑON";
  73. //        telefono = "58312889";
  74. //        sector = "CAJON";
  75.         String Query_Guardar = "INSERT INTO socias(nombre,rut,taller,telefono,sector) values ('"+nombre+"','"+rut+"','"+taller+"','"+telefono+"','"+sector+"')";
  76.  
  77.        
  78.         Statement st = conexion.createStatement();
  79.         st.executeUpdate(Query_Guardar);
  80.         conexion.close();
  81.         st.close();
  82.  
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement