SHARE
TWEET

Untitled

a guest Jun 17th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package datos;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import modelo.Estudiante;
  8.  
  9. public class Conexion {
  10.     private PreparedStatement pstmt;
  11.  
  12.     public Conexion() {
  13.     }
  14.  
  15.     private static Connection crearConexion(){
  16.         Connection conexion = null;
  17.         String usuario = "root";
  18.         String password = "";
  19.         String url = "jdbc:mysql://localhost:3306/anteproyectodb?user=" + usuario + "&password=" + password;
  20.  
  21.         try{
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.  
  24.             conexion = (Connection) DriverManager.getConnection(url);
  25.  
  26.             if (conexion != null){
  27.                 System.out.println("Conectado a la base de datos.");
  28.             }
  29.         } catch(SQLException | ClassNotFoundException e){
  30.             System.err.println("Error -> " + e.getMessage());
  31.         }
  32.  
  33.         return conexion;
  34.     }
  35.  
  36.     public boolean insertarEstudiante(Estudiante estudiante) {
  37.         Connection conexion = crearConexion();
  38.  
  39.         try {
  40.             String sql = "INSERT INTO estudiante VALUES (?, ?, ?, ?)";
  41.  
  42.             pstmt = conexion.prepareStatement(sql);
  43.             pstmt.setString(1, estudiante.getCedula());
  44.             pstmt.setString(2, estudiante.getNombres());
  45.             pstmt.setString(3, estudiante.getApellidos());
  46.             pstmt.setInt(4, estudiante.getSemestre());
  47.  
  48.             return pstmt.executeUpdate() > 0;
  49.         } catch (SQLException e) {
  50.             System.err.println("Error -> " + e.getMessage());
  51.  
  52.             return false;
  53.         }
  54.     }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top