Java Common Code Summary

1. There integer string conversion

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

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

2. Add content to the end of the file

BufferedWriter out= null;

try{

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

out.write(”aString”);

} catch(IOException e) {

// error processing code

} finally{

if(out!= null) {

out.close();

}

}

3. Get the name of the current method of

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

4. The date to String

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

or:

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

Datedate = format.parse( myString );

5. Use the Oracle JDBC link

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. List files and directories

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. parsing / reading an XML file

<?xml version="1.0"?>

 

 

John

B

12

 

Mary

A

11

 

Simon

A

18

8.java paging code implementation

publicclassPageBean {

privateintcurPage; // this page 

privateintpageCount; // total number of pages 

privateintrowsCount; // total number of rows 

privateintpageSize = 10; // how many rows per page 

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; 

}

Paging is shown below

List clist = adminbiz.queryNotFullCourse (); // query results stored in the collection List

PageBean pagebean = newPageBean (clist.size ()); // initialize objects PageBean 

// Set the current page 

pagebean.setCurPage (page); // this page is taken from the page of a parameter representative of the number of pages 

// Get the page size 

intpagesize=pagebean.getPageSize(); 

// get the index page in the list of data collection 

intfirstIndex=(page-1)*pagesize; 

inttoIndex=page*pagesize; 

if(toIndex>clist.size()){ 

toIndex=clist.size(); 

if(firstIndex>toIndex){ 

firstIndex=0; 

pagebean.setCurPage(1); 

// intercept the data set, the data obtained tab 

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

 

Published 11 original articles · won praise 466 · views 110 000 +

Guess you like

Origin blog.csdn.net/weixin_42784331/article/details/104319693