Java常用代码汇总

1. 字符串有整型的相互转换

Stringa = String.valueOf(2); //integer to numeric string

int i = Integer.parseInt(a); //numeric string to an int

2. 向文件末尾添加内容

BufferedWriter out= null;

try{

out= newBufferedWriter(newFileWriter(”filename”, true));

out.write(”aString”);

} catch(IOException e) {

// error processing code

} finally{

扫描二维码关注公众号,回复: 9209654 查看本文章

if(out!= null) {

out.close();

}

}

3. 得到当前方法的名字

StringmethodName = Thread.currentThread().getStackTrace()[1].getMethodName();

4. 转字符串到日期

java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);

或者是:

SimpleDateFormat format = newSimpleDateFormat( "yyyy-MM-dd");

Datedate = format.parse( myString );

5. 使用JDBC链接Oracle

publicclassOracleJdbcTest

{

String driverClass = "oracle.jdbc.driver.OracleDriver";

Connection con;

publicvoidinit(FileInputStream fs)throwsClassNotFoundException, SQLException, FileNotFoundException, IOException

{

Properties props = newProperties();

props.load(fs);

String url = props.getProperty("db.url");

String userName = props.getProperty("db.user");

String password = props.getProperty("db.password");

Class.forName(driverClass);

con=DriverManager.getConnection(url, userName, password);

}

publicvoidfetch()throwsSQLException, 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 = newOracleJdbcTest();

test.init();

test.fetch();

}

}

6.列出文件和目录

File dir = newFile("directoryName");

String[] children = dir.list();

if(children == null) {

// 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

Stringfilename = 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 = newFilenameFilter() {

publicbooleanaccept(File dir, Stringname) {

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 = newFileFilter() {

publicbooleanaccept(File file) {

returnfile.isDirectory();

}

};

files = dir.listFiles(fileFilter);

7.解析/读取XML 文件

<?xml version="1.0"?>

John

B

12

Mary

A

11

Simon

A

18

8.java分页代码实现

publicclassPageBean{

privateintcurPage; //当前页 

privateintpageCount; //总页数 

privateintrowsCount; //总行数 

privateintpageSize=10; //每页多少行 

publicPageBean(introws){ 

this.setRowsCount(rows); 

if(this.rowsCount % this.pageSize == 0){ 

this.pageCount=this.rowsCount / this.pageSize; 

elseif(rows

this.pageCount=1; 

else{ 

this.pageCount=this.rowsCount / this.pageSize +1; 

publicintgetCurPage(){ 

returncurPage; 

publicvoidsetCurPage(intcurPage){ 

this.curPage = curPage; 

publicintgetPageCount(){ 

returnpageCount; 

publicvoidsetPageCount(intpageCount){ 

this.pageCount = pageCount; 

publicintgetPageSize(){ 

returnpageSize; 

publicvoidsetPageSize(intpageSize){ 

this.pageSize = pageSize; 

publicintgetRowsCount(){ 

returnrowsCount; 

publicvoidsetRowsCount(introwsCount){ 

this.rowsCount = rowsCount; 

}

分页展示如下

List clist=adminbiz.queryNotFullCourse();//将查询结果存放在List集合里

PageBean pagebean=newPageBean(clist.size());//初始化PageBean对象 

//设置当前页 

pagebean.setCurPage(page); //这里page是从页面上获取的一个参数,代表页数 

//获得分页大小 

intpagesize=pagebean.getPageSize(); 

//获得分页数据在list集合中的索引 

intfirstIndex=(page-1)*pagesize; 

inttoIndex=page*pagesize; 

if(toIndex>clist.size()){ 

toIndex=clist.size(); 

if(firstIndex>toIndex){ 

firstIndex=0; 

pagebean.setCurPage(1); 

//截取数据集合,获得分页数据 

List courseList=clist.subList(firstIndex, toIndex);

发布了11 篇原创文章 · 获赞 466 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/weixin_42784331/article/details/104319693
今日推荐