public void run() { try { BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf8")); while (true) { print(bf.readLine()); } } catch (Exception e) { e.printStackTrace(); } }
/** * `获取给定对象的属性的值` `@param fieldName 属性名` `@param obj 对象` * * @return */ public static Object getFieldValue(String fieldName, Object obj) { try { Class cls = obj.getClass(); Field field = cls.getDeclaredField(fieldName); return field.get(obj); } catch (Exception e) { e.printStackTrace(); print("Field access error!"); throw new RuntimeException(e); } }
/** * `使用反射机制调用方法` * * @param methodName Method name * @param obj Object holder of the method * @param paraType Array of parameter types * @param paraValue Array of parameters * @return Returned object of the method */ public static Object call(String methodName, Object obj, Class[] paraType, Object[] paraValue) { try { Class cls = obj.getClass(); Method method = cls.getDeclaredMethod(methodName, paraType); // invoke() returns the object returned by the // underlying method. // It returns null, if the underlying method returns void. return method.invoke(obj, paraValue); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
/** `使用类名创建对象` `@param className 类名` `@return 创建的对象` */ public static Object create(String className) { try { // `获取Class的实例,即所描述的类` // `JVM先在内存中寻找这个实例,如果有,则直接使用;如果没有,就加载.class文件,` // `创建文类的Class实例,并存入内存。` // `这种加载机制叫做惰性加载。` Class cls = Class.forName(className); return cls.newInstance(); } catch (Exception e) { e.printStackTrace(); // Throw the exception to suppress error message. throw new RuntimeException(e); } }
public static int execute() throws SQLException { int flag = -1; Connection conn = null; CallableStatement cstmt = null; try { conn = ConnUtil.openConnection(); String sql = "{call av_sal(?,?)}"; cstmt = conn.prepareCall(sql); cstmt.setInt(1, 20); cstmt.registerOutParameter(2, Types.NUMERIC); cstmt.execute(); flag = (int) cstmt.getFloat(2); // flag=cstmt.getInt(2); return flag; } catch (Exception e) { e.printStackTrace(); } finally { ConnUtil.closeStatement(cstmt); ConnUtil.closeConnection(conn); } return flag; }