1、java访问 xml文件Java code import java.io.*;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class xmljava public static void main(String args) Element
2、 element=null;File f =new File(“a.xml“);DocumentBuilder db=null; /documentBuilder为抽象不能直接实例化(将 XML文件转换为 DOM文件)DocumentBuilderFactory dbf=null; trydbf= DocumentBuilderFactory.newInstance(); /返回documentBuilderFactory对象 db =dbf.newDocumentBuilder();/返回 db对象用documentBuilderFatory对象获得返回 documentBuildr对象Do
3、cument dt= db.parse(f); /得到一个 DOM并返回给document对象element = dt.getDocumentElement();/得到一个 elment根元素System.out.println(“根元素:“+element.getNodeName(); /获得根节点NodeList childNodes =element.getChildNodes() ; / 获得根元素下的子节点for (int i = 0; i 下的节点for (int j = 0; j 下的节点Node detail = nodeDetail.item(j); / 获得元素每一个节点i
4、f (“code“.equals(detail.getNodeName() / 输出codeSystem.out.println(“卡号: “ + detail.getTextContent();else if (“pass“.equals(detail.getNodeName() / 输出 passSystem.out.println(“密码: “ + detail.getTextContent();else if (“name“.equals(detail.getNodeName() / 输出 nameSystem.out.println(“姓名: “ + detail.getTextCo
5、ntent();else if (“money“.equals(detail.getNodeName() / 输出 moneySystem.out.println(“余额: “+ detail.getTextContent();catch(Exception e)System.out.println(e);XML code 100001123李四 1000000.00 100002123张三 1000.00 对我有用34 丢个板砖0 引用 举报 管理 TOP 回复次数:1539 afgasdg (米林) 等 级:#1楼 得分: 0回复于:2011-03-31 23:23:18java jdbc
6、数据库连接Java code import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class JDBConnection public Connection conn = null; / 声明Connection对象的实例public Statemen
7、t stmt = null; / 声明 Statement对象的实例public ResultSet rs = null; / 声明 ResultSet对象的实例private static String dbClassName = “com.microsoft.jdbc.sqlserver.SQLServerDriver“;/定义保存数据库驱动的变量private static String dbUrl = “jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=DB_ATM“;private static String dbUser =
8、 “sa“;private static String dbPwd = “sa“;public JDBConnection(String propertyFileName) / 带属性文件名的构造方法Properties prop = new Properties();/ 属性集合对象InputStream is = null;try is = JDBConnection.class.getClassLoader().getResourceAsStream(propertyFileName);/ 属性文件输入流/ is = new FileInputStream(“src/“ + proper
9、tyFileName);prop.load(is);/ 将属性文件流装载到Properties对象中is.close();/ 关闭流dbClassName = prop.getProperty(“dbClassName“);dbUrl = prop.getProperty(“dbUrl“);dbUser = prop.getProperty(“dbUser“);dbPwd = prop.getProperty(“dbPwd“); catch (Exception e) System.out.println(“属性文件 “ + propertyFileName + “ 打开失败!“);try C
10、lass.forName(dbClassName);/ 1.注册驱动 catch (ClassNotFoundException e) e.printStackTrace();public JDBConnection() / 默认的不带参数的构造函数try Class.forName(dbClassName);/ 1.注册驱动 catch (ClassNotFoundException e) e.printStackTrace();public static Connection getConnection() Connection conn = null;try / Class.forNam
11、e(dbClassName);/ 1.注册驱动conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);/2.建立与数据库的链接 catch (Exception ee) ee.printStackTrace();if (conn = null) System.err.println(“警告: DbConnectionManager.getConnection() 获得数据库链接失败.rnrn 链接类型:“+ dbClassName+ “rn链接位置:“+ dbUrl+ “rn用户/密码“+ dbUser + “/“ + dbPwd);r
12、eturn conn;/* 功能:执行查询语句*/public ResultSet executeQuery(String sql) try / 捕捉异常conn = getConnection(); / 调用getConnection()方法构造 Connection对象的一个实例connstmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,/3.创建语句ResultSet.CONCUR_READ_ONLY);rs = stmt.executeQuery(sql);/4.执行查询 catch (SQLException e
13、x) System.err.println(ex.getMessage(); / 输出异常信息return rs; / 返回结果集对象 5.结果处理/* 功能:执行更新操作*/public int executeUpdate(String sql) int result = 0; / 定义保存返回值的变量try / 捕捉异常conn = getConnection(); / 调用getConnection()方法构造 Connection对象的一个实例connstmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,Result
14、Set.CONCUR_READ_ONLY);result = stmt.executeUpdate(sql); / 执行更新操作 catch (SQLException ex) result = 0; / 将保存返回值的变量赋值为 0return result; / 返回保存返回值的变量/* 功能:关闭数据库的连接*/public void close() /6.释放资源try / 捕捉异常try if (rs != null) / 当 ResultSet对象的实例 rs不为空时rs.close(); / 关闭 ResultSet对象 finally try if (stmt != null)
15、 / 当Statement对象的实例 stmt不为空时stmt.close(); / 关闭Statement对象 finally if (conn != null) / 当Connection对象的实例 conn不为空时conn.close(); / 关闭Connection对象 catch (Exception e) e.printStackTrace(System.err); / 输出异常信息属性文件dbClassName=com.microsoft.jdbc.sqlserver.SQLServerDriverdbClassName2=com.mysql.jdbc.DriverdbPwd=
16、sadbPwd2=rootdbUrl=jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=DB_ATMdbUrl2=jdbc:mysql:/localhost:3306/db_atmdbUser=sadbUser2=root 对我有用13 丢个板砖0 引用 举报 管理 TOP #2楼 得分: 0回复于:2011-03-31 23:24:38多谢楼主 zqfddqr (zqfddqr) 等 级: 对我有用1 丢个板砖0 引用 举报 管理 TOP afgasdg (米林) 等 级: #3楼 得分: 0回复于:2011-03-31 23:25:
17、11java自定义按钮外观Java code import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.UIManager;import javax.swing.plaf.synth.SynthLookAndFeel;public class MyButton JFrame frame = new JFrame(“Test Buttons“);JButton jButton = new JButton(“JButton“); / 按钮public MyBu
18、tton() frame.setLayout(new FlowLayout();frame.getContentPane().add(jButton);public void show() frame.pack();frame.show();public static void main(String args) MyButton tb = new MyButton();tb.show();SynthLookAndFeel slf = new SynthLookAndFeel();try slf.load(MyButton.class.getResourceAsStream(“mybutton
19、.xml“), MyButton.class);UIManager.setLookAndFeel(slf); catch (Exception e) e.printStackTrace();return;XML code 对我有用8 丢个板砖1 引用 举报 管理 TOP afgasdg (米林) 等 级: #4楼 得分: 0回复于:2011-03-31 23:26:41java访问资源文件Java code import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties;
20、public class PropertyEditor public static void main(String args) throws Exception Properties prop = new Properties();/ 属性集合对象 FileInputStream fis = new FileInputStream(“prop.properties“);/ 属性文件输入流 (相对于根目录下的文件名,要加上包名 “src/prop.properties”)prop.load(fis);/ 将属性文件流装载到Properties对象中 fis.close();/ 关闭流 / 获取
21、属性值,sitename 已在文件中定义 System.out.println(“获取属性值:sitename=“ + prop.getProperty(“sitename“); / 获取属性值,country 未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件 System.out.println(“获取属性值:country=“ + prop.getProperty(“country“, “中国“); / 修改 sitename的属性值 prop.setProperty(“sitename“, “中国“); / 添加一个新的属性 studio prop.setProperty(
22、“studio“, “Boxcode Studio“); / 文件输出流 FileOutputStream fos = new FileOutputStream(“prop.properties“); / 将 Properties集合保存到流中 prop.store(fos, “Copyright (c) Boxcode Studio“); fos.close();/ 关闭流 资源文件sitename=u4E2Du56FDsiteurl= studio=Boxcode Studio 对我有用11 丢个板砖0 引用 举报 管理 TOP #5楼 得分: 0回复于:2011-03-31 23:33:
23、31先收藏先,以后有用得着的地方。 drg202 (drg202) 等 级: 对我有用2 丢个板砖0 引用 举报 管理 TOP afgasdg (米林) #6楼 得分: 0回复于:2011-03-31 23:36:08java日期处理 beanJava code import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calendar; 等 级:import java.util.Date;import java.util.GregorianCalendar;import java.uti
24、l.regex.Pattern;import mons.logging.Log;import mons.logging.LogFactory;public class DateUtil protected static Log logger = LogFactory.getLog(DateUtil.class);/ 格式:年月日 小时:分钟:秒public static final String FORMAT_ONE = “yyyy-MM-dd HH:mm:ss“;/ 格式:年月日 小时:分钟public static final String FORMAT_TWO = “yyyy-MM-dd
25、 HH:mm“;/ 格式:年月日 小时分钟秒public static final String FORMAT_THREE = “yyyyMMdd-HHmmss“;/ 格式:年月日public static final String LONG_DATE_FORMAT = “yyyy-MM-dd“;/ 格式:月日public static final String SHORT_DATE_FORMAT = “MM-dd“;/ 格式:小时:分钟:秒public static final String LONG_TIME_FORMAT = “HH:mm:ss“;/格式:年-月public static
26、 final String MONTG_DATE_FORMAT = “yyyy-MM“;/ 年的加减public static final int SUB_YEAR = Calendar.YEAR;/ 月加减public static final int SUB_MONTH = Calendar.MONTH;/ 天的加减public static final int SUB_DAY = Calendar.DATE;/ 小时的加减public static final int SUB_HOUR = Calendar.HOUR;/ 分钟的加减public static final int SUB_
27、MINUTE = Calendar.MINUTE;/ 秒的加减public static final int SUB_SECOND = Calendar.SECOND;static final String dayNames = “星期日“, “星期一“, “星期二“, “星期三“, “星期四“,“星期五“, “星期六“ ;SuppressWarnings(“unused“)private static final SimpleDateFormat timeFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);public DateUtil(
28、) /* 把符合日期格式的字符串转换为日期类型*/public static java.util.Date stringtoDate(String dateStr, String format) Date d = null;SimpleDateFormat formater = new SimpleDateFormat(format);try formater.setLenient(false);d = formater.parse(dateStr); catch (Exception e) / log.error(e);d = null;return d;/* 把符合日期格式的字符串转换为日
29、期类型*/public static java.util.Date stringtoDate(String dateStr, String format,ParsePosition pos) Date d = null;SimpleDateFormat formater = new SimpleDateFormat(format);try formater.setLenient(false);d = formater.parse(dateStr, pos); catch (Exception e) d = null;return d;/* 把日期转换为字符串*/public static St
30、ring dateToString(java.util.Date date, String format) String result = “;SimpleDateFormat formater = new SimpleDateFormat(format);try result = formater.format(date); catch (Exception e) / log.error(e);return result;/* 获取当前时间的指定格式*/public static String getCurrDate(String format) return dateToString(ne
31、w Date(), format);public static String dateSub(int dateKind, String dateStr, int amount) Date date = stringtoDate(dateStr, FORMAT_ONE);Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(dateKind, amount);return dateToString(calendar.getTime(), FORMAT_ONE);/* 两个日期相减* retur
32、n 相减得到的秒数*/public static long timeSub(String firstTime, String secTime) long first = stringtoDate(firstTime, FORMAT_ONE).getTime();long second = stringtoDate(secTime, FORMAT_ONE).getTime();return (second - first) / 1000;/* 获得某月的天数*/public static int getDaysOfMonth(String year, String month) int days
33、 = 0;if (month.equals(“1“) | month.equals(“3“) | month.equals(“5“)| month.equals(“7“) | month.equals(“8“) | month.equals(“10“)| month.equals(“12“) days = 31; else if (month.equals(“4“) | month.equals(“6“) | month.equals(“9“)| month.equals(“11“) days = 30; else if (Integer.parseInt(year) % 4 = 0 else
34、 days = 28;return days;/* 获取某年某月的天数*/public static int getDaysOfMonth(int year, int month) Calendar calendar = Calendar.getInstance();calendar.set(year, month - 1, 1);return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);/* 获得当前日期*/public static int getToday() Calendar calendar = Calendar.getInsta
35、nce();return calendar.get(Calendar.DATE);/* 获得当前月份*/public static int getToMonth() Calendar calendar = Calendar.getInstance();return calendar.get(Calendar.MONTH) + 1;/* 获得当前年份*/public static int getToYear() Calendar calendar = Calendar.getInstance();return calendar.get(Calendar.YEAR);/* 返回日期的天*/publ
36、ic static int getDay(Date date) Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar.get(Calendar.DATE);/* 返回日期的年*/public static int getYear(Date date) Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar.get(Calendar.YEAR);/* 返回日期的月份,1-12*/p
37、ublic static int getMonth(Date date) Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar.get(Calendar.MONTH) + 1;/* 计算两个日期相差的天数,如果 date2 date1 返回正数,否则返回负数*/public static long dayDiff(Date date1, Date date2) return (date2.getTime() - date1.getTime() / 86400000;/* 比较两个日期的
38、年差*/public static int yearDiff(String before, String after) Date beforeDay = stringtoDate(before, LONG_DATE_FORMAT);Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);return getYear(afterDay) - getYear(beforeDay);/* 比较指定日期与当前日期的差*/public static int yearDiffCurr(String after) Date beforeDay = new
39、Date();Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);return getYear(beforeDay) - getYear(afterDay);/* 比较指定日期与当前日期的差*/public static long dayDiffCurr(String before) Date currDate = DateUtil.stringtoDate(currDay(), LONG_DATE_FORMAT);Date beforeDate = stringtoDate(before, LONG_DATE_FORMAT);retur
40、n (currDate.getTime() - beforeDate.getTime() / 86400000;/* 获取每月的第一周*/public static int getFirstWeekdayOfMonth(int year, int month) Calendar c = Calendar.getInstance();c.setFirstDayOfWeek(Calendar.SATURDAY); / 星期天为第一天c.set(year, month - 1, 1);return c.get(Calendar.DAY_OF_WEEK);/* 获取每月的最后一周*/public st
41、atic int getLastWeekdayOfMonth(int year, int month) Calendar c = Calendar.getInstance();c.setFirstDayOfWeek(Calendar.SATURDAY); / 星期天为第一天c.set(year, month - 1, getDaysOfMonth(year, month);return c.get(Calendar.DAY_OF_WEEK);/* 获得当前日期字符串,格式“yyyy_MM_dd_HH_mm_ss“* * return*/public static String getCurre
42、nt() Calendar cal = Calendar.getInstance();cal.setTime(new Date();int year = cal.get(Calendar.YEAR);int month = cal.get(Calendar.MONTH) + 1;int day = cal.get(Calendar.DAY_OF_MONTH);int hour = cal.get(Calendar.HOUR_OF_DAY);int minute = cal.get(Calendar.MINUTE);int second = cal.get(Calendar.SECOND);St
43、ringBuffer sb = new StringBuffer();sb.append(year).append(“_“).append(StringUtil.addzero(month, 2).append(“_“).append(StringUtil.addzero(day, 2).append(“_“).append(StringUtil.addzero(hour, 2).append(“_“).append(StringUtil.addzero(minute, 2).append(“_“).append(StringUtil.addzero(second, 2);return sb.
44、toString();/* 获得当前日期字符串,格式“yyyy-MM-dd HH:mm:ss“* * return*/public static String getNow() Calendar today = Calendar.getInstance();return dateToString(today.getTime(), FORMAT_ONE);/* 判断日期是否有效,包括闰年的情况* * param date* YYYY-mm-dd* return*/public static boolean isDate(String date) StringBuffer reg = new St
45、ringBuffer(“(d2(02468048)|(1357926)-?(0?“);reg.append(“13578)|(102)-?(0?1-9)|(1-20-9)|(301)“);reg.append(“|(0?469)|(11)-?(0?1-9)|(1-20-9)|(30)|“);reg.append(“(0?2-?(0?1-9)|(1-20-9)|(d2(0246812“);reg.append(“35679)|(1357901345789)-?(0?13578)|(102)“);reg.append(“-?(0?1-9)|(1-20-9)|(301)|(0?469)|(11)“)
46、;reg.append(“-?(0?1-9)|(1-20-9)|(30)|(0?2-?(0?“);reg.append(“1-9)|(10-9)|(20-8)“);Pattern p = Ppile(reg.toString();return p.matcher(date).matches(); 对我有用16 丢个板砖0 引用 举报 管理 TOP afgasdg (米林) 等 级: #7楼 得分: 0回复于:2011-03-31 23:37:13java dos输入输出Java code import java.util.*;public class DaoXu /* param args*/
47、public static void main(String args) / TODO 自动生成方法存根System.out.println(“请输入单个字符并回车: “);Scanner c = new Scanner(System.in);String ch = new String5;for(int i=0; i= 0; j-) System.out.print(chj+“ “); 对我有用5 丢个板砖0 引用 举报 管理 TOP afgasdg (米林) 等 级: #8楼 得分: 0回复于:2011-03-31 23:38:04java获取 ip地址Java code import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.