You are on page 1of 16

Update User

e the mobile number and email address of the shareholders of his company.So he decides to develop the application which display the owner table and to g
email id of the owner.
tion using jdbc template to display the owner table and to update the mobile and email id.

ject-oriented specifications given as a part of the problem statement.


entions as mentioned. Create separate classes in separate files.]

swith the following attributes/variables


Attributes
name

password
emailld
mobileNumber

ters and setterS.

class with the following attributes/variables


|Attributes
dataSource
SCROLL DOWN TO SOLVE
Main.java
1 import com.spring.entity.*;
2 import com.spring.dao.*;
3 import java,awt. List;
4 import java.lo.BufferedReader;
5 import Java.lo.IOExceptlon
6 import Java.lo.InputStreamReader;
import java.util.loggìng. Level;
import Java.util.logging.Logger
10 import org.springtramework.context.ApplcatlonContext;
11
import org.springramework.context.support.Class PathXmlAppllcationContext;
12
13Vpublic class Main
14
15
public static void main(Stringl] args) throws NumberFormatException, IOException {
16 Logger log=Logger.getLogger("'org.hibernate");
17 log.setlLevel (Level.OFF);
18
System.setProperty("org.apache.commons.logging.Log'", "org.apache.commons.logging.impl.NoOpLog);
19
20 BufferedReader br= new BufferedReader(new InputStreamReader(System.In);
21
22
23
ApplicationContext context =newClassPathxmlApplicationContext("applicationContext.xml");
24 OwnerDAO dao = context.getBean("ownerDAO", OwnerDAO.class);
25
26 System.out.printn("...Owner List ...");
27 System.out.printf("%-15s%-15s %-15s %s\n", "id", "Name", "Email", "Mobile"):
28 for (Owner o: dao.getAllOwners()){
29
System.out-printf("%-15s %-15s %-15s %sin", o.getld ), o.getName ), o.getEmailld). Q.getMobileNumber )):
28 for (Ownero: dao.getAIIOWners ()){
29 System.out.printf("%-15s %-15s %-15s %s\n", o.getid (), o.getName 0, o.getEmald (), o.getMobileNumber());
30
31
32 System.out-printin("Enter the id you want to update");
33 int id =Integer-parselnt (br. readlLine());
34
35 V if (dao.getOwnerByld(d) =null){
36 System.out.printin ("No such owner");
37 }else{
38 System.out-printin ("Enter the mobile number and email id");
39 String mobile = br.readLine();
40 String email =br.readLine();
41
42 dao.update (id, mobile, emal);
43
44 System.out-printn ("updated successfully");
45
46 System.out.printin ("...Owner List ...");
47 System.out.printf("%-15s %-15s %-15s %sn", "id", "Name", "Emal", "Mobile"):
48 for (Owner o: dao.getAlIOwners(0){
49 V System.out.printf("%-15s %-15s %-15s %s\n", o.getid(), o.getName (), 0.getEmaild(),
50 0.getMobileNumber());
51
52
53
54
55
56
applcatlonContext,xml X
1VBbeans xmlns-"htp/www.springtramework.org/schema/beans"
xmlns:xsl="http:/w.W3.orn/2001/XMLSchema-Instance
Xmlnsscontext="http/Aww.sprlngtramework.org/schemalcontext"
xs0schemaLocatlon
http:/Aw.springtramework.org/schema/beans
htp/A.Springtramework.org/schema/beans/spring-beans-3.1.xsd
http:/AwW.springtramework.org/schema/context
http:/wW.springtramework.org/schema/context/sprlng-context-3.1xsd2
10 <context:annotation-config />
11 <context:component-scan
12 base-package="com.spring.dao" />
13V context:property-placeholder
14 location=classpath:oracle.propertles" />
15
16 V Cbean id="dataSource"
17 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
19 value-"oracle.jdbc.drlver.OracleDrlver" />
20 <property name-"url" value="S{db.url}"/
21 <propertyname="usermame" value="${db.usermame}" >
22 <property name-"password" value="S(db.password}" />
23 <Jbean>
24
25 V <bean class-"org.springframework.dbc,core. JdbcTemplate"
26 id-"jdbcTemplate">
27 <property name="datasSource" refs"dataSource">
28 </bean>
29
Owner Java
package com.sprlng.entty
2
3 Vpublicclass Owner
private int ld;
private String name;
private String password;
private String mobileNumber;
private String emalilld;
10 public int getld(){
11 return id;
12
13
14 V public void setld(int id) {
15 this.id = id;
16
17

18 public String getName() {


19 returnname;
20
21
22 public void setName(String name) {
23 this,name = namej
24
25
26 public String getPassword() {
27 return password;
28
OwnerJava
33
34 V public Strling getMoblleNumber() {
35 return mobleNumber;
36
37
38 public void setMobileNumber(String mobileNumber) {
39 this.mobileNumber = mobileNumber;
40 }
41
42 public String getEmald() {
43 return emailld;
44 }
45
46 publicvoid setEmailld(Stringemailld) {
4 this.emailld = emailld;
48
49
50 public Owner() {
51 }
52
53 public Owner(String name, String password, String mobilenumber, String emailid) {
54 this.name = name;
55 this.password =password;
56 this.mobileNumber = mobilenumber;
57 this,emalld = emailid;
58
59
60
61
Delete User
inwanted owners from the list as he buys their shares in the company. So he
er list and todelete the owner for that particular id.

of the problem statement.


Sses in separate files.]

Attributes
name

password
emailld
mobileNumber

es

Attributes
dataSource
jdbcTemplate

SCROLL DOWN TO SOLVE


Main.java
mport com.spring.entty.*;
import com.spring.dao,*
import java.lo.BufferedReader;
import java.lo.IOException;
import Java.lo.InputStreamReader;
import java.util. List;
import java.util.logging. Level;
import java.util.logging. Logger;
import org.springframework.context.ApplicationContext;
10 import org-springframework.context.support.ClassPathXmlApplicationContext;
12 v public class Main {
publicstatic void main(String[] args) throws NumberFormatException, IOException {
Logger log=Logger.getLogger("org.hibernate");
15 log.setlevel (Level.OFF);
16 System.setProperty("org.apache.commons. logging.Log", "org.apache.commons.logging.implLNo OpLog"):
17
18 ApplicationContext context = new ClassPathBmlApplicationContext("applicationContext.xmi":
19 BufferedReader br = new BufferedReader(new InputStreamReader(System.in):
20 System.out-printin("... Owner List ... ");
21 System.out.printf("%-15s %-15s %-15s %s\n", "d", "Name", "Email", "Mobile"):
22 OwnerDAOownerDA0= (OwnerDAO) context.getBean("ownerDAo");
23 List<Owner> list = ownerDAO.getAllOwners();
24 for (Owner owner : ist) {
25 System.out-printf("%-15s%-15s %-15s%s\n", owner.getld(), owner.getName(),owner.getEmailld().
26 Owner-getMobileNumber());
27
28 System.out.printin("Enter the id of the person you want to delete"):
29 int id =Integer,parselnt(br.readLine());
Owner.java X appllcatlonContext.xml
Main.java
1 þackage com.spring.entty;
3 V
public class Ownmer {
private Int ld;
private Stringname;
private String password;
private String mobileNumber;
8 private String emalld;
10 public int getid () {
11 return ld;
12
13
14 v public void setid(int Id) {
15 this.ld = ld;
16 }
17
18 public String getName(){
19 return name;
20
21
22 v public vold setName(String name) {
23 this,name = name;
24
25
26 V public String getPassword() {
27 return password;
28
29
OwnerDAO,java ONnerJava applicatlonContext.xnl a Maln.java
þackage com.spring.dao;
import com.spring.entity.*
import java.util. Listt
import javaxsql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import orgsprìngframeworkjdbc.core.BeanPropertyRowMapper;
import org-springframework. jdbc.core.JdbcTemplate;
10 V public class OwnerDAO{
11 private Jdbclemplate jdbcTemplate;
12
13 @Autowired
14 v public void setDataSource(DataSource dataSource) {
15 this.jdbcTemplate= new JdbcTemplate(dataSource);
16
17
18 public ListCOwner> getAllOwners() { I
19 ListKOwner> list =jdbcTemplate.query("select *from owner", new BeanPropertyRowMapper(Owner.class))F
20 return list;
21
22
23 public String getOwnerByld(int id) {
24 try{
25 V String ownerName =jdbcTemplate.queryForObject("select name from owner where id-?", new Object[] {id },
26 String.class);
27 return ownerName;
28 }catch (Exception e) (
29 // TODO: handle exception
OwnerDAO,java Owner.java applicationContext Xml Maln.java
2 import com.spring.entity.*;
Import Java.util. List;
4 import javax.sql.DataSource;
5 import org.springframework. beans.factory.annotatlon.Autowired;
6 import org.springtramework. jdbe.core.BeanPropertyRowMapper;
7 Import org.springtramework,jdbc.core.JdbcTemplate;
8V public class OwnerDAO
private JdbcTemplate jdbcTemplate;
10 @Autowired
11 public void setDataSource(DataSource dataSource) {
12 this jdbcTemplate=new JdbcTemplate(dataSource);
13
14 public List<Owner> getAllOwners() {
15 ListKOwner> list= jdbcTemplate.query("select*from owner", new BeanPropertyRowMapper(Owner.class)):
16 return list
17
18 public String getOwnerByld (int id) {
19 try{
20 String ownerName =jdbcTemplate.queryfForObject("select name from owner where id-?, new Object[] {id },
21 String.class);
22 return ownerNane;
23 }catch (Exception e) {
24 return null;
25
26
27 public void deleteOwner (int id) {
28 jdbcTemplate.update("delete from owner where id =?", id);
29
30

You might also like