用java实现sqlite查询
我正在从事轮班经理计划,该计划计算月薪等。 该程序基于SQLite数据库,可通过用户输入不断更新。 我的问题是,我如何使用java中的SQLite函数来检索信息,比如说在一个命令中检索月薪(我知道我可以使用“在date1和date2之间选择总和(提示)”,但如何在变量中获得函数结果?) 到目前为止,我已经创建了一个函数,它获取两个日期,检索这些日期之间的所有轮班工资,并使用ResultSet对其进行汇总 这是我的密码:用java实现sqlite查询,java,database,sqlite,Java,Database,Sqlite,我正在从事轮班经理计划,该计划计算月薪等。 该程序基于SQLite数据库,可通过用户输入不断更新。 我的问题是,我如何使用java中的SQLite函数来检索信息,比如说在一个命令中检索月薪(我知道我可以使用“在date1和date2之间选择总和(提示)”,但如何在变量中获得函数结果?) 到目前为止,我已经创建了一个函数,它获取两个日期,检索这些日期之间的所有轮班工资,并使用ResultSet对其进行汇总 这是我的密码: public static String tipsMade(String d
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;
}
我编辑了你的代码。
请注意,如果选择提示的总和,则列名会更改。您也只得到一行一列作为结果,因此您不应该再需要while循环了
提示的总和现在保存在变量sum
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 sum(tips) from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum = rs.getInt("sum(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;
}
我编辑了你的代码。
请注意,如果选择提示的总和,则列名会更改。您也只得到一行一列作为结果,因此您不应该再需要while循环了
提示的总和现在保存在变量sum
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 sum(tips) from shifts where fulldate between "+"'"+date1+"'"+"and " +"'"+date2+"'"+ ";");
while(rs.next()){
sum = rs.getInt("sum(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;
}
我只是想知道如何从sqlite函数中检索数据,正如您所看到的,代码已经编写好了,只是在尝试改进它。我只是想知道如何从sqlite函数中检索数据,正如您所看到的,代码已经编写好了,只是在尝试改进它。