Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.mobileapi.serving.services;

import in.shop2020.mobileapi.serving.utils.Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

public class Mysql {
        // JDBC driver name and database URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://%s/user";

        // Database credentials
        static final String USER = "root";
        static final String PASS = "shop2020";

        public static void addAffClicks(long userId, int storeId, String url, String subTag) {
                Connection conn = null;
                PreparedStatement stmt = null;
                try {
                        Class.forName("com.mysql.jdbc.Driver");
                        conn = DriverManager.getConnection(String.format(DB_URL, Utils.getMysqlHost()), USER, PASS);
                        stmt = conn.prepareStatement("INSERT INTO aff_clicks (user_id, store_id, url, subtag, created) values (?, ?, ?, ?, ?)");
                    stmt.setLong(1, userId);
                    stmt.setInt(2, storeId);
                    stmt.setString(3, url);
                    stmt.setString(4, subTag);
                    stmt.setTimestamp(5, new Timestamp(Calendar.getInstance().getTime().getTime()));
                    stmt.executeUpdate();
                } catch (SQLException se) {
                        // Handle errors for JDBC
                        se.printStackTrace();
                } catch (Exception e) {
                        // Handle errors for Class.forName
                        e.printStackTrace();
                } finally {
                        // finally block used to close resources
                        try {
                                if (stmt != null)
                                        stmt.close();
                        } catch (SQLException se2) {
                        }// nothing we can do
                        try {
                                if (conn != null)
                                        conn.close();
                        } catch (SQLException se) {
                                se.printStackTrace();
                        }// end finally try
                }// end try
        }// end main
        
        public static void main (String[] args){
                Mysql.addAffClicks(1, 1, "1", "1");
        }
}// end FirstExample