├── .gitignore ├── .vscode ├── settings.json └── launch.json ├── .settings ├── org.eclipse.jdt.apt.core.prefs ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── src └── main │ └── java │ └── bistu │ ├── share │ ├── Instruction.java │ ├── Overview.java │ └── Detail.java │ ├── server │ ├── MainServer.java │ └── Serve.java │ └── db │ └── DbUtil.java ├── .project ├── DbStructure └── createDb.sql ├── .classpath └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target/* 2 | /**/*.class 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.configuration.updateBuildConfiguration": "automatic" 3 | } -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.apt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.apt.aptEnabled=false 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /src/main/java/bistu/share/Instruction.java: -------------------------------------------------------------------------------- 1 | package bistu.share; 2 | 3 | public class Instruction { 4 | public static final int SERVE_END = 999; 5 | public static final int GET_LIST = 1; 6 | public static final int GET_DETAIL = 2; 7 | public static final int UPDATE_STICKY = 3; 8 | public static final int REMOVE_STICKY = 4; 9 | public static final int ADD_STICKY = 5; 10 | 11 | public static final int ERR_CODE = -1; 12 | public static final int FINE_CODE = 0; 13 | } -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 3 | org.eclipse.jdt.core.compiler.compliance=11 4 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore 7 | org.eclipse.jdt.core.compiler.processAnnotations=disabled 8 | org.eclipse.jdt.core.compiler.release=disabled 9 | org.eclipse.jdt.core.compiler.source=11 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sticky-server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /DbStructure/createDb.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE db_sticky_android; 2 | 3 | DROP TABLE IF EXISTS `sticky`; 4 | 5 | CREATE TABLE `sticky` ( 6 | `c_id` int(11) NOT NULL AUTO_INCREMENT, 7 | `c_modify` datetime NOT NULL, 8 | `c_overview` varchar(300) DEFAULT NULL, 9 | `c_full_text` mediumtext DEFAULT NULL, 10 | PRIMARY KEY (`c_id`) 11 | ); 12 | 13 | CREATE TRIGGER t_modify_overview_insert 14 | BEFORE INSERT ON db_sticky_android.sticky FOR EACH ROW 15 | SET NEW.c_overview = SUBSTRING(NEW.c_full_text, 1, 300) 16 | 17 | CREATE TRIGGER t_modify_overview_update 18 | BEFORE UPDATE ON db_sticky_android.sticky FOR EACH ROW 19 | SET NEW.c_overview = SUBSTRING(NEW.c_full_text, 1, 300); -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "java", 9 | "name": "Debug (Launch) - Current File", 10 | "request": "launch", 11 | "mainClass": "${file}" 12 | }, 13 | { 14 | "type": "java", 15 | "name": "Debug (Launch)-App", 16 | "request": "launch", 17 | "mainClass": "bistu.App", 18 | "projectName": "sticky-server" 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /src/main/java/bistu/share/Overview.java: -------------------------------------------------------------------------------- 1 | package bistu.share; 2 | 3 | import java.io.Serializable; 4 | import java.sql.Timestamp; 5 | 6 | public class Overview implements Serializable { 7 | private long id; 8 | private Timestamp modifyTime; 9 | private String summary; 10 | 11 | public Overview(long id, Timestamp modifyTime, String summary) { 12 | this.id = id; 13 | this.modifyTime = modifyTime; 14 | this.summary = summary; 15 | } 16 | 17 | public long getId() { return id; } 18 | public void setId(long id) { this.id = id; } 19 | 20 | public Timestamp getModifyTime() { return modifyTime; } 21 | public void setModifyTime(Timestamp modifyTime) { this.modifyTime = modifyTime; } 22 | 23 | public String getSummary() { return summary; } 24 | public void setSummary(String summary) { this.summary = summary; } 25 | 26 | @Override 27 | public String toString() { 28 | return String.format("Overview: id=%ld, modifyTime=%s, summary=%s", id, modifyTime.toString(), summary); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /src/main/java/bistu/share/Detail.java: -------------------------------------------------------------------------------- 1 | package bistu.share; 2 | 3 | import java.io.Serializable; 4 | import java.sql.Timestamp; 5 | 6 | public class Detail implements Serializable { 7 | private long id; 8 | private Timestamp modifyTime; 9 | private String fullText; 10 | 11 | public Detail(long id, Timestamp modifyTime, String fullText) { 12 | this.id = id; 13 | this.modifyTime = modifyTime; 14 | this.fullText = fullText; 15 | } 16 | 17 | public long getId() { return id; } 18 | public void setId(long id) { this.id = id; } 19 | 20 | public Timestamp getModifyTime() { return modifyTime; } 21 | public void setModifyTime(Timestamp modifyTime) { this.modifyTime = modifyTime; } 22 | 23 | public String getFullText() { return fullText; } 24 | public void setFullText(String fullText) { this.fullText = fullText; } 25 | 26 | @Override 27 | public String toString() { 28 | return String.format("Detail: id=%d, modifyTime=%s, fullText=%s", this.id, modifyTime.toString(), fullText); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/main/java/bistu/server/MainServer.java: -------------------------------------------------------------------------------- 1 | package bistu.server; 2 | 3 | import java.io.IOException; 4 | import java.util.logging.Level; 5 | import java.util.logging.Logger; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | 9 | import bistu.db.DbUtil; 10 | 11 | public class MainServer { 12 | 13 | private static final String LOGGER_NAME = "sticky_android server"; 14 | private static final int SERVE_PORT = 12345; 15 | 16 | private Logger logger; 17 | private ServerSocket ssocket; 18 | 19 | private Throwable loadJdbcDriver() { 20 | Throwable t = null; 21 | try { 22 | Class.forName("org.mariadb.jdbc.Driver"); 23 | } catch (ClassNotFoundException e) { 24 | t = e; 25 | } 26 | return t; 27 | } 28 | 29 | private Throwable initServerSocket() { 30 | Throwable t = null; 31 | try { 32 | this.ssocket = new ServerSocket(SERVE_PORT); 33 | logger.log(Level.INFO, String.format("Listening port: %d", SERVE_PORT)); 34 | } catch (IOException e) { 35 | t = e; 36 | } 37 | return t; 38 | } 39 | 40 | private void serve() { 41 | while (true) { 42 | try { 43 | logger.log(Level.INFO, "waitting for user connecting..."); 44 | Socket socket = this.ssocket.accept(); 45 | new Thread(new Serve(socket)).start(); 46 | } catch (IOException e) { 47 | logger.log(Level.SEVERE, "there is a connection failed, relistening the same port.", e); 48 | } 49 | } 50 | } 51 | 52 | private void initServer() { 53 | this.logger = Logger.getLogger(LOGGER_NAME); 54 | logger.log(Level.INFO, "inited the logger."); 55 | 56 | Throwable loadJdbcDriverThrow = this.loadJdbcDriver(); 57 | if (loadJdbcDriverThrow == null) { 58 | logger.log(Level.INFO, "load the jdbc driver."); 59 | } else { 60 | logger.log(Level.SEVERE, "load jdbc drive error!", loadJdbcDriverThrow); 61 | System.exit(-1); 62 | } 63 | 64 | logger.log(Level.INFO, "initializing DbUtil..."); 65 | DbUtil.getInstance(); 66 | logger.log(Level.INFO, "initialize DbUtil succeed."); 67 | 68 | Throwable initServerSocketThrow = this.initServerSocket(); 69 | if (initServerSocketThrow == null) { 70 | logger.log(Level.INFO, "inited the server socket."); 71 | } else { 72 | logger.log(Level.SEVERE, "init server socket error!", initServerSocketThrow); 73 | System.exit(-1); 74 | } 75 | 76 | logger.log(Level.INFO, "server start successfully."); 77 | logger.log(Level.INFO, "start listen from user."); 78 | this.serve(); 79 | 80 | } 81 | 82 | public static void main(String[] args) { 83 | MainServer s = new MainServer(); 84 | s.initServer(); 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | bistu 8 | sticky-server 9 | 1.0-SNAPSHOT 10 | 11 | sticky-server 12 | 13 | http://www.example.com 14 | 15 | 16 | UTF-8 17 | 11 18 | 11 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.13.1 26 | test 27 | 28 | 29 | 30 | 31 | org.mariadb.jdbc 32 | mariadb-java-client 33 | 2.5.2 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | maven-clean-plugin 43 | 3.1.0 44 | 45 | 46 | 47 | maven-resources-plugin 48 | 3.0.2 49 | 50 | 51 | maven-compiler-plugin 52 | 3.8.0 53 | 54 | 55 | maven-surefire-plugin 56 | 2.22.1 57 | 58 | 59 | maven-jar-plugin 60 | 3.0.2 61 | 62 | 63 | maven-install-plugin 64 | 2.5.2 65 | 66 | 67 | maven-deploy-plugin 68 | 2.8.2 69 | 70 | 71 | 72 | maven-site-plugin 73 | 3.7.1 74 | 75 | 76 | maven-project-info-reports-plugin 77 | 3.0.0 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /src/main/java/bistu/server/Serve.java: -------------------------------------------------------------------------------- 1 | package bistu.server; 2 | 3 | import java.io.ObjectInputStream; 4 | import java.io.ObjectOutputStream; 5 | import java.io.IOException; 6 | import java.net.Socket; 7 | import java.util.logging.Logger; 8 | import java.util.logging.Level; 9 | import java.util.List; 10 | 11 | import bistu.share.Detail; 12 | import bistu.share.Overview; 13 | import bistu.share.Instruction; 14 | import bistu.db.DbUtil; 15 | 16 | public class Serve implements Runnable { 17 | 18 | private Socket socket; 19 | private DbUtil db; 20 | private String logName; 21 | private Logger logger; 22 | 23 | private ObjectInputStream objectIn; 24 | private ObjectOutputStream objectOut; 25 | 26 | private boolean fine = true; 27 | 28 | Serve(Socket socket) { 29 | this.socket = socket; 30 | int port = socket.getPort(); 31 | String addr = socket.getInetAddress().getHostAddress(); 32 | this.logName = String.format("Serve for %s:%d", addr, port); 33 | this.logger = Logger.getLogger(this.logName); 34 | this.db = DbUtil.getInstance(); 35 | } 36 | 37 | private void serve_SERVE_END() throws IOException { 38 | socket.close(); 39 | this.fine = false; 40 | logger.log(Level.INFO, "received SERVE_END sign, finish service."); 41 | } 42 | 43 | private void serve_GET_LIST() throws IOException { 44 | List stickies = db.getList(); 45 | if (stickies != null) { 46 | objectOut.writeInt(Instruction.FINE_CODE); 47 | objectOut.writeObject(stickies); 48 | } else { 49 | objectOut.writeInt(Instruction.ERR_CODE); 50 | } 51 | objectOut.flush(); 52 | } 53 | 54 | private void serve_GET_DETAIL() throws IOException { 55 | long id = objectIn.readLong(); 56 | Detail detail = db.getDetail(id); 57 | if (detail != null) { 58 | objectOut.writeInt(Instruction.FINE_CODE); 59 | objectOut.writeObject(detail); 60 | } else { 61 | objectOut.writeInt(Instruction.ERR_CODE); 62 | } 63 | objectOut.flush(); 64 | } 65 | 66 | private void serve_ADD_STICKY() throws IOException { 67 | long id = db.insertSticky(); 68 | if (id > 0) { 69 | objectOut.writeInt(Instruction.FINE_CODE); 70 | objectOut.writeLong(id); 71 | } else { 72 | objectOut.writeInt(Instruction.ERR_CODE); 73 | } 74 | objectOut.flush(); 75 | } 76 | 77 | private void serve_UPDATE_STICKY() throws IOException { 78 | try { 79 | Detail d = (Detail)objectIn.readObject(); 80 | if (db.updateSticky(d)) { 81 | objectOut.writeInt(Instruction.FINE_CODE); 82 | } else { 83 | objectOut.writeInt(Instruction.ERR_CODE); 84 | } 85 | objectOut.flush(); 86 | logger.log(Level.INFO, String.format("update: %s", d.toString())); 87 | } catch (ClassNotFoundException e) { 88 | logger.log(Level.SEVERE, "the class Detail not found while reading updated sticky.", e); 89 | } 90 | } 91 | 92 | private void serve_REMOVE_STICKY() throws IOException { 93 | long id = objectIn.readLong(); 94 | if (db.removeSticky(id)) { 95 | objectOut.writeInt(Instruction.FINE_CODE); 96 | } else { 97 | objectOut.writeInt(Instruction.ERR_CODE); 98 | } 99 | objectOut.flush(); 100 | } 101 | 102 | private void serve() { 103 | while (this.fine) { 104 | try { 105 | switch (objectIn.readInt()) { 106 | case Instruction.SERVE_END: 107 | this.serve_SERVE_END(); 108 | break; 109 | 110 | case Instruction.GET_LIST: 111 | this.serve_GET_LIST(); 112 | break; 113 | 114 | case Instruction.GET_DETAIL: 115 | this.serve_GET_DETAIL(); 116 | break; 117 | 118 | case Instruction.ADD_STICKY: 119 | this.serve_ADD_STICKY(); 120 | break; 121 | 122 | case Instruction.UPDATE_STICKY: 123 | this.serve_UPDATE_STICKY(); 124 | break; 125 | 126 | case Instruction.REMOVE_STICKY: 127 | this.serve_REMOVE_STICKY(); 128 | break; 129 | } 130 | } catch (IOException e) { 131 | logger.log(Level.SEVERE, "error while reading/writing socket. shutting this serve.", e); 132 | this.fine = false; 133 | } 134 | } 135 | } 136 | 137 | @Override 138 | public void run() { 139 | logger.log(Level.INFO, "a user connected."); 140 | 141 | try { 142 | this.objectIn = new ObjectInputStream(socket.getInputStream()); 143 | this.objectOut = new ObjectOutputStream(socket.getOutputStream()); 144 | logger.log(Level.INFO, "inited the object stream."); 145 | } catch (IOException e) { 146 | logger.log(Level.SEVERE, "error occurred while initializing object stream.", e); 147 | this.fine = false; 148 | } 149 | 150 | this.serve(); 151 | } 152 | 153 | } -------------------------------------------------------------------------------- /src/main/java/bistu/db/DbUtil.java: -------------------------------------------------------------------------------- 1 | package bistu.db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.logging.Logger; 11 | import java.util.logging.Level; 12 | 13 | import bistu.share.Detail; 14 | import bistu.share.Overview; 15 | 16 | public class DbUtil { 17 | 18 | private static final String URL = "jdbc:mariadb://[::]:3306/db_sticky_android"; 19 | private static final String USER = "sticky_android"; 20 | private static final String PASSWD = "android"; 21 | private static final String LogName = "database util"; 22 | 23 | public static final String TABLE_NAME = "sticky"; 24 | public static final String COL_ID = "c_id"; 25 | public static final String COL_MODIFY = "c_modify"; 26 | public static final String COL_OVERVIEW = "c_overview"; 27 | public static final String COL_FULL_TEXT = "c_full_text"; 28 | 29 | private static DbUtil db; 30 | 31 | private Connection connection; 32 | private PreparedStatement s_selectList; 33 | private PreparedStatement s_selectSticky; 34 | private PreparedStatement s_updateSticky; 35 | private PreparedStatement s_removeSticky; 36 | private PreparedStatement s_insertSticky; 37 | private PreparedStatement s_lastInsertId; 38 | private Logger logger; 39 | 40 | private DbUtil() { 41 | try { 42 | this.logger = Logger.getLogger(LogName); 43 | this.connection = DriverManager.getConnection(URL, USER, PASSWD); 44 | logger.log(Level.INFO, "connected to database."); 45 | } catch (SQLException e) { 46 | logger.log(Level.SEVERE, "connect to database failed!", e); 47 | System.exit(-1); 48 | } 49 | 50 | try { 51 | this.s_selectList = connection.prepareStatement(String.format("select %s, %s, %s from %s;", COL_ID, COL_MODIFY, COL_OVERVIEW, TABLE_NAME)); 52 | this.s_selectSticky = connection.prepareStatement(String.format("select * from %s where %s=?;", TABLE_NAME, COL_ID)); 53 | this.s_updateSticky = connection.prepareStatement(String.format("update %s set %s=?, %s=? where %s=?", TABLE_NAME, COL_MODIFY, COL_FULL_TEXT, COL_ID)); 54 | this.s_removeSticky = connection.prepareStatement(String.format("delete from %s where %s=?", TABLE_NAME, COL_ID)); 55 | this.s_insertSticky = connection.prepareStatement(String.format("insert into %s (%s) values (now());", TABLE_NAME, COL_MODIFY)); 56 | this.s_lastInsertId = connection.prepareStatement("select last_insert_id()"); 57 | logger.log(Level.INFO, "prepared statement."); 58 | } catch (SQLException e) { 59 | logger.log(Level.SEVERE, "preparing statement error!", e); 60 | System.exit(-1); 61 | } 62 | } 63 | 64 | public static DbUtil getInstance() { 65 | if (db == null) { 66 | db = new DbUtil(); 67 | return db; 68 | } else { 69 | return db; 70 | } 71 | } 72 | 73 | /** 74 | * get id, modify time, overview from table 75 | * @return return a List, null if error occurred. 76 | */ 77 | public List getList() { 78 | ResultSet rs = null; 79 | try { 80 | rs = s_selectList.executeQuery(); 81 | logger.log(Level.INFO, "selected full list from table."); 82 | } catch (SQLException e) { 83 | logger.log(Level.SEVERE, "error occurred while selecting sticky full list!", e); 84 | return null; 85 | } 86 | 87 | ArrayList result = new ArrayList<>(); 88 | try { 89 | while (rs.next()) { 90 | Overview o = new Overview( 91 | rs.getLong(COL_ID), 92 | rs.getTimestamp(COL_MODIFY), 93 | rs.getString(COL_OVERVIEW) 94 | ); 95 | result.add(o); 96 | } 97 | logger.log(Level.INFO, String.format("retrieved %d stickies from table.", result.size())); 98 | } catch (SQLException e) { 99 | logger.log(Level.SEVERE, "error occurred while retrieving data from resultSet!", e); 100 | return null; 101 | } 102 | return result; 103 | } 104 | 105 | /** 106 | * get detail of a sticky. 107 | * @param id the required sticky's id 108 | * @return Detail of sticky, null if error occurred. 109 | */ 110 | public Detail getDetail(long id) { 111 | ResultSet rs = null; 112 | try { 113 | s_selectSticky.setLong(1, id); 114 | rs = s_selectSticky.executeQuery(); 115 | s_selectSticky.clearParameters(); 116 | } catch (SQLException e) { 117 | logger.log(Level.SEVERE, "error occurred while querying detail.", e); 118 | return null; 119 | } 120 | 121 | Detail d = null; 122 | try { 123 | if (rs.next()) { 124 | d = new Detail( 125 | rs.getLong(COL_ID), 126 | rs.getTimestamp(COL_MODIFY), 127 | rs.getString(COL_FULL_TEXT) 128 | ); 129 | logger.log(Level.INFO, String.format("detail got: %s.", d.toString())); 130 | } else { 131 | logger.log(Level.SEVERE, String.format("did not find the sticky with id: %d", id)); 132 | } 133 | } catch (SQLException e) { 134 | logger.log(Level.SEVERE, "error occurred while retrieving data from resultSet.", e); 135 | return null; 136 | } 137 | return d; 138 | } 139 | 140 | /** 141 | * update a sticky. 142 | * @param sticky the new sticky, should has the same id of previous sticky. 143 | * @return true if update succeed, false if update fail or error occurred. 144 | */ 145 | public boolean updateSticky(Detail sticky) { 146 | boolean result = false; 147 | try { 148 | s_updateSticky.setTimestamp(1, sticky.getModifyTime()); 149 | s_updateSticky.setString(2, sticky.getFullText()); 150 | s_updateSticky.setLong(3, sticky.getId()); 151 | result = s_updateSticky.executeUpdate() > 0; 152 | s_updateSticky.clearParameters(); 153 | logger.log(Level.INFO, "sticky updated."); 154 | } catch (SQLException e) { 155 | logger.log(Level.SEVERE, "error occurred while updating sticky.", e); 156 | } 157 | return result; 158 | } 159 | 160 | /** 161 | * @param id the id of sticky to be removed 162 | * @return true if remove succeed, false if fail or error occurred. 163 | */ 164 | public boolean removeSticky(long id) { 165 | boolean result = false; 166 | try { 167 | s_removeSticky.setLong(1, id); 168 | result = s_removeSticky.executeUpdate() > 0; 169 | logger.log(Level.INFO, result ? "remove sticky succeed.": "removed nothing."); 170 | } catch (SQLException e) { 171 | logger.log(Level.SEVERE, "error occurred while removing sticky.", e); 172 | } 173 | return result; 174 | } 175 | 176 | /** 177 | * will create a blank sticky in database. the modify time of sticky depends on when 178 | * the sql be run on server. 179 | * @return sticky's id if succeed, -1 if create fail. 180 | */ 181 | public long insertSticky() { 182 | long id = -1; 183 | try { 184 | if (s_insertSticky.executeUpdate() > 0) { 185 | ResultSet idInserted = s_lastInsertId.executeQuery(); 186 | idInserted.next(); 187 | id = idInserted.getLong(1); 188 | logger.log(Level.INFO, String.format("inserted id=%d.", id)); 189 | } 190 | } catch (SQLException e) { 191 | logger.log(Level.SEVERE, "error occurred while inserting or getting id."); 192 | } 193 | return id; 194 | } 195 | 196 | } --------------------------------------------------------------------------------