| 16639 |
amit.gupta |
1 |
package in.shop2020.mobileapi.serving.services;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.mobileapi.serving.utils.Utils;
|
|
|
4 |
|
|
|
5 |
import java.sql.Connection;
|
|
|
6 |
import java.sql.DriverManager;
|
|
|
7 |
import java.sql.PreparedStatement;
|
|
|
8 |
import java.sql.SQLException;
|
|
|
9 |
import java.sql.Timestamp;
|
|
|
10 |
import java.util.Calendar;
|
|
|
11 |
|
|
|
12 |
public class Mysql {
|
|
|
13 |
// JDBC driver name and database URL
|
|
|
14 |
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
|
15 |
static final String DB_URL = "jdbc:mysql://%s/user";
|
|
|
16 |
|
|
|
17 |
// Database credentials
|
|
|
18 |
static final String USER = "root";
|
|
|
19 |
static final String PASS = "shop2020";
|
|
|
20 |
|
|
|
21 |
public static void addAffClicks(long userId, int storeId, String url, String subTag) {
|
|
|
22 |
Connection conn = null;
|
|
|
23 |
PreparedStatement stmt = null;
|
|
|
24 |
try {
|
|
|
25 |
Class.forName("com.mysql.jdbc.Driver");
|
|
|
26 |
conn = DriverManager.getConnection(String.format(DB_URL, Utils.getMysqlHost()), USER, PASS);
|
|
|
27 |
stmt = conn.prepareStatement("INSERT INTO aff_clicks (user_id, store_id, url, subtag, created) values (?, ?, ?, ?, ?)");
|
|
|
28 |
stmt.setLong(1, userId);
|
|
|
29 |
stmt.setInt(2, storeId);
|
|
|
30 |
stmt.setString(3, url);
|
|
|
31 |
stmt.setString(4, subTag);
|
|
|
32 |
stmt.setTimestamp(5, new Timestamp(Calendar.getInstance().getTime().getTime()));
|
|
|
33 |
stmt.executeUpdate();
|
|
|
34 |
} catch (SQLException se) {
|
|
|
35 |
// Handle errors for JDBC
|
|
|
36 |
se.printStackTrace();
|
|
|
37 |
} catch (Exception e) {
|
|
|
38 |
// Handle errors for Class.forName
|
|
|
39 |
e.printStackTrace();
|
|
|
40 |
} finally {
|
|
|
41 |
// finally block used to close resources
|
|
|
42 |
try {
|
|
|
43 |
if (stmt != null)
|
|
|
44 |
stmt.close();
|
|
|
45 |
} catch (SQLException se2) {
|
|
|
46 |
}// nothing we can do
|
|
|
47 |
try {
|
|
|
48 |
if (conn != null)
|
|
|
49 |
conn.close();
|
|
|
50 |
} catch (SQLException se) {
|
|
|
51 |
se.printStackTrace();
|
|
|
52 |
}// end finally try
|
|
|
53 |
}// end try
|
|
|
54 |
}// end main
|
|
|
55 |
|
|
|
56 |
public static void main (String[] args){
|
|
|
57 |
Mysql.addAffClicks(1, 1, "1", "1");
|
|
|
58 |
}
|
|
|
59 |
}// end FirstExample
|