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= 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 == ) {
// 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"?>
<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分页代码实现
1publicclassPageBean{
2privateintcurPage;//当前页
3privateintpaount;//总页数
4privateintrowsCount;//总行数
5privateintpageSize=10;//每页多少行
6
7
8
9publicPageBean(introws){
10
11this.setRowsCount(rows);
12if(this.rowsCount%this.pageSize==0){
13this.paount=this.rowsCount/this.pageSize;
14}
15elseif(rows<this.pageSize){
16this.paount=1;
17}
18else{
19this.paount=this.rowsCount/this.pageSize+1;
20}
21}
22
23
24publicintgetCurPage(){
25returncurPage;
26}
27publicvoidsetCurPage(intcurPage){
28this.curPage=curPage;
29}
30publicintgetPaount(){
31returnpaount;
32}
33publicvoidsetPaount(intpaount){
34this.paount=paount;
35}
36publicintgetPageSize(){
37returnpageSize;
38}
39publicvoidsetPageSize(intpageSize){
40this.pageSize=pageSize;
41}
42publicintgetRowsCount(){
43returnrowsCount;
44}
45publicvoidsetRowsCount(introwsCount){
46this.rowsCount=rowsCount;
47}
48}
分页展示如下
1Listclist=adminbiz.queryNotFullCourse();//将查询结果存放在List集合里
2PageBeanpagebean=newPageBean(clist.size());//初始化PageBean对象
3//设置当前页
4pagebean.setCurPage(page);//这里page是从页面上获取的一个参数,页数
5//获得分页大小
6intpagesize=pagebean.getPageSize();
7//获得分页数据在list集合中的索引
8intfirstIndex=(page-1)*pagesize;
9inttoIndex=page*pagesize;
10if(toIndex>clist.size()){
11toIndex=clist.size();
12}
13if(firstIndex>toIndex){
14firstIndex=0;
15pagebean.setCurPage(1);
16}
17//截取数据集合,获得分页数据
18ListcourseList=clist.subList(firstIndex,toIndex);
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
蜀山号 » java代码大全及详解(java必背代码入门)
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 蜀山号