我嘗試選擇用戶第一次登錄平臺。每個用戶可以在一天內多次進入該平臺,並且在第一次選擇時遇到問題。每個用戶每天選擇第一個登錄時間
數據庫訪問類:
package poin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DataBase {
Connection conn ;
Statement stmt;
List<Join> list = new ArrayList<Join>();
public DataBase(){
super();
}
public List<Join> getCard() throws Exception {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;user=****;password=*******;database=********");
stmt = conn.createStatement();
String sql = "SELECT [Id] ,[Last Name] ,[First Name],[Field Time] FROM [******].[dbo].[Jointur] order by [Field Time]";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
//rs.beforeFirst();
//rs.next();
//Join obj =new Join(); FAUTE CAR NOUS AVONS CR2ER UN SEUL OBJET POUR TOUTE ARRAY LIST IL FAUT ETRE A L INTERIEUR DE LA BOUCLE VOIR LA BOUCLE
while(rs.next())
{
Join obj =new Join();//
//Retrieve by column name
obj.setCardHolderId(rs.getInt("ID"));
obj.setNom(rs.getString("Last Name"));
obj.setPrenom(rs.getString("First Name"));
obj.setFieldTime(rs.getDate("Field Time"));
obj.setTimestamp(rs.getTimestamp("Fiel Time"));
list.add(obj);
}
rs.close();
stmt.close();
conn.close();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
加入類;這個類包含插入ArrayList中對象的類型:
package poin;
import java.sql.Timestamp;
import java.util.Date;
public class Join {
private int cardHolderId;
private String Nom;
private String Prenom;
private Date fieldTime;
private Timestamp timestamp;
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public Join() {
super();
}
public int getCardHolderId() {
return cardHolderId;
}
public void setCardHolderId(int cardHolderId) {
this.cardHolderId = cardHolderId;
}
public String getNom() {
return Nom;
}
public void setNom(String nom) {
Nom = nom;
}
public String getPrenom() {
return Prenom;
}
public void setPrenom(String prenom) {
Prenom = prenom;
}
public Date getFieldTime() {
return fieldTime;
}
public void setFieldTime(Date fieldTime) {
this.fieldTime = fieldTime;
}
}
類測試:類包含主
package poin;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
public class Test {
static Date dateDebut;
static Date dateFin;
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
String dateEntre = "2016-03-03";
String dateF = "2016-03-03";
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
dateDebut= ft.parse(dateEntre);
dateFin=ft.parse(dateF);
// TODO Auto-generated method stub
DataBase obj = new DataBase();
List<Join> list = new ArrayList<Join>();
List<Join> result = new ArrayList<Join>();
try {
list=obj.getCard();
//iterateur pour la liste extraite de la base de donnée
Iterator<Join> it=result.iterator();
//gregorian calendrier
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(dateDebut);
//dernier element de la liste: base de donnée
Join obj2 = list.get(list.size()-1);
// dernier identifiant de la carte dans la table
int dernId = obj2.getCardHolderId();
result.add(list.get(0));
while(!cal.getTime().after(dateFin)){
for (Join j : list){
//System.out.println(cal.getTime());
//Join o = it.next();
//boolean o2 = result.add(o);
if(j.getFieldTime().compareTo(cal.getTime())==0){
//ICI LE PROBLEME (translated: HERE IS THE PROBLEM)
for (Join i : result){
if (i.getCardHolderId() != j.getCardHolderId() && i.getFieldTime() != j.getFieldTime()) result.add(j);
}
}
}
cal.add(Calendar.DAY_OF_WEEK, 1);
}
for(Join k : list){
System.out.println(k.getCardHolderId()+" "+k.getNom());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
請描述你的問題。 –
每天,對於一個人,我需要選擇他第一次登錄。每個人可以有多個時間登錄 –
您有什麼問題與第一次選擇?你遺棄了很多代碼(很多可能與問題無關的代碼),並期望我們從中提煉出真正的問題。 –