1. 字符串有整型的相互转换
Stringa = String.valueOf(2); //integer to numeric string
int i = Integer.parseInt(a); //numeric string to an int
2. 向文件末尾添加内容
BufferedWriter out= ;
try{
out= newBufferedWriter(newFileWriter(”filename”, true));
out.write(”aString”);
} catch(IOException e) {
// error processing code
} finally {
if (out != ) {
out.close;
}
}
3. 得到当前方法的名字
StringmethodName = Thread.currentThread.getStackTrace[1].getMethodName; 4. 转字符串到日期
java.util.Date = java.text.DateFormat.getDateInstance.parse(dateString);
或者是:
SimpleDateFormat format= new SimpleDateFormat( "yyyy-MM-dd");
Date date= format.parse( myString );
5. 使用JDBC链接Oracle
publicclassOracleJdbcTest
{
StringdriverClass = "oracle.jdbc.driver.OracleDriver";
Connection con;
publicvoidinit(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException
{
Properties props = newProperties;
props.load(fs);
Stringurl = props.getProperty("db.url");
StringuserName = props.getProperty("db.user");
Stringpassword = props.getProperty("db.password");
Class.forName(driverClass);
con=DriverManager.getConnection(url, userName, password);
}
publicvoidfetch throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");
ResultSet rs = ps.executeQuery;
while(rs.next)
{
// do the thing you do
}
rs.close;
ps.close;
}
publicstaticvoidmain(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest;
test.init;
test.fetch;
}
}
6.列出文件和目录
File dir = newFile("directoryName");
String children = dir.list;
if(children == ) {
【常用编程java代码大全 java代码大全及详解下载】 // Either dir does not exist or is not a directory
} else {
for (int i=0; i < children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}
// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);
// The list of files can also be retrieved as File objects
File files = dir.listFiles;
// This filter only returns directories
FileFilter fileFilter = new FileFilter {
public boolean accept(File file) {
return file.isDirectory;
}
};
files = dir.listFiles(fileFilter);
7.解析/读取XML 文件
<?xml version="1.0"?>
<students>
<student>
<name>John</name>
<grade>B</grade>
<age>12</age>
</student>
<student>
<name>Mary</name>
<grade>A</grade>
<age>11</age>
</student>
<student>
<name>Simon</name>
<grade>A</grade>
<age>18</age>
</student>
</students>
8. java分页代码实现
1public class PageBean {
2private intcurPage; //当前页
3private intpageCount; //总页数
4private introwsCount; //总行数
5private intpageSize=10; //每页多少行
6
7
8
9publicPageBean(introws){
10
11this.setRowsCount(rows);
12if(this.rowsCount % this.pageSize == 0){
13this.pageCount=this.rowsCount / this.pageSize;
14}
15elseif(rows<this.pageSize){
16this.pageCount=1;
17}
18else{
19this.pageCount=this.rowsCount / this.pageSize +1;
20}
21}
22
23
24publicintgetCurPage{
25returncurPage;
26}
27publicvoidsetCurPage(intcurPage) {
28this.curPage = curPage;
29}
30publicintgetPageCount{
31returnpageCount;
32}
33publicvoidsetPageCount(intpageCount) {
34this.pageCount = pageCount;
35}
36publicintgetPageSize{
37returnpageSize;
38}
39publicvoidsetPageSize(intpageSize) {
40this.pageSize = pageSize;
41}
42publicintgetRowsCount{
43returnrowsCount;
44}
45publicvoidsetRowsCount(introwsCount) {
46this.rowsCount = rowsCount;
47}
48}
分页展示如下
1List clist=adminbiz.queryNotFullCourse;//将查询结果存放在List集合里
2PageBean pagebean=new PageBean(clist.size);//初始化PageBean对象
3//设置当前页
4 pagebean.setCurPage(page); //这里page是从页面上获取的一个参数,代表页数
5 //获得分页大小
6 int pagesize=pagebean.getPageSize;
7 //获得分页数据在list集合中的索引
8 int firstIndex=(page-1)*pagesize;
9 int toIndex=page*pagesize;
10 if(toIndex>clist.size){
11 toIndex=clist.size;
12 }
13 if(firstIndex>toIndex){
14 firstIndex=0;
15 pagebean.setCurPage(1);
16 }
17 //截取数据集合,获得分页数据
18 List courseList=clist.subList(firstIndex, toIndex);
推荐阅读
- 4种常用方法图文教程 iphone传照片到电脑怎么传
- Ps常用快捷键大全一览表 ps复制粘贴的快捷键是什么
- 各按键作用与常用快捷键大全 键盘功能介绍大全图片
- 程序员必备的10款工具软件 编程有哪些软件
- 一文了解26款浏览器大全推荐 常用的中文浏览器有哪些
- 常用的频率和功率计算公式 赫兹和电功率怎么换算
- 新手常用电表实物接线图 220v电表接法实物图
- 4种常用方法图文教程 iphone照片怎么导入电脑
- 一文读懂计算机键盘按键的含义 键盘常用15个功能键基础知识
- 常用各种进制之间的转换 二进制转化为十进制具体方法