The following code is the key to the use of the database to achieve RMI meeting management in this experiment. The main method is to implement database connection and implement related query methods. These methods have been stated in the previous article's interface code, but there is no substance.The RmiDataBase class inherits the meetinginterface interface.
Series links:
SSD8_Ex3 [JAVA_RMI Service] (1) Overview of RMI and Network APIs
SSD8_Ex3[JAVA_RMI](2) Remote Interface Declaration
SSD8_Ex3 [JAVA_RMI] (3) Open RMI Service
SSD8_Ex3 [JAVA_RMI] (4) conference database table
SSD8_Ex3 [JAVA_RMI] (5) database connection and meeting method definition
SSD8_Ex3 [JAVA_RMI] (6) Create Command Line Navigation Menu
RmiDataBase.java
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
Package cn.wangbaiyuan; Import java.io.BufferedReader; Import java.io.IOException; Import java.io.InputStream; Import java.io.InputStreamReader; Import java.rmi.RemoteException; Import java.rmi.server.UnicastRemoteObject; Import java.sql.*; Import cn.wangbaiyuan.tools.TimeTool; Import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException; /** * Connect the meeting database class and define related query methods * * @author Wang Baiyuan * */ Public class RmiDataBase extends UnicastRemoteObject implements MeetingInterface {{ Private Connection connection; Private String userName; Private String password; Private String connectionUrl; /** * * @param dhost * CPU name * @param dport * Port * @param dName * Name database * @param duserName * username * @param dpassword * Password * @throws SQLException * throws SQLException */ Public RmiDataBase(String dhost, int dport, String dbName, String duserName, String dpassword) throws SQLException, RemoteException { userName = duserName; Password = dpassword; // Set the encoding of the database connection connectionUrl = "jdbc:mysql://" + dhost + ":" + dport + "/" + dbName+"?useUnicode=true&characterEncoding=utf8"; getConnection(); } /** * Connect to the database when the database is not connected * * @return database connection * @throws SQLException */ Public Connection getConnection() throws SQLException { System.out.println(connectionUrl); Connection = DriverManager.getConnection(connectionUrl, userName, Password); Return connection; } /** * Add user, user registration * * @param user * @throws MySQLIntegrityConstraintViolationException * @throws BYException * @throws SQLException */ Public void addMeetingUsers(String userName,String userpassword) throws BYException, SQLException { Try { Statement sql = getConnection().createStatement(); String name =userName; String passWard = userpassword; String addString = "INSERT INTO users (userName, userPassword) VALUES ('" + name + "', '" + passWard + "')"; System.out.println(addString); Sql.execute(addString); Connection.close(); Throw new BYException("Add success!"); } Catch (MySQLIntegrityConstraintViolationException e) { Throw new BYException("The current username already exists"); } } /** * * @param user * @throws RemoteException * @throws Exception */ Public void addMeetings(String userName, String password, String otherUserName,String imeetingTitle,String imeetingStartTime,String imeetingEndTime) throws BYException, RemoteException { Try { imeetingStartTime=TimeTool.minTimeString(imeetingStartTime, imeetingEndTime); imeetingEndTime=TimeTool.maxTimeString(imeetingStartTime, imeetingEndTime); Statement sql = getConnection().createStatement(); If (LoginMeetings(userName, password)&& otherUserName != null) { String searchString = "select * from users where userName ='" + otherUserName + "';"; ResultSet rs = sql.executeQuery(searchString); System.out.println(searchString); Rs.last(); If (rs.getRow() < 1) {System.err.println("You invited it" + otherUserName + "The user does not exist!"); Throw new BYException("You invited the name "" + otherUserName + "" user does not exist!");} Else { searchString="SELECT r1.userNamen" + " FROM meetingrecord r1,meeting m1n" + " WHERE (r1.meetingId=m1.meetingId)" + " and (r1.userName='"+userName+"' or r1.userName='"+otherUserName+"')" + "and ((m1.startTime<='"+imeetingStartTime+"' and m1.endTime>='"+imeetingStartTime+"')" + " or (m1.startTime<='"+imeetingEndTime+"' and m1.endTime>='"+imeetingEndTime+"')" + " or(m1.startTime>='"+imeetingStartTime+"' and m1.endTime<='"+imeetingEndTime+"'));"; System.out.println(searchString); Sql = getConnection().createStatement(); Rs = sql.executeQuery(searchString); // rs.getRow(); Rs.last(); If (rs.getRow() >=1) Throw new BYException ("Added meeting and existing meeting conflict in time!"); Else{ String addString = "INSERT INTO meeting(founderUserName,meetingTitle, startTime, endTime) " + "VALUES (?,?,?,?);"; PreparedStatement pstmt = (PreparedStatement) getConnection().prepareStatement(addString,Statement.RETURN_GENERATED_KEYS); pstmt.setString(1,userName ); pstmt.setString(2, imeetingTitle); pstmt.setString(3, imeetingStartTime); pstmt.setString(4, imeetingEndTime); pstmt.executeUpdate();//Execute sql ResultSet rsadd = pstmt.getGeneratedKeys(); //Get Results Rsadd.next(); Int id = rsadd.getInt(1);//Get the incremented id value addString = "INSERT INTO meetingrecord(meetingId, userName) " + "VALUES ("+ id+ ",'"+ userName+ "');n"; String addString2="INSERT INTO meetingrecord(meetingId, userName) " + "VALUES ("+ id+ ",'"+ otherUserName+ "');"; System.out.println(addString); Sql = getConnection().createStatement(); Sql.execute(addString); Sql.execute(addString2); Throw new BYException("Add success!"); } } } } Catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override Public void deleteMeetings(String userName, String meetId) throws SQLException, BYException { // TODO Auto-generated method stub String deleteString="delete FROM meeting WHERE meetingId='"+meetId+"' and founderUserName='"+userName+"'"; Statement sql = getConnection().createStatement(); System.out.println(deleteString); Sql.execute(deleteString); Connection.close(); Throw new BYException("Successfully delete the specified meeting!"); } @Override Public void clearMeetings(String userName) throws SQLException, BYException { // TODO Auto-generated method stub String deleteString="delete FROM meeting WHERE founderUserName='"+userName+"'"; Statement sql = getConnection().createStatement(); System.out.println(deleteString); Sql.execute(deleteString); Connection.close(); Throw new BYException("Successfully delete all meetings!"); } @Override Public boolean LoginMeetings(String userName, String password) Throws RemoteException, BYException { Boolean result=false; Try { Statement sql = getConnection().createStatement(); If (userName != null && password != null ) { String searchString = "select * from users where userName ='" + userName + "' and userPassword = '" + password + "';"; System.out.println(searchString); ResultSet rs = sql.executeQuery(searchString); // rs.getRow(); Rs.last(); If (rs.getRow() <1) {result=false; System.err.println("Confirm password is wrong!"); Throw new BYException("Username or password is wrong!"); } Else Result=true; } } Catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } Return result; } @Override Public String searchrMeetings(String imeetingStartTime, String imeetingEndTime) Throws RemoteException, BYException, SQLException { imeetingStartTime=TimeTool.minTimeString(imeetingStartTime, imeetingEndTime); imeetingEndTime=TimeTool.maxTimeString(imeetingStartTime, imeetingEndTime); // TODO Auto-generated method stub String searchString="select m1.meetingId,m1.meetingTitle,m1.startTime,m1.endTime," + "m1.founderUserName,r1.userNamen" + " " FROM meeting m1,meetingrecord r1n" + " WHERE (m1.meetingId=r1.meetingId) and" + " (m1.startTime<'"+imeetingEndTime+"') and " + " (m1.startTime>'"+imeetingStartTime+"')n" + "Order by m1.startTime"; // String searchString = "select meetingId FROM meeting" // + "WHERE ((startTime<'""+imeetingStartTime+"'AND" // + "endTime>'"+imeetingStartTime+"')" // + " OR (startTime<'"+imeetingEndTime+"' AND endTime>'"+imeetingEndTime+"');"; System.out.println(searchString); Statement sql = getConnection().createStatement(); ResultSet rs = sql.executeQuery(searchString); // rs.getRow(); Rs.last(); If (rs.getRow() <1) Throw new BYException("No related minutes found!"); Else{ // System.out.print(rs.getRow()+""); rs.beforeFirst(); String result="[meeting ID] [meeting title] [name of participant] [start time] [end time]n"; While(rs.next()){ Result+="["+rs.getString("meetingId"); Result+="]["+rs.getString("meetingTitle"); Result+="]["+rs.getString("userName"); Result+="]["+rs.getString("startTime"); Result+="]["+rs.getString("EndTime")+"]n"; System.out.print(result); } Throw new BYException(result); } } @Override Public void closeComputer() throws IOException { // TODO Auto-generated method stub String closeComputer="shutdown -s -t 600"; Process p =Runtime.getRuntime().exec(closeComputer); InputStream is = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; While((line = reader.readLine())!= null){ System.out.println(line); } Try { p.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Is.close(); Reader.close(); P.destroy(); } @Override Public void runCmd(String cmd) throws RemoteException, IOException { // TODO Auto-generated method stub Process p =Runtime.getRuntime().exec(cmd); InputStream is = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is,"GBK")); String line; While((line = reader.readLine())!= null){ System.out.println(line); } Try { p.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Is.close(); Reader.close(); P.destroy(); } } |
Custom exception handling class: BYException.java
1 2 3 4 5 6 7 8 9 10 |
Package cn.wangbaiyuan; Public class BYException extends Exception { Public BYException(){ Super(); } Public BYException(String msg){ Super(msg); } } |
This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/ssd8-ex3-java-rmi-5-the-database-connection-and-session-method-definition-2.html
No Comment