java经典程序代码(java新手代码大全)

每日英文

Things have a way of working out when you least expect it. Never give it up.

事情总是在你最不抱期望的时候得到解决,所以,永远不要放弃。

每日掏心话

青春就应该有一种快乐勇敢的气质,这不光是青春的一种表现,也可以理解为是青春的秘密。

责编:乐乐 | 来自:方志明

编程技术圈(ID:study_tech)第 1402期推文

往日回顾:知名国产论坛,凉了!!!!

正文

大家好,我是小乐。

java经典程序代码(java新手代码大全)

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

Stringa =String.valueOf(2);//integer to numeric stringint i = Integer.parseInt(a);//numeric string to an int2. 向文件末尾添加内容BufferedWriterout=null;try{out=newBufferedWriter(newFileWriter(”filename”,true));out.write(”aString”);}catch(IOException e) {// error processing code}finally{if(out!=null) {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链接OraclepublicclassOracleJdbcTest{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 directoryStringfilename = 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 objectsFile[] files = dir.listFiles();// This filter only returns directoriesFileFilter 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;//当前页3privateintpageCount;//总页数4privateintrowsCount;//总行数5privateintpageSize=10;//每页多少行6789publicPageBean(introws){1011this.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}222324publicintgetCurPage(){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=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//截取数据集合,获得分页数据18List courseList=clist.subList(firstIndex, toIndex);

你还有什么想要补充的吗?

PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。

欢迎加入后端架构师交流群,在后台回复“学习”即可。

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。在这里,我为大家准备了一份2021年最新最全BAT等大厂Java面试经验总结。

别找了,想获取史上最简单的Java大厂面试题学习资料

扫下方二维码回复「面试」就好了

发表评论

登录后才能评论