Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class Controller
- {
- private static Connection con;
- public Controller()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- public ArrayList<Customer> getAllCustomerDetails()
- {
- ArrayList<Customer> customerList = new ArrayList<Customer>();
- try
- {
- PreparedStatement pst = con.prepareStatement("select * from Customer");
- ResultSet rs = pst.executeQuery();
- while(rs.next())
- {
- Customer c = new Customer();
- c.setCustomerId(rs.getInt("customer_id"));
- c.setCustomerName(rs.getString("customer_name"));
- c.setAddress(rs.getString("address"));
- customerList.add(c);
- }
- }
- catch (Exception e)
- {
- System.out.println("Exception " + e);
- }
- return customerList;
- }
- public void insertCustomer(int thatId, String thatName, String thatAddress){
- try
- {
- Statement st = con.createStatement();
- PreparedStatement pst = con.prepareStatement("insert into customer values (?,?,?)");
- Scanner sc = new Scanner(System.in);
- pst.setInt(1,thatId);
- pst.setString(2,thatName);
- pst.setString(3,thatAddress);
- int ret = pst.executeUpdate();
- if (ret>0)
- {
- System.out.println(ret + " record created");
- }
- else
- System.out.println("Error");
- }
- catch (Exception e){
- System.out.println(e);
- }
- }
- public void updateCustomer(int thatId,String thatName ,String thatAddress){
- try
- {
- Statement st = con.createStatement();
- PreparedStatement pst = con.prepareStatement("update customer set customer_name = ?, address = ? where customer_id = ?");
- Scanner sc = new Scanner(System.in);
- pst.setInt(3,thatId);
- pst.setString(1,thatName);
- pst.setString(2,thatAddress);
- int ret = pst.executeUpdate();
- if (ret>0)
- {
- System.out.println(ret + " record updated");
- }
- else
- System.out.println("Error");
- }
- catch (Exception e){
- System.out.println(e);
- }
- }
- public void deleteCustomer(int thatId){
- try
- {
- Statement st = con.createStatement();
- PreparedStatement pst = con.prepareStatement("delete from customer where customer_id = ?");
- Scanner sc = new Scanner(System.in);
- pst.setInt(1,thatId);
- boolean ret = pst.execute();
- if (!ret)
- {
- System.out.println(ret + " record deleted");
- }
- else
- System.out.println("Error");
- }
- catch (Exception e){
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement