Skip to content

Commit

Permalink
Merge branch 'zzy-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaihuaiyi committed Dec 4, 2016
2 parents f51293e + 053d301 commit ae78a4e
Show file tree
Hide file tree
Showing 36 changed files with 2,348 additions and 960 deletions.
611 changes: 296 additions & 315 deletions .idea/dataSources/28179e24-55d2-4f4f-ac0e-48af0bcb62a2.xml

Large diffs are not rendered by default.

546 changes: 291 additions & 255 deletions .idea/dataSources/3ac11ac6-9623-45ae-9aee-51395197a1ca.xml

Large diffs are not rendered by default.

170 changes: 170 additions & 0 deletions src/dao/LogDao.java
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();
}
}
}
89 changes: 60 additions & 29 deletions src/dao/NoteDao.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dao;
import model.Log;
import model.Note;
import org.jetbrains.annotations.Nullable;

Expand All @@ -14,7 +15,7 @@ public class NoteDao
@Nullable
private Statement newDao()
{
if (stmt!=null)
if (stmt != null)
return stmt;
try
{
Expand Down Expand Up @@ -42,12 +43,12 @@ private int closeDao()
{
try
{
if (stmt!=null)
if (stmt != null)
stmt.close();
if (conn!=null)
if (conn != null)
conn.close();
stmt=null;
conn=null;
stmt = null;
conn = null;
return 1;
}
catch (SQLException e)
Expand All @@ -68,12 +69,12 @@ public Note getNoteById(int nid)
{
String sql = "SELECT * FROM note WHERE id=" + nid + ";";
stmt = newDao();
ResultSet rs=null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery(sql);
Note note = new Note();
if(rs.next())
if (rs.next())
{
note.setId(rs.getInt("id"));
UserDao userDao = new UserDao();
Expand All @@ -92,11 +93,15 @@ public Note getNoteById(int nid)
e.printStackTrace();
return null;
}
finally {
if (rs!=null)
try {
finally
{
if (rs != null)
try
{
rs.close();
} catch (SQLException e) {
}
catch (SQLException e)
{
e.printStackTrace();
}
closeDao();
Expand All @@ -105,9 +110,9 @@ public Note getNoteById(int nid)
public Collection<Note> getAllNotes()
{
Collection<Note> notes = new LinkedList<>();
String sql = "select * from note;";
String sql = "SELECT * FROM note;";
stmt = newDao();
ResultSet rs=null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery(sql);
Expand All @@ -132,11 +137,15 @@ public Collection<Note> getAllNotes()
e.printStackTrace();
return null;
}
finally {
if (rs!=null)
try {
finally
{
if (rs != null)
try
{
rs.close();
} catch (SQLException e) {
}
catch (SQLException e)
{
e.printStackTrace();
}
closeDao();
Expand All @@ -147,7 +156,7 @@ public Collection<Note> getNotesByUser(int uid)
{
String sql = "SELECT * FROM note WHERE author=" + uid + ";";
stmt = newDao();
ResultSet rs=null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery(sql);
Expand All @@ -174,11 +183,15 @@ public Collection<Note> getNotesByUser(int uid)
e.printStackTrace();
return null;
}
finally {
if (rs!=null)
try {
finally
{
if (rs != null)
try
{
rs.close();
} catch (SQLException e) {
}
catch (SQLException e)
{
e.printStackTrace();
}
closeDao();
Expand All @@ -188,7 +201,7 @@ public Collection<Note> getNotesByPaper(int pid)
{
String sql = "SELECT * FROM note WHERE paper=" + pid + ";";
stmt = newDao();
ResultSet rs=null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery(sql);
Expand All @@ -215,11 +228,15 @@ public Collection<Note> getNotesByPaper(int pid)
e.printStackTrace();
return null;
}
finally {
if (rs!=null)
try {
finally
{
if (rs != null)
try
{
rs.close();
} catch (SQLException e) {
}
catch (SQLException e)
{
e.printStackTrace();
}
closeDao();
Expand All @@ -233,15 +250,29 @@ public int insertNote(int authorID, int paperID, String title, String content, T
+ title + "','" + content + "','" + publishTime + "');";
try
{
return stmt.executeUpdate(sql);
int result = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
int id;
if (rs.next())
{
id = rs.getInt(1);
LogDao logDao = new LogDao();
if (logDao.insertLog(Log.ADD, Log.NOTE, id, authorID) > 0)
return result;
else
return -3;//写入日志失败
}
rs.close();
return -2;//其他未知错误
}
catch (SQLException e)
{
System.err.println("MySQL查询错误@dao.NoteDao.insertNote");
e.printStackTrace();
return -1;
}
finally {
finally
{
closeDao();
}
}
Expand Down
Loading

0 comments on commit ae78a4e

Please sign in to comment.