Guest User

mm-dao

a guest
Oct 30th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.41 KB | None | 0 0
  1. package marketmanagement.dao;
  2.  
  3. import java.sql.CallableStatement;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import com.mysql.jdbc.PreparedStatement;
  13.  
  14. import marketmanagement.model.ProductDetail;
  15. import marketmanagement.model.PurchaseProduct;
  16. import marketmanagement.model.RoleMaster;
  17. import marketmanagement.model.UserManagement;
  18.  
  19. public class Dao {
  20.  
  21.  
  22. public static Connection getConnection()throws SQLException, ClassNotFoundException{
  23.  
  24. Connection connection=null;
  25.  
  26. Class.forName("com.mysql.jdbc.Driver");
  27. connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/marketmanagement","root","root");
  28.  
  29. return connection;
  30. }
  31.  
  32. public List<RoleMaster> getUserRole()throws Exception{
  33. String getName="select roleId,roleName from rolemaster";
  34. /*int ret=
  35. DatabaseUtil.select(getName, null, (ResultSet rs)->{
  36.  
  37. return 0;
  38. });*/
  39.  
  40.  
  41. List<RoleMaster> roleNameList=new ArrayList<RoleMaster>();
  42. Connection connection=null;
  43. try{
  44. connection=getConnection();
  45. Statement statement=connection.createStatement();
  46. ResultSet rs=statement.executeQuery(getName);
  47. while(rs.next()){
  48. RoleMaster getRole=new RoleMaster();
  49. getRole.setRoleId(rs.getInt("roleId"));
  50. getRole.setRoleName(rs.getString("roleName"));
  51.  
  52. roleNameList.add(getRole);
  53. }
  54. }finally {
  55. connection.close();
  56. }
  57. return roleNameList;
  58.  
  59. }
  60.  
  61. public int userRegistraton(UserManagement usermngmt) throws SQLException, ClassNotFoundException{
  62. Connection connection=null;
  63. int status=0;
  64. try{
  65. connection=getConnection();
  66. String insertUser="insert into usermanagement(userName,email,password,phoneNo,dateOfBirth,balance,roleId) values('"+usermngmt.getUserName()+"','"+usermngmt.getEmail()+"','"+usermngmt.getPassword()+"','"+usermngmt.getPhoneNo()+"','"+usermngmt.getDateOfBirth()+"','"+usermngmt.getBalance()+"','"+usermngmt.getRoleId()+"')";
  67. Statement statement=connection.createStatement();
  68. status=statement.executeUpdate(insertUser);
  69.  
  70. }finally {
  71. connection.close();
  72. }
  73.  
  74.  
  75. return status;
  76.  
  77. }
  78. public UserManagement loginuser(String email)throws SQLException, ClassNotFoundException{
  79. Connection connection=null;
  80.  
  81. try{
  82. connection=getConnection();
  83. String getLoginDetails="select userName,email,password,roleId from usermanagement where email='"+email+"'";
  84. //System.out.println("getLoginDetails="+getLoginDetails);
  85. Statement statement=connection.createStatement();
  86. ResultSet rs=statement.executeQuery(getLoginDetails);
  87. if(rs.next()){
  88. UserManagement usermngmt=new UserManagement();
  89. usermngmt.setUserName(rs.getString("userName"));
  90. usermngmt.setEmail(rs.getString("email"));
  91. usermngmt.setPassword(rs.getString("password"));
  92. usermngmt.setRoleId(rs.getInt("roleId"));
  93. return usermngmt;
  94. }
  95. }finally {
  96. connection.close();
  97. }
  98.  
  99. return null;
  100. }
  101. /*public List<UserManagement> loginUser1(String email)throws SQLException, ClassNotFoundException{
  102. List<UserManagement> loginUser=new ArrayList<UserManagement>();
  103.  
  104. Connection connection=null;
  105. try{
  106. connection=getConnection();
  107. String getLoginDetails="select userName,email,password,roleId from usermanagement where email="+email;
  108. Statement statement=connection.createStatement();
  109. ResultSet rs=statement.executeQuery(getLoginDetails);
  110. if(rs.next()){
  111. UserManagement usermngmt=new UserManagement();
  112. usermngmt.setUserName(rs.getString("userName"));
  113. usermngmt.setEmail(rs.getString("email"));
  114. usermngmt.setPassword(rs.getString("password"));
  115. usermngmt.setRoleId(rs.getInt("roleId"));
  116.  
  117. loginUser.add(usermngmt);
  118. }
  119. }finally {
  120. connection.close();
  121. }
  122.  
  123. return loginUser;
  124.  
  125. }*/
  126. public List<UserManagement> useralrdyLogin(int id)throws SQLException, ClassNotFoundException{
  127. List<UserManagement> loginUser=new ArrayList<UserManagement>();
  128. Connection connection=null;
  129. try{
  130. connection=getConnection();
  131. String getLoginDetails="select userId,userName from usermanagement where roleId="+id;
  132. Statement statement=connection.createStatement();
  133. ResultSet rs=statement.executeQuery(getLoginDetails);
  134. while(rs.next()){
  135. UserManagement usermngmt=new UserManagement();
  136. usermngmt.setUserId(rs.getInt("userId"));
  137. usermngmt.setUserName(rs.getString("userName"));
  138.  
  139. loginUser.add(usermngmt);
  140. }
  141. }finally {
  142. connection.close();
  143. }
  144.  
  145. return loginUser;
  146.  
  147. }
  148.  
  149. public List<UserManagement> listUser(int roleId) throws SQLException, ClassNotFoundException {
  150. //List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
  151. List<UserManagement> list=new ArrayList<UserManagement>();
  152. Connection connection=null;
  153. try{
  154.  
  155. connection=getConnection();
  156. /*StringBuilder builder = new StringBuilder();
  157. builder.append("select um.userId,um.userName,um.email,um.password,um.phoneNo,um.dateOfBirth,um.balance,um.roleId,rm.roleName");
  158. builder.append(" from usermanagement um ");
  159. builder.append(" inner join rolemaster rm ON um.roleId=rm.roleId ");
  160. if(roleId>0)
  161. builder.append(" where um.roleId=").append(roleId);
  162.  
  163. String retriveStudentList=builder.toString();
  164. Statement statement=connection.createStatement();*/
  165. //System.out.println(retriveStudentList);
  166. CallableStatement cstmt=connection.prepareCall("{call studentList(?)}");
  167. cstmt.setInt(1, roleId);
  168. ResultSet rs=cstmt.executeQuery();
  169. while(rs.next()){
  170. //userId, userName, email, password, phoneNo, dateOfBirth, balance,roleId
  171. UserManagement userManagement=new UserManagement();
  172. userManagement.setUserId(rs.getInt("userId"));
  173. userManagement.setUserName(rs.getString("userName"));
  174. userManagement.setEmail(rs.getString("email"));
  175. userManagement.setPassword(rs.getString("password"));
  176. userManagement.setPhoneNo(rs.getString("phoneNo"));
  177. userManagement.setDateOfBirth(rs.getString("dateOfBirth"));
  178. userManagement.setBalance(rs.getString("balance"));
  179. userManagement.setRoleId(rs.getInt("roleId"));
  180. userManagement.setRoleName(rs.getString("roleName"));
  181. list.add(userManagement);
  182. }
  183.  
  184.  
  185. return list;
  186. }
  187. finally {
  188. connection.close();
  189. }
  190. }
  191.  
  192. public List<ProductDetail> listProduct(int userId)throws SQLException, ClassNotFoundException{
  193.  
  194. List<ProductDetail> listProduct=new ArrayList<ProductDetail>();
  195. Connection connection=null;
  196. try{
  197. connection=getConnection();
  198. /*StringBuilder builder = new StringBuilder();
  199. builder.append("select p.productId,p.productCode,p.productName,p.productPrice,p.productDesription,p.userId,um.userName");
  200. builder.append(" from product p ");
  201. builder.append(" inner join usermanagement um ON p.userId=um.userId ");
  202. if(userId>0)
  203. builder.append(" where p.userId=").append(userId);
  204.  
  205. String getProductList=builder.toString();*/
  206. //System.out.println(getProductList);
  207. CallableStatement cstmt=connection.prepareCall("{call listProduct(?)}");
  208. cstmt.setInt(1, userId);
  209. //Statement statement=connection.createStatement();
  210. ResultSet rs=cstmt.executeQuery();
  211. while(rs.next()){
  212. ProductDetail product=new ProductDetail();
  213. product.setProductId(rs.getInt("productId"));
  214. product.setProductCode(rs.getString("productCode"));
  215. product.setProductName(rs.getString("productName"));
  216. product.setProductPrice(rs.getString("productPrice"));
  217. product.setProductQuntity(rs.getInt("productQuntity"));
  218. product.setProductDescription(rs.getString("productDesription"));
  219. product.setUserId(rs.getInt("userId"));
  220. product.setUserName(rs.getString("userName"));
  221.  
  222. listProduct.add(product);
  223.  
  224. }
  225. }finally {
  226. connection.close();
  227. }
  228.  
  229. return listProduct;
  230.  
  231. }
  232.  
  233. public UserManagement getUserById(int id) throws SQLException, ClassNotFoundException {
  234. UserManagement userManagement=null;
  235. //List<StudentInfo> list=new ArrayList<StudentInfo>();
  236. try(Connection connection=getConnection();
  237. ){
  238. //String retriveUserByID="select * from usermanagement where userId="+id;
  239. CallableStatement cstmt=connection.prepareCall("{call getUserById(?)}");
  240. cstmt.setInt(1, id);
  241. // Statement statement=connection.createStatement();
  242. ResultSet rs=cstmt.executeQuery();
  243. if(rs.next()){
  244.  
  245. userManagement=new UserManagement();
  246. userManagement.setUserId(rs.getInt("userId"));
  247. userManagement.setUserName(rs.getString("userName"));
  248. userManagement.setEmail(rs.getString("email"));
  249. userManagement.setPassword(rs.getString("password"));
  250. userManagement.setPhoneNo(rs.getString("phoneNo"));
  251. userManagement.setDateOfBirth(rs.getString("dateOfBirth"));
  252. userManagement.setBalance(rs.getString("balance"));
  253. userManagement.setRoleId(rs.getInt("roleId"));
  254. return userManagement;
  255. //list.add(student);
  256. }else{
  257. return null;
  258. }
  259. }
  260.  
  261. //return student;
  262. }
  263.  
  264. public int updateUserInfo(UserManagement userManagement) throws SQLException, ClassNotFoundException {
  265. int x=0;
  266. Connection connection=null;
  267. try{//userId, userName, email, password, phoneNo, dateOfBirth, roleId
  268. connection=getConnection();
  269. String updateUser="update usermanagement set userName='"+userManagement.getUserName()+"',email='"+userManagement.getEmail()+"',password='"+userManagement.getPassword()+"',phoneNo='"+userManagement.getPhoneNo()+"',dateOfBirth='"+userManagement.getDateOfBirth()+"',balance='"+userManagement.getBalance()+"',roleId='"+userManagement.getRoleId()+"' where userId='"+userManagement.getUserId()+"'";
  270. //System.out.println(updateUser);
  271. Statement statement=connection.createStatement();
  272. x=statement.executeUpdate(updateUser);
  273. }
  274. finally{
  275. connection.close();
  276. }
  277. return x;
  278. }
  279.  
  280. public int deleteUser(int id) throws SQLException, ClassNotFoundException {
  281. int status=0;
  282. Connection connection=null;
  283. try{
  284. connection=getConnection();
  285. //StudentInfo student=new StudentInfo();
  286.  
  287. Statement st=connection.createStatement();
  288. String deleteStudent="delete from usermanagement where userId="+id;
  289.  
  290. status=st.executeUpdate(deleteStudent);
  291. }
  292. finally{
  293. connection.close();
  294. }
  295. return status;
  296. }
  297.  
  298. //productId, productcode, productName, productPrice, productDesription, userId
  299. public int productDetail(ProductDetail product) throws SQLException, ClassNotFoundException{
  300. Connection connection=null;
  301. int status=0;
  302. try{
  303. connection=getConnection();
  304. /*String insertProduct="insert into product(productcode, productName, productPrice, productQuntity, productDesription, userId) values('"+product.getProductCode()+"','"+product.getProductName()+"','"+product.getProductPrice()+"','"+product.getProductDescription()+"','"+product.getUserId()+"')";
  305. Statement statement=connection.createStatement();*/
  306. CallableStatement cstmt=connection.prepareCall("{call productDetail(?,?,?,?,?,?)}");
  307. cstmt.setString(1, product.getProductCode());
  308. cstmt.setString(2, product.getProductName());
  309. cstmt.setString(3, product.getProductPrice());
  310. cstmt.setInt(4, product.getProductQuntity());
  311. cstmt.setString(5, product.getProductDescription());
  312. cstmt.setInt(6, product.getUserId());
  313. status=cstmt.executeUpdate();
  314.  
  315. }finally {
  316. connection.close();
  317. }
  318. return status;
  319. }
  320.  
  321. public ProductDetail getProductById(int id) throws SQLException, ClassNotFoundException {
  322. ProductDetail product=null;
  323. Connection connection=null;
  324. //List<StudentInfo> list=new ArrayList<StudentInfo>();
  325. try{
  326. connection=getConnection();
  327. /*String retriveProductByID="select * from product where productId="+id;
  328.  
  329. Statement statement=connection.createStatement();*/
  330. CallableStatement cstmt=connection.prepareCall("{call getProductById(?)}");
  331. cstmt.setInt(1, id);
  332. ResultSet rs=cstmt.executeQuery();
  333. if(rs.next()){
  334.  
  335. product=new ProductDetail();
  336. product.setProductId(rs.getInt("productId"));
  337. product.setProductCode(rs.getString("productCode"));
  338. product.setProductName(rs.getString("productName"));
  339. product.setProductPrice(rs.getString("productPrice"));
  340. product.setProductQuntity(rs.getInt("productQuntity"));
  341. product.setProductDescription(rs.getString("productDesription"));
  342. product.setUserId(rs.getInt("userId"));
  343. //product.setUserName(rs.getString("userName"));
  344. //System.out.println("productdetails="+product);
  345. return product;
  346. //list.add(student);
  347. }else{
  348. return null;
  349. }
  350. }
  351. finally{
  352. connection.close();
  353. }
  354.  
  355. }
  356.  
  357. public int updateProductInfo(ProductDetail product) throws SQLException, ClassNotFoundException {
  358. int x=0;
  359. Connection connection=null;
  360. try{//productId, productcode, productName, productPrice, productDesription, userId
  361. connection=getConnection();
  362. String updateUser="update product set productcode='"+product.getProductCode()+"',productName='"+product.getProductName()+"',productPrice='"+product.getProductPrice()+"',productQuntity='"+product.getProductQuntity()+"',productDesription='"+product.getProductDescription()+"',userId='"+product.getUserId()+"' where productId='"+product.getProductId()+"'";
  363. //System.out.println(updateUser);
  364. Statement statement=connection.createStatement();
  365. x=statement.executeUpdate(updateUser);
  366. }
  367. finally{
  368. connection.close();
  369. }
  370. return x;
  371. }
  372.  
  373. public int deleteProduct(int id) throws SQLException, ClassNotFoundException {
  374. int status=0;
  375. Connection connection=null;
  376. try{
  377. connection=getConnection();
  378. //StudentInfo student=new StudentInfo();
  379.  
  380. String deleteStudent="delete from product where productId="+id;
  381. Statement st=connection.createStatement();
  382. status=st.executeUpdate(deleteStudent);
  383. }
  384. finally{
  385. connection.close();
  386. }
  387. return status;
  388. }
  389.  
  390. public int purchaseProduct(PurchaseProduct product) throws SQLException, ClassNotFoundException{
  391. Connection connection=null;
  392. int status=0;
  393. try{
  394. connection=getConnection();
  395. /*String insertProduct="insert into product(productcode, productName, productPrice, productQuntity, productDesription, userId) values('"+product.getProductCode()+"','"+product.getProductName()+"','"+product.getProductPrice()+"','"+product.getProductDescription()+"','"+product.getUserId()+"')";
  396. Statement statement=connection.createStatement();*/
  397. CallableStatement cstmt=connection.prepareCall("{call purchaseproduct(?,?,?,?,?,?,?,?)}");
  398. cstmt.setString(1, product.getProductCode());
  399. cstmt.setString(2, product.getProductName());
  400. cstmt.setInt(3, product.getProductPrice());
  401. cstmt.setInt(4, product.getProductQuntity());
  402. cstmt.setInt(5, product.getTotal());
  403. cstmt.setString(6, product.getSeller());
  404. cstmt.setInt(7, product.getProductId());
  405. cstmt.setInt(8, product.getUserId());
  406. status=cstmt.executeUpdate();
  407.  
  408. }finally {
  409. connection.close();
  410. }
  411. return status;
  412. }
  413.  
  414. public List<PurchaseProduct> listPurchaseProduct(int userId)throws SQLException, ClassNotFoundException{
  415.  
  416. List<PurchaseProduct> listPurchase=new ArrayList<PurchaseProduct>();
  417. Connection connection=null;
  418. try{
  419. connection=getConnection();
  420. CallableStatement cstmt=connection.prepareCall("{call listPurchaseProduct(?)}");
  421. cstmt.setInt(1, userId);
  422. ResultSet rs=cstmt.executeQuery();
  423. while(rs.next()){
  424. PurchaseProduct purchase=new PurchaseProduct();
  425. purchase.setPurchaseId(rs.getInt("purchaseId"));
  426. purchase.setProductCode(rs.getString("productCode"));
  427. purchase.setProductName(rs.getString("productName"));
  428. purchase.setProductPrice(rs.getInt("productPrice"));
  429. purchase.setProductQuntity(rs.getInt("productQuntity"));
  430. purchase.setTotal(rs.getInt("total"));
  431. purchase.setSeller(rs.getString("seller"));
  432. purchase.setProductId(rs.getInt("productId"));
  433. purchase.setUserId(rs.getInt("userId"));
  434. purchase.setUserName(rs.getString("userName"));
  435.  
  436. listPurchase.add(purchase);
  437.  
  438. }
  439. }finally {
  440. connection.close();
  441. }
  442. return listPurchase;
  443. }
  444. }
Add Comment
Please, Sign In to add comment