-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbService.java
154 lines (139 loc) · 4.11 KB
/
DbService.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cs3220.servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DbService {
private String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu79";
private String username = "cs3220stu79";
private String password = "4VmDDSf5Y2Ar";
private Connection connection;
public DbService()
{
try
{
connection = DriverManager.getConnection( url, username, password );
}
catch( SQLException e )
{
e.printStackTrace();
}
}
public void close()
{
if( connection != null )
{
try
{
connection.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}
}
public List<ContactEntry> getEntries()
{
List<ContactEntry> entries = new ArrayList<ContactEntry>();
try
{
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( "select * from contacts" );
while( rs.next() )
{
ContactEntry entry = new ContactEntry();
entry.setId( rs.getInt( "id" ) );
entry.setName( rs.getString( "name" ) );
entry.setPhone( rs.getString( "phone" ) );
entries.add( entry );
}
stmt.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
return entries;
}
public ContactEntry getEntry( int id )
{
ContactEntry entry = new ContactEntry();
try
{
String sql = "select * from contacts where id = ?";
PreparedStatement pstmt = connection.prepareStatement( sql );
pstmt.setInt( 1, id );
ResultSet rs = pstmt.executeQuery();
if( rs.next() )
{
entry.setId( rs.getInt( "id" ) );
entry.setName( rs.getString( "name" ) );
entry.setPhone( rs.getString( "phone" ) );
}
pstmt.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
return entry;
}
public int addEntry( String name, String message )
{
int id = 0;
try
{
String sql = "insert into contacts (name, message) values (?, ?)";
PreparedStatement pstmt = connection.prepareStatement( sql,
Statement.RETURN_GENERATED_KEYS );
pstmt.setString( 1, name );
pstmt.setString( 2, message );
pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
if( rs.next() ) id = rs.getInt( 1 );
pstmt.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
return id;
}
public void updateEntry( int id, String name, String message )
{
try
{
String sql = "update contacts set name = ?, phone = ? where id = ?";
PreparedStatement pstmt = connection.prepareStatement( sql );
pstmt.setString( 1, name );
pstmt.setString( 2, message );
pstmt.setInt( 3, id );
pstmt.executeUpdate();
pstmt.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}
public void deleteEntry( int id )
{
try
{
String sql = "delete from contacts where id = ?";
PreparedStatement pstmt = connection.prepareStatement( sql );
pstmt.setInt( 1, id );
pstmt.executeUpdate();
pstmt.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}
}