Java小贴士

  1. Sql Date与Date
    java.sql.Date 是针对SQL语句使用的,它只包含日期而没有时间部分;java.util.Date 是 java.sql.Date 的父类(注意拼写)前者是常用的表示时间的类,我们通常格式化或者得到当前时间都是用他.后者之后在读写数据库的时候用他,因为PreparedStament的setDate()的第2参数和ResultSet的getDate()方法的第2个参数都是java.sql.Date
    java.sql.Date date=new java.sql.Date();
    java.util.Date d=new java.util.Date (date.getTime());
    另:
    java.sql.Time sTime=new java.sql.Time(d.getTime());     
    java.sql.Timestamp stp=new java.sql.Timestamp(d.getTime());
  2. Java访问Oracle,设置动态SQL参数值Number类型的小数可用setString(), setBigDecimal()来设置. 用setFloat()会发生舍入误差.例如:9999999999.99 会变成10000000000.
  3. Java连接Oracle
      Connection conn = new OracleDriver().defaultConnection();
      String sql = "INSERT INTO user (id,name) " + "VALUES (?,?)";
      try {
          PreparedStatement pstmt = conn.prepareStatement(sql);
          pstmt.setString(1, id);
          pstmt.setString(2, name);
          pstmt.executeUpdate();
          pstmt.close();
        } catch (SQLException e) { }
  4. How to get current date string like '11-MAY-2006'
          Locale aLocale = new Locale("en");
          Calendar cal = new GregorianCalendar();
          Date date = cal.getTime();
          SimpleDateFormat sf = new SimpleDateFormat("dd-MMM-yyyy", aLocale);
          System.out.println("==============curdate="+sf.format(date));
  5. How to get the next day of a specified date?
    import java.util.GregorianCalendar;
    GregorianCalendar cal = new GregorianCalendar (2005, 8, 1, 0, 0,1);
    cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
    Date nextday = cal.getTime();    
    -Please note that, the value used to set the MONTH time field in the calendar is 0-based. e.g., 0 for January.
  6. How to copy files
    import  java.io. * ;
    public   class  Copy  {
      
    public static void main(String[] args) throws IOException {
        File inputFile 
    = new File("farrago.txt");
        File outputFile 
    = new File("outagain.txt");
        FileReader in 
    = new FileReader(inputFile);
        FileWriter out 
    = new FileWriter(outputFile);
        
    int c;
        
    while ((c = in.read()) != -1)
          out.write(c);
        in.close();
        out.close();
      }

    }


  7. How to use Properties
    Properties prop  =   new  Properties();
    prop.setProperty(
    " abc " "" );
    Please note that, NullPointerException will be thrown: prop.setProperty(
    " abc " null ); 
  8.  How to remove all '-' in the string
    System. out .println( " str= " + str.replaceAll( " - " "" ));
  9.     How to trim a string
    String abc  =   "  300.00 " ;
    System.out.println(
    " trim= " + abc.trim());    
  10.  How to use StringTokenizer?
    String str  =   " '001','182-0-935722','',' 0.00',' 0.00','','20 Jul 2006','2','6','1820935722','' " ;
    StringTokenizer st 
    =   new  StringTokenizer(str.substring(str.indexOf( " ( " ) + 1 , str.indexOf( " ) " )),  " , " );
    String[] strArray 
    =   new  String[ 11 ];
    System.out.println(st.countTokens());
    while  (st.hasMoreElements()) {
         String next 
    = st.nextToken();
         System.out.println(
    "next="+next);
          
    //remove ' '
         System.out.println("next="+next.substring(1, next.lastIndexOf("'")));      
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值