1. Strings can be converted to and from integers
String a = String.valueOf(2); //integer to numeric string
int i = Integer.parseInt(a); //numeric string to an int
Copy the code
2. Add content to the end of the file
BufferedWriter out = null;
try {
out = new BufferedWriter(newFileWriter (" filename ",true)); AString out. Write (" "); }catch (IOException e) {
// error processing code
} finally {
if(out ! =null) { out.close(); }}Copy the code
3. Get the name of the current method
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
Copy the code
4. Roll the string to the date
java.util.Date = java.text.DateFormat.getDateInstance().parse(date String); Or: SimpleDateFormat format =new SimpleDateFormat( "yyyy-MM-dd" );
Date date = format.parse( myString );
Copy the code
5. Use JDBC to connect to Oracle
public class OracleJdbcTest
{
String driverClass = "oracle.jdbc.driver.OracleDriver";
Connection con;
public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
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);
}
public void fetch(a) 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();
}
public static void main(String[] args)
{
OracleJdbcTest test = newOracleJdbcTest(); test.init(); test.fetch(); }}Copy the code
6. List files and directories
File dir = new File("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 directoryString 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) {
returnfile.isDirectory(); }}; files = dir.listFiles(fileFilter);Copy the code
7. Parse/read XML files
<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>West Gate</name>
<grade>A</grade>
<age>18</age>
</student>
</students>
Copy the code
Java paging code implementation
public class PageBean {
private int curPage; / / the current page
private int pageCount; / / the total number of pages
private int rowsCount; / / the total number of rows
private int pageSize=10; // How many lines per page
public PageBean(int rows){
this.setRowsCount(rows);
if(this.rowsCount % this.pageSize == 0) {this.pageCount=this.rowsCount / this.pageSize;
}
else if(rows<this.pageSize){
this.pageCount=1;
}
else{
this.pageCount=this.rowsCount / this.pageSize +1; }}public int getCurPage(a) {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getPageCount(a) {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageSize(a) {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRowsCount(a) {
return rowsCount;
}
public void setRowsCount(int rowsCount) {
this.rowsCount = rowsCount; }}Copy the code
Page display as follows
List clist = adminbiz.queryNotFullCourse();// Store the query result in the List collection
PageBean pagebean = new PageBean(clist.size());// Initialize the PageBean object
// Set the current page
pagebean.setCurPage(page); // Here page is a parameter from the page, representing the number of pages
// Get the page size
int pagesize = pagebean.getPageSize();
// Get the index of paging data in the list collection
int firstIndex = (page - 1) * pagesize;
int toIndex = page * pagesize;
if (toIndex > clist.size()) {
toIndex = clist.size();
}
if (firstIndex > toIndex) {
firstIndex = 0;
pagebean.setCurPage(1);
}
// Select paged data from paged data
List courseList = clist.subList(firstIndex, toIndex);
Copy the code