package JDBC;

import pojo.IcCard;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class IcCardJDBC {

    public IcCardJDBC() {
        super();
    }

    public Connection getConnection() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/parkinglot", "root", "root");
    }

    public void templateMethod() throws Exception {
        Connection conn = this.getConnection();
        PreparedStatement ps = conn.prepareStatement("");

        conn.close();
    }

    public IcCard get_IcCardById(int id) throws Exception {
        Connection conn = this.getConnection();
        PreparedStatement ps = conn.prepareStatement("SELECT *\n" +
                "FROM icinformation\n" +
                "WHERE user_id = ?;"
        );
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        IcCard pr = null;
        if(rs.next()) {
            pr = new IcCard(
                    rs.getInt("user_id"),
                    rs.getString("card_number"),
                    rs.getString("password"),
                    rs.getDouble("balance")
            );
        }
        conn.close();
        return pr;
    }
}



