import java.sql.*;
public class database
{Connection con;
int count;
number_sequence nsq;
public database() throws ClassNotFoundException
{nsq=new number_sequence();
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:customerBase");
Statement st=con.createStatement();
ResultSet res = st.executeQuery("SELECT COUNT(*) FROM customerbase");
while (res.next())
{count = res.getInt(1);
}
System.out.println("Number of Records"+count);
}catch(SQLException e)
{System.out.println(e);
}
catch(Exception e)
{System.out.println(e.getMessage());
}
}
public void add_to_database(customer c)
{try
{PreparedStatement ps=con.prepareStatement("insert into customerbase values(?,?,?)");
int random=nsq.integer_generator_upto(10000);
c.set_acc_no(random);
ps.setInt(1,random );
ps.setString(2, c.get_name());
ps.setInt(3,c.get_no_of_rentals());
int x=ps.executeUpdate();
System.out.println(x);
System.out.println("ID generated for customer is "+random);
}
catch(SQLException e)
{System.out.println(e);
System.out.println('*');
}
catch(Exception e)
{System.out.println(e.getMessage());
}
}
public void copy_database_into(LinkedList<customer> list) throws SQLException
{customer temp;
try
{PreparedStatement ps=con.prepareStatement("select * from customerbase");
ResultSet rs=ps.executeQuery();
while(rs.next())
{temp=new customer(rs.getInt("ID"),rs.getString("cust_name"),rs.getInt ("no_of_rentals"));
list.add(temp);
}
}
catch(SQLException e)
{System.out.println(e);
System.out.println("this is an SQL error");
}
catch(Exception e)
{System.out.println(e.getMessage());
}
System.out.println("database has been successfully copied to list");
}
public void update_rentals_for(int id,int by_)
{
try
{PreparedStatement ps=con.prepareStatement("select no_of_rentals from customerbase where ID=id");
ResultSet rs=ps.executeQuery();
rs.next();
int rentals=rs.getInt("no_of_rentals");
System.out.println("no of rentals is "+rentals);
System.out.println("the rentals is to be updated by "+by_);
rentals+=by_;
PreparedStatement ps2=con.prepareStatement("update customerbase set no_of_rentals=? where ID=?");
ps2.setInt(1, rentals);
ps2.setInt(2,id);
int r=ps2.executeUpdate();
System.out.println("updated "+rentals);
}
catch(SQLException e)
{System.out.println(e);
}
catch(Exception e)
{}
}