-
Notifications
You must be signed in to change notification settings - Fork 1
/
AlumnoData.java
62 lines (44 loc) · 1.57 KB
/
AlumnoData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* 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 universidad.modelo;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import universidad.entidades.Alumno;
/**
*
* @author pascu
*/
public class AlumnoData {
private Connection con;
public AlumnoData(Conexion conexion){
con=conexion.getConnection();
}
public void guardarAlumno(Alumno alumno){
String sql="INSERT into alumno (nombre_alumno, fn_alumno,activo) "
+ "VALUES(?, ?, ?);";
try{
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1,alumno.getNombre_alumno() );
ps.setDate(2, Date.valueOf(alumno.getFn_alumno()));
ps.setBoolean(3, alumno.isActivo());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
alumno.setId_alumno(rs.getInt(1));
} else {
JOptionPane.showMessageDialog(null,"No puedo obtener id");
}
con.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"Error al guardar Alumno");
}
}
}