Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tallereslaborales;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- public class Mysql {
- public String nombre,taller,sector,rut,telefono;
- Connection conexion = null;
- Statement comando = null;
- ResultSet registro;
- public void ConectarBD() throws SQLException{
- //CARGAR DRIVER JDBC
- try{
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch (ClassNotFoundException error){
- JOptionPane.showMessageDialog(null, "ERROR: " + error);
- }
- //CONECTAR MYSQL
- String servidor = "jdbc:mysql://localhost:3306/vilcun";
- String usuario="root";
- String password="";
- try{
- conexion = DriverManager.getConnection(servidor,usuario,password);
- }
- catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "ERROR: " + ex);
- }
- finally{
- JOptionPane.showMessageDialog(null, "¡CONECTADO A LA BASE DE DATOS!");
- }
- }
- public void MostrarDB() throws SQLException{
- String NombreTabla = "socias";
- String Query_Mostrar = "SELECT * FROM " + NombreTabla;
- comando = conexion.createStatement();
- registro = comando.executeQuery(Query_Mostrar);
- while(registro.next()){
- System.out.println("NOMBRE: " + registro.getString(1) +
- "\nRUT: " + registro.getString(2) +
- "\nTALLER: " + registro.getString(3) +
- "\nTELEFONO: " + registro.getString(4) +
- "\nSECTOR: " + registro.getString(5));
- System.out.println("----------------------------------------");
- }
- }
- public void GuardarDB()throws SQLException{
- //SI DEJO LOS ATRIBUTOS DEFINIDOS EN EL MÉTODO SI ME FUNCIONA
- // nombre = "JORGE";
- // rut = "195194467";
- // taller = "PIÑON";
- // telefono = "58312889";
- // sector = "CAJON";
- String Query_Guardar = "INSERT INTO socias(nombre,rut,taller,telefono,sector) values ('"+nombre+"','"+rut+"','"+taller+"','"+telefono+"','"+sector+"')";
- Statement st = conexion.createStatement();
- st.executeUpdate(Query_Guardar);
- conexion.close();
- st.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement