Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package datos;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import modelo.Estudiante;
- public class Conexion {
- private PreparedStatement pstmt;
- public Conexion() {
- }
- private static Connection crearConexion(){
- Connection conexion = null;
- String usuario = "root";
- String password = "";
- String url = "jdbc:mysql://localhost:3306/anteproyectodb?user=" + usuario + "&password=" + password;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conexion = (Connection) DriverManager.getConnection(url);
- if (conexion != null){
- System.out.println("Conectado a la base de datos.");
- }
- } catch(SQLException | ClassNotFoundException e){
- System.err.println("Error -> " + e.getMessage());
- }
- return conexion;
- }
- public boolean insertarEstudiante(Estudiante estudiante) {
- Connection conexion = crearConexion();
- try {
- String sql = "INSERT INTO estudiante VALUES (?, ?, ?, ?)";
- pstmt = conexion.prepareStatement(sql);
- pstmt.setString(1, estudiante.getCedula());
- pstmt.setString(2, estudiante.getNombres());
- pstmt.setString(3, estudiante.getApellidos());
- pstmt.setInt(4, estudiante.getSemestre());
- return pstmt.executeUpdate() > 0;
- } catch (SQLException e) {
- System.err.println("Error -> " + e.getMessage());
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement