/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ram.dao;

/**
 *
 * @author Ram
 */
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.ram.bean.User;
public class UserDao {

public static Connection getConnection(){
    Connection con=null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","ram");
    }catch(Exception e){System.out.println(e);}
    return con;
}
public static int save(User u){
    int status=0;
    try{
        Connection con=getConnection();
        PreparedStatement ps=con.prepareStatement(
"insert into user(name,password,email,sex,country) values(?,?,?,?,?)");
        ps.setString(1,u.getName());
        ps.setString(2,u.getPassword());
        ps.setString(3,u.getEmail());
        ps.setString(4,u.getSex());
        ps.setString(5,u.getCountry());
        status=ps.executeUpdate();
    }catch(Exception e){System.out.println(e);}
    return status;
}
public static int update(User u){
    int status=0;
    try{
        Connection con=getConnection();
        PreparedStatement ps=con.prepareStatement(
"update user set name=?,password=?,email=?,sex=?,country=? where id=?");
        ps.setString(1,u.getName());
        ps.setString(2,u.getPassword());
        ps.setString(3,u.getEmail());
        ps.setString(4,u.getSex());
        ps.setString(5,u.getCountry());
        ps.setInt(6,u.getId());
        status=ps.executeUpdate();
    }catch(Exception e){System.out.println(e);}
    return status;
}
public static int delete(User u){
    int status=0;
    try{
        Connection con=getConnection();
        PreparedStatement ps=con.prepareStatement("delete from user where id=?");
        ps.setInt(1,u.getId());
        status=ps.executeUpdate();
    }catch(Exception e){System.out.println(e);}

    return status;
}
public static List<User> getAllRecords(){
    List<User> list=new ArrayList<User>();

    try{
        Connection con=getConnection();
        PreparedStatement ps=con.prepareStatement("select * from user");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            User u=new User();
            u.setId(rs.getInt("id"));
            u.setName(rs.getString("name"));
            u.setPassword(rs.getString("password"));
            u.setEmail(rs.getString("email"));
            u.setSex(rs.getString("sex"));
            u.setCountry(rs.getString("country"));
            list.add(u);
        }
    }catch(Exception e){System.out.println(e);}
    return list;
}
public static User getRecordById(int id){
    User u=null;
    try{
        Connection con=getConnection();
        PreparedStatement ps=con.prepareStatement("select * from user where id=?");
        ps.setInt(1,id);
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            u=new User();
            u.setId(rs.getInt("id"));
            u.setName(rs.getString("name"));
            u.setPassword(rs.getString("password"));
            u.setEmail(rs.getString("email"));
            u.setSex(rs.getString("sex"));
            u.setCountry(rs.getString("country"));
        }
    }catch(Exception e){System.out.println(e);}
    return u;
}
    public static void main(String[] args) {


User ub=new User();
ub.setName("Ram");
ub.setEmail("ramsoftware786@gmail.com");
ub.setCountry("india");
ub.setSex("Male");
ub.setPassword("aaaa");
UserDao ud=new UserDao();
ud.save(ub);
    }
}