LenguajeDao.java 1.5 KB
Newer Older
1 2 3 4
package com.roshka.proyectofinal.lenguaje;

import com.roshka.proyectofinal.DataBase;
import com.roshka.proyectofinal.entity.Lenguaje;
5
import jakarta.servlet.RequestDispatcher;
6 7 8

import java.sql.Connection;
import java.sql.PreparedStatement;
9 10 11 12
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
13 14 15 16 17 18 19 20 21

public class LenguajeDao {

    public static int save(Lenguaje l){
        int status=0;
        try{
            Connection con= DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement(
                    "insert into lenguaje (nombre_lenguaje) values (?)");
22

23 24 25 26 27 28 29 30 31
            ps.setString(1,l.getNombre_lenguaje());

            status=ps.executeUpdate();

            con.close();
        }catch(Exception ex){ex.printStackTrace();}

        return status;
    }
32

33
    public static List<Lenguaje> listar(){
34 35 36 37 38 39 40 41 42 43 44 45
        ArrayList<Lenguaje>list = new ArrayList<>();
        String sql = "select * from lenguaje";
        try{
            Connection con= DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                Lenguaje len = new Lenguaje();
                len.setId(rs.getInt("id"));
                len.setNombre_lenguaje(rs.getString("nombre_lenguaje"));
                list.add(len);
            }
46

47 48 49 50 51 52 53
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
  }