-
Notifications
You must be signed in to change notification settings - Fork 10
/
MySQLAccess.java
128 lines (116 loc) · 4.01 KB
/
MySQLAccess.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
package io.bittiger.adindex;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLAccess {
private Connection d_connect = null;
private String d_user_name;
private String d_password;
private String d_server_name;
private String d_db_name;
public void close() throws Exception {
System.out.println("Close database");
try {
if (d_connect != null) {
d_connect.close();
}
} catch (Exception e) {
throw e;
}
}
public MySQLAccess(String server,String db,String user,String password) {
d_server_name = server;
d_db_name = db;
d_user_name = user;
d_password = password;
}
private Connection getConnection() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
String conn = "jdbc:mysql://" + d_server_name + "/" +
d_db_name+"?user="+d_user_name+"&password="+d_password;
System.out.println("Connecting to database: " + conn);
d_connect = DriverManager.getConnection(conn);
System.out.println("Connected to database");
return d_connect;
} catch(Exception e) {
throw e;
}
}
private Boolean isRecordExist(Connection connect,String sql_string) throws SQLException {
PreparedStatement existStatement = null;
boolean isExist = false;
try
{
existStatement = connect.prepareStatement(sql_string);
ResultSet result_set = existStatement.executeQuery();
if (result_set.next())
{
isExist = true;
}
}
catch(SQLException e )
{
System.out.println(e.getMessage());
throw e;
}
finally
{
if (existStatement != null)
{
existStatement.close();
};
}
return isExist;
}
public Ad.Builder getAdData(Long adId) throws Exception {
Connection connect = null;
PreparedStatement adStatement = null;
ResultSet result_set = null;
Ad.Builder ad = Ad.newBuilder();
String sql_string = "select * from " + d_db_name + ".ad where adId=" + adId;
try {
connect = getConnection();
adStatement = connect.prepareStatement(sql_string);
result_set = adStatement.executeQuery();
while (result_set.next()) {
ad.setAdId(result_set.getLong("adId"));
ad.setCampaignId(result_set.getLong("campaignId"));
String keyWords = result_set.getString("keyWords");
String[] keyWordsList = keyWords.split(",");
for(int index = 0; index < keyWordsList.length; index++) {
ad.addKeyWords(keyWordsList[index]);
}
ad.setBidPrice(result_set.getDouble("bidPrice"));
ad.setPrice(result_set.getDouble("price"));
ad.setThumbnail(result_set.getString("thumbnail"));
ad.setDescription(result_set.getString("description"));
ad.setBrand(result_set.getString("brand"));
ad.setDetailUrl(result_set.getString("detail_url"));
ad.setCategory(result_set.getString("category"));
ad.setTitle(result_set.getString("title"));
}
}
catch(SQLException e )
{
System.out.println(e.getMessage());
throw e;
}
finally
{
if (adStatement != null) {
adStatement.close();
};
if (result_set != null) {
result_set.close();
}
if (connect != null) {
connect.close();
}
}
return ad;
}
}