- 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()); - Java访问Oracle,设置动态SQL参数值Number类型的小数可用setString(), setBigDecimal()来设置. 用setFloat()会发生舍入误差.例如:9999999999.99 会变成10000000000.
- 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) { } - 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)); - 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. - 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();
}
}
- How to use Properties
Properties prop = new Properties();
prop.setProperty( " abc " , "" );
Please note that, NullPointerException will be thrown: prop.setProperty( " abc " , null ); - How to remove all '-' in the string
System. out .println( " str= " + str.replaceAll( " - " , "" ));
- How to trim a string
String abc = " 300.00 " ;
System.out.println( " trim= " + abc.trim()); - 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("'")));
}
Java小贴士
最新推荐文章于 2024-07-27 13:31:57 发布