有两个表,EntryTable
和ExitTable
。两者都有相似的列:id、date、time
。当员工刷卡时,调用此函数。如果他那天已经打卡了,他的记录将被输入到导出表中。否则,它被输入到条目表中。
public int addEntryRecord(String id) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Attend","rt","rt");
Statement stmt = con.createStatement();
long millis=System.currentTimeMillis();
java.sql.Date date=new java.sql.Date(millis);
Date today = new Date();
Date currentTime = new java.sql.Time(today.getTime());
stmt.executeQuery("SELECT id from Attend.EntryTable WHERE EntryTable.id ='"+id+"'and
EntryTable.DayOf='"+date+"'");
ResultSet rs = stmt.getResultSet();
System.out.println(rs.next());
if(rs.next()== true){
String sql = "INSERT Attend.ExitTable VALUES('"+id+"','"+date+"','"+currentTime+" ')";
stmt.executeUpdate(sql);
System.out.println("Good job");
}
else{
String sql ="INSERT Attend.EntryTable VALUES('"+id+"','"+date+"','"+currentTime+"')";
stmt.executeUpdate(sql);
System.out.println("Entry noted");
}
}catch (Exception e) {
System.out.println(e);
}
return 0;
}
错误是记录总是进入ExitTable
,不知道为什么。
请帮忙。
最佳答案
你的代码有一些问题。
所有 ResultSet.next() 中的一个是不合理的。 如果你写:
System.out.println(rs.next());
对 rs.next() 的下一次调用可能是错误的(如果记录只有一个,就像您的情况一样)
如果你需要这样做,最好这样做:
boolean next = rs.next();
System.out.println( next );
if ( next ) {
然后我建议在需要替换参数时使用准备好的语句而不是语句:
另外,下次发布表架构时,时间比较有时会很棘手,您是否检查过您使用的是正确的日期类型? (例如,日期而不是时间戳?)无论如何,如果您使用 Prepared 语句,比较会更好。
我要补充一点,您从未关闭过资源(大多数情况下这是错误的)。
这是我在本地尝试并在我的系统上工作的一个简短示例:
/*
数据库架构:
创建数据库参加;
在出席时授予所有特权。* 由“Tester_2020”识别;
创建表条目表( id VARCHAR(100) 不为空, DayOf DATE 不为空, 时间不为空 );
创建表退出表( id VARCHAR(100) 不为空, DayOf DATE 不为空, 时间不为空 );
*/
/*
* Use an external method to access the connection, better in an external data source or factory.
*/
public Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/Attend", "rt", "Tester_2020");
}
/*
* Usually is better to use a logging facility (slf4j, log4j, java.util.logging etc.)
*/
public void log( String message ) {
System.out.println( message );
}
/*
* Better not to suppress exception usually
*/
public int addEntryRecordPS(String id) throws Exception {
// using try with resources to delegate closing connection.
try ( Connection con = getConnection() ) {
long millis = System.currentTimeMillis();
java.sql.Date date = new java.sql.Date(millis);
Date today = new Date();
Time currentTime = new java.sql.Time(today.getTime());
try ( PreparedStatement checkPstm = con.prepareStatement( "SELECT id from Attend.EntryTable WHERE EntryTable.id = ? and EntryTable.DayOf = ?") ) {
checkPstm.setString( 1 , id );
checkPstm.setDate( 2 , date );
try ( ResultSet rs = checkPstm.executeQuery() ) {
boolean next = rs.next();
log( "hasNext? : "+next );
if ( next ) {
// again try with resource (no need to close pstm)
try ( PreparedStatement pstm = con.prepareStatement( "INSERT INTO Attend.ExitTable ( id, DayOf, TimeOf ) VALUES ( ?, ? ,? ) " ) ) {
pstm.setString( 1 , id );
pstm.setDate( 2 , date );
pstm.setTime( 3 , currentTime );
int res = pstm.executeUpdate();
log("Good job "+res);
}
} else {
// again try with resource (no need to close pstm)
try ( PreparedStatement pstm = con.prepareStatement( "INSERT INTO Attend.EntryTable ( id, DayOf, TimeOf ) VALUES ( ?, ? ,? ) " ) ) {
pstm.setString( 1 , id );
pstm.setDate( 2 , date );
pstm.setTime( 3 , currentTime );
int res = pstm.executeUpdate();
log("Entry noted "+res);
}
}
}
}
}
return 0;
}
希望对您有所帮助。
关于java - 我正在用 java 和 mySQL 做一个出勤程序,但它对我来说效果不佳。这是代码 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642180/