import java.sql.*;

public class Jstore_connector {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/cs4010";
    //  Database credentials
    static final String USER = "cs4010";
    static final String PASS = "cs4010";

    public Jstore_connector(){
    }

    public static void main(String[] args) {

        String[] ans =(new Jstore_connector()).the_call(args);
        System.out.println(ans[0]);
    }
    public String[] the_call(String[] args1){
        String[] the_return={"good"};
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs=null;
        try {
            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
            //STEP 3: Open a connection
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            CallableStatement cstmt=null;
            String call_string="";
            int ver;
            int this_call=Integer.parseInt(args1[0]);
            // System.out.println(this_call);
            switch (this_call) {
            case 0:   call_string="select newer_version(\""+args1[1]+"\",\""+args1[2]+"\",\""+args1[3]+"\")"; 
                // System.out.println(call_string);
                break;  
            case 1:
                ver=Integer.parseInt(args1[2]);   
                call_string="select get_version(\""+args1[1]+"\","+ver+")"; 
                //System.out.println(call_string);
                break; 
            case 2:
                ver=Integer.parseInt(args1[2]);   
                call_string="select update_notes(\""+args1[1]+"\","+ver+",\""+args1[3]+"\")";
                // System.out.println(call_string);
                break;    
            default:
                break;
            }
            cstmt = conn.prepareCall( call_string); 
            //  System.out.println(call_string); 
            boolean hasMoreResultSets = cstmt.execute();


            if ( hasMoreResultSets ) {

                rs = cstmt.getResultSet();
                //STEP 5: Extract data from result set
                while (rs.next()) {

                    switch (this_call) {
                    case 0: the_return[0]  = new Integer(rs.getInt(1)).toString();
                        break;
                    case 1: the_return[0]=rs.getString(1);
                        break;
                    case 2: the_return[0]=rs.getString(1);
                        // System.out.println(the_return[0]);
                        break;
                    default:
                        the_return[0]="next call"; 

                    }
                }

            }
            //STEP 6: Clean-up environment
            if (rs!=null)  rs.close();
            cstmt.close();
            conn.close();
        } catch (SQLException se) {
            the_return[0]="SQLException";
            //System.out.println(the_return[0]);
        } catch (Exception e) {
            //Handle errors for Class.forName
            the_return[0]="Exception" ;
        } finally {
            //finally block used to close resources
            try {
                if (stmt!=null)
                    stmt.close();
            } catch (SQLException se2) {

                the_return[0]="SQLException1";
            }// nothing we can do
            try {
                if (conn!=null)
                    conn.close();
            } catch (SQLException se) {
                the_return[0]="SQLException2";

            }//end finally try
        }//end try
        return the_return; 
    }//end this_call
}//end Class