隨筆-71  評論-5  文章-0  trackbacks-0
            2015年8月31日
               摘要: 一個stmt多個rs進行操作.那么從stmt得到的rs1,必須馬上操作此rs1后,才能去得到另外的rs2,再對rs2操作.不能互相交替使用,會引起rs已經(jīng)關(guān)閉錯誤——Operation not allowed after ResultSet closed.錯誤的代碼如下: stmt=conn.createStatement();  rs=stm...  閱讀全文
          posted @ 2015-10-13 14:58 藤本薔薇 閱讀(216) | 評論 (0)編輯 收藏
          在 JDK1.5 引入自動裝箱和拆箱的機制后,包裝類和基本類型之間的轉(zhuǎn)換就更加輕松便利了。

          裝箱:把基本類型轉(zhuǎn)換成包裝類,使其具有對象的性質(zhì),又可分為手動裝箱和自動裝箱

          int i= 10; //定義一個int基本類型值
          Integer x = new Integer(i); //手動裝箱
          Integer y = i; //自動裝箱
          posted @ 2015-09-22 20:28 藤本薔薇 閱讀(229) | 評論 (0)編輯 收藏

          功能:將一個由英文字母組成的字符串轉(zhuǎn)換成指定格式---從右邊開始每三個字母用逗號分隔的形式。

          請在編輯器中的第 4、10 行將代碼填寫完整

          運行效果: j,aew,kjl,dfx,mop,zdmpublic static void main(String[] args) {
                  // Java文件名
          String fileName = "HelloWorld.jav"; 
                  // 郵箱
          String email = "laurenyang@imooc.com";
          // 判斷.java文件名是否正確:合法的文件名應(yīng)該以.java結(jié)尾
                  /*
                  參考步驟:
                  1、獲取文件名中最后一次出現(xiàn)"."號的位置
                  2、根據(jù)"."號的位置,獲取文件的后綴
                  3、判斷"."號位置及文件后綴名
                  */
                  //獲取文件名中最后一次出現(xiàn)"."號的位置
          int index = fileName.lastIndexOf('.');
                  
                  // 獲取文件的后綴
          String prefix = fileName.substring(index);
                  
          // 判斷必須包含"."號,且不能出現(xiàn)在首位,同時后綴名為"java"
          if (index !=0 && index != -1 &&  prefix.equals("java")) {
          System.out.println("Java文件名正確");
          } else {
          System.out.println("Java文件名無效");
          }

                  // 判斷郵箱格式是否正確:合法的郵箱名中至少要包含"@", 并且"@"是在"."之前
                   /*
                  參考步驟:
                  1、獲取文件名中"@"符號的位置
                  2、獲取郵箱中"."號的位置
                  3、判斷必須包含"@"符號,且"@"必須在"."之前
                  */
             // 獲取郵箱中"@"符號的位置
          int index2 = email.indexOf("@");
                  
                  // 獲取郵箱中"."號的位置
          int index3 = email.indexOf('.');
                  
          // 判斷必須包含"@"符號,且"@"必須在"."之前
          if (index2 != -1 && index3 > index2) {
          System.out.println("郵箱格式正確");
          } else {
          System.out.println("郵箱格式無效");
          }
          }






          字節(jié)是計算機存儲信息的基本單位,1 個字節(jié)等于 8 位, gbk 編碼中 1 個漢字字符存儲需要 2 個字節(jié)1 個英文字符存儲需要 1 個字節(jié)。所以我們看到上面的程序運行結(jié)果中,每個漢字對應(yīng)兩個字節(jié)值,如“學(xué)”對應(yīng) “-47 -89” ,而英文字母 “J” 對應(yīng) “74” 。同時,我們還發(fā)現(xiàn)漢字對應(yīng)的字節(jié)值為負數(shù),原因在于每個字節(jié)是 8 位,最大值不能超過 127,而漢字轉(zhuǎn)換為字節(jié)后超過 127,如果超過就會溢出,以負數(shù)的形式顯示。






               public static void main(String[] args) {
          // 定義一個字符串
          String s = "aljlkdsflkjsadjfklhasdkjlflkajdflwoiudsafhaasdasd";
                  
                  // 出現(xiàn)次數(shù)
          int num = 0;
                   // 循環(huán)遍歷每個字符,判斷是否是字符 a ,如果是,累加次數(shù)
          for ( int i=0;i<s.length()-1;i++)
          {
                      // 獲取每個字符,判斷是否是字符a
          if (  'a'==s.charAt(i)      ) {
                          // 累加統(tǒng)計次數(shù)
          num++; 
          }
          }
          System.out.println("字符a出現(xiàn)的次數(shù):" + num);
          }




          功能:將一個由英文字母組成的字符串轉(zhuǎn)換成指定格式---從右邊開始每三個字母用逗號分隔的形式。

          請在編輯器中的第 4、10 行將代碼填寫完整

          運行效果: j,aew,kjl,dfx,mop,zdm


              public static void main(String[] args) {
          // 創(chuàng)建一個空的StringBuilder對象
                  StringBuilder str = new StringBuilder();
          // 追加字符串
          str.append("jaewkjldfxmopzdm");
                  // 從后往前每隔三位插入逗號
          for(int i = str.length()-3; i>0 ; i=i-3){
               
                     str.insert(i,",");
          }
              
                  // 將StringBuilder對象轉(zhuǎn)換為String對象并輸出
          System.out.print(str.toString());
          }

           結(jié)果: j,aew,kjl,dfx,mop,zdm
          posted @ 2015-09-22 16:12 藤本薔薇 閱讀(357) | 評論 (0)編輯 收藏
          String s1 = "imooc";
          String s2 = "imooc";
                  
                  //定義字符串s3,保存“I love”和s1拼接后的內(nèi)容
          String s3 = "I love" + s1; 
                  // 比較字符串s1和s2
          // imooc為常量字符串,多次出現(xiàn)時會被編譯器優(yōu)化,只創(chuàng)建一個對象
          System.out.println("s1和s2內(nèi)存地址相同嗎?" + (s1 == s2));
                  
                  //比較字符串s1和s3
          System.out.println("s1和s3內(nèi)存地址相同嗎?" +  (s1==s3));
          String s4 = "I love " + s1;
                   //比較字符串s4和s3
          // s1是變量,s4在運行時才知道具體值,所以s3和s4是不同的對象
          System.out.println("s3和s4內(nèi)存地址相同嗎?" + (s4 == s3));
          posted @ 2015-09-22 15:26 藤本薔薇 閱讀(278) | 評論 (0)編輯 收藏
          //外部類HelloWorld
          public class HelloWorld {
              
              // 內(nèi)部類Inner,類Inner在類HelloWorld的內(nèi)部
              public class Inner {
                  
          // 內(nèi)部類的方法
          public void show() {
          System.out.println("welcome to imooc!");
          }
          }
              
          public static void main(String[] args) {
                  
                  // 創(chuàng)建外部類對象
          HelloWorld hello = new HelloWorld();
                  // 創(chuàng)建內(nèi)部類對象
          Inner i = hello.new Inner();
                  // 調(diào)用內(nèi)部類對象的方法
          i.show();
          }
          }
          posted @ 2015-09-09 15:47 藤本薔薇 閱讀(313) | 評論 (0)編輯 收藏














          posted @ 2015-09-09 14:41 藤本薔薇 閱讀(321) | 評論 (0)編輯 收藏
          不清楚路徑的查找 : find / -name mysql


           MYSQL常用經(jīng)典命令(沒有試過)
          1.停止mysql
          kill -9 `ps -ef | grep mysqld_safe| grep -v grep| awk '{print $2}'`
          kill -9 `ps -ef | grep 'mysqld' | grep -v grep| awk '{print $2}'`

          2.啟動mysql
          cd /usr/local/mysql-5.4.1-beta-linux-x86_64-glibc23
          /bin/sh bin/mysqld_safe --user=mysql & 


          find /home/lijiajia/ -amin -10        #查找在系統(tǒng)中最后10分鐘訪問的文件
          find /home/lijiajia/ -atime -2        #查找在系統(tǒng)中最后48小時訪問的文件
          find /home/lijiajia/ -empty           #查找在系統(tǒng)中為空的文件或者文件夾
          find /home/lijiajia/ -mmin -5         # 查找在系統(tǒng)中最后5 分鐘里修改過的文件
          find /home/lijiajia/ -mtime -1        #查找在系統(tǒng)中最后24 小時里修改過的文件
          find /home/lijiajia/ -nouser          #查找在系統(tǒng)中屬于作廢用戶的文件(不明白是什么意思)
          find /home/lijiajia/ -amin 10         #查找在系統(tǒng)中最后10分鐘訪問的文件
          find /home/ftp/pub -user lijiajia     #查找在系統(tǒng)中屬于lijiajia這個用戶的文件
          posted @ 2015-09-02 10:26 藤本薔薇 閱讀(214) | 評論 (0)編輯 收藏
            public static Map ConvertObjToMap(Object obj){
                        Map<String,Object> reMap = new HashMap<String,Object>();
                        if (obj == null
                         return null;
                        Field[] fields = obj.getClass().getDeclaredFields();
                        try {
                         for(int i=0;i<fields.length;i++){
                          try {
                           Field f = obj.getClass().getDeclaredField(fields[i].getName());
                           f.setAccessible(true);
                                 Object o = f.get(obj);
                                 if(o == null){
                                     o = "";
                                 }else{
                                     o = String.valueOf(o);
                                 }
                                 reMap.put(fields[i].getName(), String.valueOf(o));
                          } catch (NoSuchFieldException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                          } catch (IllegalArgumentException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                          } catch (IllegalAccessException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                          }
                         }
                        } catch (SecurityException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                        } 
                        return reMap;
                       }
          posted @ 2015-08-31 19:52 藤本薔薇 閱讀(1019) | 評論 (0)編輯 收藏

          import java.io.IOException;
          import java.util.Date;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpException;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.PostMethod;
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;

          import com.alibaba.fastjson.JSONObject;
          import com.huoniu.openapi.constant.Constant.MESSAGE;
          import com.huoniu.openapi.constant.InvokeContext;
          import com.huoniu.openapi.model.RetCode;
          import com.huoniu.openapi.model.RetMsg;
          import com.huoniu.openapi.model.SmsCode;
          import com.huoniu.openapi.service.SmsCodeService;
          import com.huoniu.openapi.utils.AESUtil;
          import com.huoniu.openapi.utils.SmsUtil;
          import com.huoniu.openapi.web.interceptor.InvokeContextInitInterceptor;
          import com.puff.framework.annotation.Before;
          import com.puff.framework.annotation.Controller;
          import com.puff.framework.annotation.Inject;
          import com.puff.framework.annotation.InterceptorChain;
          import com.puff.framework.utils.JsonUtil;
          import com.puff.framework.utils.StringUtil;
          import com.puff.web.view.TextView;
          import com.puff.web.view.View;
          import com.puff.web.view.ViewFactory;

          @Controller("/rest/sms")
          public class HuyiSmsController {    

              private static String content = "您的驗證碼是:%s。請不要把驗證碼泄露給其他人。";
              public View send(){
                  
                  String invokeData = InvokeContext.getInvokeData();
                  if (StringUtil.blank(invokeData)) {
                      return json(RetMsg.error(RetCode.OTHER_ERROR, "發(fā)送短信失敗!"));
                  }
                  
                  JSONObject jsonObject = JSONObject.parseObject(invokeData);
                  String  mobile = jsonObject.getString("customer_no");
                  if (StringUtil.blank(mobile)) {
                      return json(RetMsg.error(RetCode.NULL_PARAM, "手機號碼不能為空"));
                  }
                      
                      HttpClient client = new HttpClient(); 
                      PostMethod method = new PostMethod(MESSAGE.NEW_MESSAGEURL);  //接口地址
                          
                      client.getParams().setContentCharset("UTF-8");
                      method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");

                      int mobile_code = (int)((Math.random()*9+1)*100000);        
                                                         System.out.println("mobile_code : "+mobile_code);
                      NameValuePair[] data = {//提交短信
                              new NameValuePair("account", MESSAGE.NEW_ACCOUNT), 
                              new NameValuePair("password", SmsUtil.MD5Encode(MESSAGE.NEW_PASSWORD)),
                              new NameValuePair("mobile", mobile), 
                              new NameValuePair("content", String.format(content, mobile_code)),
                      };
                      
                      method.setRequestBody(data);        

                      try {
                          client.executeMethod(method);    
                          String SubmitResult =method.getResponseBodyAsString();
                          Document doc = DocumentHelper.parseText(SubmitResult); 
                          Element root = doc.getRootElement();
                          String code = root.elementText("code");    
                          String msg = root.elementText("msg");    
                          String smsid = root.elementText("smsid");    
                                      
                          if(code == "2"){  //發(fā)送成功,寫庫
                              
                          }
                          
                      } catch (HttpException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } catch (DocumentException e) {
                          e.printStackTrace();
                      }    
                      
                  
                      return json(RetMsg.success("發(fā)送成功!!!"));
                  
              }
              
              
              
              public View json(RetMsg msg) {
                  String data = JsonUtil.toJson(msg);
                  if (InvokeContext.isEncrypt()) {
                      data = AESUtil.encrypt(data);
                  }
                  return ViewFactory.text(data, TextView.ContentType.JSON);
              }

          }
          posted @ 2015-08-31 10:02 藤本薔薇 閱讀(306) | 評論 (0)編輯 收藏
          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.PostMethod;

          import com.alibaba.fastjson.JSONObject;
          import com.huoniu.openapi.constant.Constant.TONGLIAN;


          public class TLInterfaceService {
              
              private static final String USERNAME = "";
              private static final String PASSWORD = "";
              private static final String TENANT = "";
              private static final String GRANT_TYPE_GET = "password";    
              private static final String GRANT_TYPE_REFRESH = "refresh_token";    
              
              private static String TOKEN = null;
              private static String REFRESH_TOKEN = null;
              private static long EXPIRES_IN ;
              private static long START_DATE;

              
              public  String getToken() {
                  if(TOKEN==null){
                          TOKEN  = login();
                  }else{
                      Date date = new Date();
                      if(START_DATE-date.getTime()<EXPIRES_IN-30){
                          TOKEN  = refresh();
                      }
                      
                  }
                  
                  return TOKEN;
              }
              
              
              public  void setToken(String token) {
                  TOKEN = token;
              }


              /**
               * 登陸,獲取token
               * 
          @return 
               
          */
                      
                  public static String login(){

                          HttpClient httpClient = new HttpClient();
                          String URL = String.format(TONGLIAN.PREV_PATH, "");  //接口地址
                          try {
                              PostMethod postMethod = new PostMethod(URL);
                              postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
                              NameValuePair[] data = { 
                                      new NameValuePair("username", USERNAME),
                                      new NameValuePair("password", PASSWORD),
                                      new NameValuePair("tenant",  TENANT),
                                      new NameValuePair("grant_type", GRANT_TYPE_GET)
                                       };
                              postMethod.setRequestBody(data);
                              int statusCode = httpClient.executeMethod(postMethod);
                              if(200 == statusCode){
                                  String body  = postMethod.getResponseBodyAsString();
                                  JSONObject json=JSONObject.parseObject(body);
                                  TOKEN = json.getString("access_token");
                                  REFRESH_TOKEN = json.getString("refresh_token");
                                  EXPIRES_IN = Long.parseLong(json.getString("expires_in"));
                                  START_DATE =  (new Date()).getTime();
                              }
                          } catch (Exception e) {
                              
                          }
                          return TOKEN;
                   }
                  
                  /**
                   * refresh_token
                   * 
          @return 
                   
          */
                                  
                  public static String refresh(){
                          
                      HttpClient httpClient = new HttpClient();
                          String URL = String.format(TONGLIAN.PREV_PATH, ""); //接口地址
                          try {
                              PostMethod postMethod = new PostMethod(URL);
                              postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
                              NameValuePair[] data = { 
                                      new NameValuePair("refresh_token", REFRESH_TOKEN),
                                      new NameValuePair("grant_type", GRANT_TYPE_REFRESH)
                               };
                              postMethod.setRequestBody(data);
                              int statusCode = httpClient.executeMethod(postMethod);
                              
                              if(200 == statusCode){
                                  String body  = postMethod.getResponseBodyAsString();
                                  JSONObject json=JSONObject.parseObject(body);
                                  TOKEN = json.getString("access_token");
                                  REFRESH_TOKEN = json.getString("refresh_token");
                                  EXPIRES_IN = Long.parseLong(json.getString("expires_in"));
                                  START_DATE =  (new Date()).getTime();
                              }
                          } catch (Exception e) {
                              
                          }
                          return TOKEN;
                   }

          }
          posted @ 2015-08-31 09:54 藤本薔薇 閱讀(290) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 获嘉县| 峨边| 丰顺县| 高邑县| 钦州市| 常宁市| 青神县| 山阳县| 酉阳| 什邡市| 城固县| 浦县| 阜南县| 禹城市| 海原县| 昭平县| 庆元县| 稷山县| 齐河县| 平南县| 图木舒克市| 永登县| 利辛县| 合山市| 阜康市| 晋城| 崇明县| 南召县| 赤城县| 天长市| 汕尾市| 云安县| 神池县| 墨江| 寿光市| 苏尼特右旗| 肥城市| 六安市| 晋中市| 晴隆县| 博兴县|