-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonDBService.java
More file actions
103 lines (93 loc) · 3.83 KB
/
PersonDBService.java
File metadata and controls
103 lines (93 loc) · 3.83 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package dbsample;
import org.apache.axis2.context.MessageContext;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class PersonDBService {
public Person[] listAllPeople() {
Connection conn = (Connection) MessageContext.getCurrentMessageContext().getProperty(
DBSampleServiceLifeCycle.DB_CONNECTION);
if(conn !=null) {
String SQL = "SELECT * FROM PERSON";
try {
PreparedStatement statement = conn.prepareStatement(SQL);
ResultSet result = statement.executeQuery();
ArrayList list = new ArrayList();
while (result.next()) {
Person person = new Person();
person.setId(result.getInt("ID"));
person.setName(result.getString("NAME"));
person.setAddress(result.getString("ADDRESS"));
person.setAge(result.getInt("AGE"));
list.add(person);
}
return (Person[])list.toArray(new Person[list.size()]);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public String[] listPeopleNames() {
Connection conn = (Connection) MessageContext.getCurrentMessageContext().getProperty(
DBSampleServiceLifeCycle.DB_CONNECTION);
if(conn !=null) {
String SQL = "SELECT NAME FROM PERSON";
try {
PreparedStatement statement = conn.prepareStatement(SQL);
ResultSet result = statement.executeQuery();
ArrayList list = new ArrayList();
while (result.next()) {
list.add(result.getString("NAME"));
}
return (String[])list.toArray(new String[list.size()]);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public NameAge getNameAge(int id) {
Connection conn = (Connection) MessageContext.getCurrentMessageContext().getProperty(
DBSampleServiceLifeCycle.DB_CONNECTION);
if(conn !=null) {
String SQL = "SELECT NAME , AGE FROM PERSON WHERE ID=?";
try {
PreparedStatement statement = conn.prepareStatement(SQL);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
if (result.next()) {
NameAge nameAge = new NameAge();
nameAge.setName(result.getString("NAME"));
nameAge.setAge(result.getInt("AGE"));
return nameAge;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public void insertPerson(int id,
String name,
String address,
int age) {
Connection conn = (Connection) MessageContext.getCurrentMessageContext().getProperty(
DBSampleServiceLifeCycle.DB_CONNECTION);
if(conn !=null) {
String SQL = "INSERT INTO PERSON (ID , NAME , ADDRESS , AGE) VALUES (?,?,?,?)";
try {
PreparedStatement statement = conn.prepareStatement(SQL);
statement.setInt(1,id);
statement.setString(2,name);
statement.setString(3,address);
statement.setInt(4,age);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}