-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
2,348 additions
and
960 deletions.
There are no files selected for viewing
611 changes: 296 additions & 315 deletions
611
.idea/dataSources/28179e24-55d2-4f4f-ac0e-48af0bcb62a2.xml
Large diffs are not rendered by default.
Oops, something went wrong.
546 changes: 291 additions & 255 deletions
546
.idea/dataSources/3ac11ac6-9623-45ae-9aee-51395197a1ca.xml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package dao; | ||
|
||
import model.Log; | ||
|
||
import java.sql.*; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
public class LogDao | ||
{ | ||
private Statement stmt; | ||
private Connection conn; | ||
|
||
private Statement newDao() | ||
{ | ||
if (stmt != null) | ||
return stmt; | ||
try | ||
{ | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/papermanage", "root", "coding"); | ||
stmt = conn.createStatement(); | ||
return stmt; | ||
} | ||
catch (SQLException e) | ||
{ | ||
System.err.println("MySQL连接错误@dao.Dao"); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
catch (Exception e) | ||
{ | ||
System.err.println("MySQL驱动程序错误@dao.Dao"); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private int closeDao() | ||
{ | ||
try | ||
{ | ||
if (stmt != null) | ||
stmt.close(); | ||
if (conn != null) | ||
conn.close(); | ||
stmt = null; | ||
conn = null; | ||
return 1; | ||
} | ||
catch (SQLException e) | ||
{ | ||
System.err.println("MySQL连接错误@dao.Dao.closeDao"); | ||
e.printStackTrace(); | ||
return -1; | ||
} | ||
catch (Exception e) | ||
{ | ||
System.err.println("MySQL驱动程序错误@dao.Dao.closeDao"); | ||
e.printStackTrace(); | ||
return -2; | ||
} | ||
} | ||
public Collection<Log> getAllLogs() | ||
{ | ||
Collection<Log> logs = new LinkedList<>(); | ||
String sql = "SELECT * FROM log;"; | ||
stmt = newDao(); | ||
ResultSet rs = null; | ||
try | ||
{ | ||
rs = stmt.executeQuery(sql); | ||
Log log; | ||
while (rs.next()) | ||
{ | ||
log = new Log(); | ||
log.setId(rs.getInt("id")); | ||
log.setOperatorid(rs.getInt("operatorid")); | ||
log.setTarget(rs.getInt("target")); | ||
log.setTargetid(rs.getInt("targetid")); | ||
log.setTime(rs.getTimestamp("time")); | ||
log.setType(rs.getInt("type")); | ||
logs.add(log); | ||
} | ||
return logs; | ||
} | ||
catch (SQLException e) | ||
{ | ||
System.err.println("MySQL查询错误@dao.LogDao.getAllLogs"); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
finally | ||
{ | ||
if (rs != null) | ||
try | ||
{ | ||
rs.close(); | ||
} | ||
catch (SQLException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
closeDao(); | ||
} | ||
} | ||
public Collection<Log> getLogsByUser(int uid) | ||
{ | ||
Collection<Log> logs = new LinkedList<>(); | ||
String sql = "select * from log WHERE operatorid='" + uid + "';"; | ||
stmt = newDao(); | ||
ResultSet rs = null; | ||
try | ||
{ | ||
rs = stmt.executeQuery(sql); | ||
Log log; | ||
while (rs.next()) | ||
{ | ||
log = new Log(); | ||
log.setId(rs.getInt("id")); | ||
log.setOperatorid(rs.getInt("operatorid")); | ||
log.setTarget(rs.getInt("target")); | ||
log.setTargetid(rs.getInt("targetid")); | ||
log.setTime(rs.getTimestamp("time")); | ||
log.setType(rs.getInt("type")); | ||
logs.add(log); | ||
} | ||
} | ||
catch (SQLException e) | ||
{ | ||
System.err.println("MySQL查询错误@dao.LogDao.getLogsByUser"); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
finally | ||
{ | ||
if (rs != null) | ||
try | ||
{ | ||
rs.close(); | ||
} | ||
catch (SQLException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
closeDao(); | ||
} | ||
return logs; | ||
} | ||
public int insertLog(int type, int target, int targetid, int operatorid) | ||
{ | ||
stmt = newDao(); | ||
Timestamp time = new Timestamp(System.currentTimeMillis()); | ||
String sql = "INSERT INTO log(time, type, target, targetid, operatorid) " + | ||
"VALUES ('" + time + "','" + type + "','" + target + "','" + targetid + "','" + operatorid + "');"; | ||
try | ||
{ | ||
return stmt.executeUpdate(sql); | ||
} | ||
catch (SQLException e) | ||
{ | ||
System.err.println("MySQL查询错误@dao.LogDao.insertLog"); | ||
e.printStackTrace(); | ||
return -1; | ||
} | ||
finally | ||
{ | ||
closeDao(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.