1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.bestray.transtracking.dao.impl;
import com.bestray.transtracking.dao.AddressDao;
import com.bestray.trastrack.domain.Address;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.bestray.transtracking.dao.mapper.AddressMapper;
/**
*
* @author Girija Sahu
*/
public class AddressDaoImpl implements AddressDao {
private DataSource ds;
public AddressDaoImpl(DataSource ds) {
this.ds = ds;
}
public Address addAddress(final Address address) throws Exception {
JdbcTemplate template = new JdbcTemplate(ds);
KeyHolder keyHolder = new GeneratedKeyHolder();
final java.util.Date date= new java.util.Date();
template.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement("insert into address (address1,address2,city,state_id,country_id,created_date,updated_date,updated_by) values (?,?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
ps.setString(1, address.getAddress1());
ps.setString(2, address.getAddress2());
ps.setString(3, address.getCity());
ps.setLong(4, address.getStateId());
ps.setLong(5, 1);
ps.setTimestamp(6, new Timestamp(date.getTime()));
ps.setTimestamp(7, new Timestamp(date.getTime()));
ps.setInt(8, 1);
return ps;
}
}, keyHolder);
Long generatedId = new Long(keyHolder.getKey().longValue());
//System.out.println("Generated Id"+generatedId);
address.setId( generatedId);
return address;
}
public Address findById(long addressId) throws Exception{
JdbcTemplate template = new JdbcTemplate(ds);
String queryString = "Select * from address where id =?";
return (Address) template.queryForObject(queryString, new Object[]{addressId}, new AddressMapper());
}
public void updateAddress(Address address) throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
final java.util.Date date= new java.util.Date();
// jdbcTemplate.update("update t_actor set weapon = ? where id = ?", new Object[] {"Banjo", new Long(5276)});
String sql = "update address set address1 =? ,address2 =?, city=?,state_id=?,updated_date=? where id = ?";
jdbcTemplate.update(sql, new Object[]{address.getAddress1(), address.getAddress2(), address.getCity(),address.getStateId(),new Timestamp(date.getTime()),address.getId()});
}
public void delete(Long addressid) throws Exception{
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
jdbcTemplate.update("DELETE FROM address WHERE id = ?", new Object[]{addressid});
}
}
|