dimanche 21 février 2016

sqlite query using java

I'm working on shifts manager program which calculates monthly salary and etc. the program based on SQLite database which keeps getting updated by the user input. my question is , how can i use the SQLite function in java to retrieve information, lets say monthly salary in one command (i know i can use " select sum(tips) between date1 and date2",but how can i get the function result inside a variable?) so far i've created a function which gets two dates and retrieves all the shifts salary between these dates and summarise them with ResultSet.

here's my code:

public static String tipsMade(String date1, String date2){
    Connection c = null;
    Statement stmt = null;
    ResultSet rs = null;
    String ans= null;
    int sum = 0;
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gil\\test.db");
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");
        stmt = c.createStatement();
        rs = stmt.executeQuery("select tips from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
        while(rs.next()){
            sum += rs.getInt("tips");
        }
        ans = Integer.toString(sum);
        //

        //close connections and etc
        stmt.close();
        c.commit();
        c.close();

    } catch ( Exception e ) {
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        System.exit(0);
    }
    return ans;
    }

Aucun commentaire:

Enregistrer un commentaire