`
中华国锋
  • 浏览: 41033 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java源代码实例

阅读更多

java访问xml文件

Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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 element=null; File f =new File("a.xml"); DocumentBuilder db=null; //documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件) DocumentBuilderFactory dbf=null; try{ dbf= DocumentBuilderFactory.newInstance(); //返回documentBuilderFactory对象 db =dbf.newDocumentBuilder();//返回db对象用documentBuilderFatory对象获得返回documentBuildr对象 Document dt= db.parse(f); //得到一个DOM并返回给document对象 element = dt.getDocumentElement();//得到一个elment根元素 System.out.println("根元素:"+element.getNodeName()); //获得根节点 NodeList childNodes =element.getChildNodes() ; // 获得根元素下的子节点 for (int i = 0; i < childNodes.getLength(); i++) // 遍历这些子节点 { Node node1 = childNodes.item(i); // childNodes.item(i); 获得每个对应位置i的结点 if ("Account".equals(node1.getNodeName())) { // 如果节点的名称为"Account",则输出Account元素属性type System.out.println("\r\n找到一篇账号. 所属区域: " + node1.getAttributes().getNamedItem ("type").getNodeValue() + ". "); NodeList nodeDetail = node1.getChildNodes(); // 获得<Accounts>下的节点 for (int j = 0; j < nodeDetail.getLength(); j++) { // 遍历<Accounts>下的节点 Node detail = nodeDetail.item(j); // 获得<Accounts>元素每一个节点 if ("code".equals(detail.getNodeName())) // 输出code System.out.println("卡号: " + detail.getTextContent()); else if ("pass".equals(detail.getNodeName())) // 输出pass System.out.println("密码: " + detail.getTextContent()); else if ("name".equals(detail.getNodeName())) // 输出name System.out.println("姓名: " + detail.getTextContent()); else if ("money".equals(detail.getNodeName())) // 输出money System.out.println("余额: "+ detail.getTextContent()); } } } } catch(Exception e){System.out.println(e);} }
XML code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?xml version="1.0" encoding="gbk"?> <Accounts> <Account type="by0003"> <code>100001</code> <pass>123</pass> <name>李四</name> <money>1000000.00</money> </Account> <Account type="hz0001"> <code>100002</code> <pass>123</pass> <name>张三</name> <money>1000.00</money> </Account> </Accounts>
java jdbc数据库连接
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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 Statement 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 = "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/" + propertyFileName); 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 { Class.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.forName(dbClassName);// 1.注册驱动 conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//2.建立与数据库的链接 } catch (Exception ee) { ee.printStackTrace(); } if (conn == null) { System.err .println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + dbClassName + "\r\n链接位置:" + dbUrl + "\r\n用户/密码" + dbUser + "/" + dbPwd); } return conn; } /* * 功能:执行查询语句 */ public ResultSet executeQuery(String sql) { try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,//3.创建语句 ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql);//4.执行查询 } catch (SQLException ex) { System.err.println(ex.getMessage()); // 输出异常信息 } return rs; // 返回结果集对象 5.结果处理 } /* * 功能:执行更新操作 */ public int executeUpdate(String sql) { int result = 0; // 定义保存返回值的变量 try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); result = stmt.executeUpdate(sql); // 执行更新操作 } catch (SQLException ex) { result = 0; // 将保存返回值的变量赋值为0 } return result; // 返回保存返回值的变量 } /* * 功能:关闭数据库的连接 */ public void close() {//6.释放资源 try { // 捕捉异常 try { if (rs != null) { // 当ResultSet对象的实例rs不为空时 rs.close(); // 关闭ResultSet对象 } } finally { try { if (stmt != null) { // 当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.SQLServerDriver
dbClassName2=com.mysql.jdbc.Driver
dbPwd=sa
dbPwd2=root
dbUrl=jdbc\:microsoft\:sqlserver\://localhost\:1433;DatabaseName\=DB_ATM
dbUrl2=jdbc\:mysql\://localhost\:3306/db_atm
dbUser=sa
dbUser2=root
java自定义按钮外观
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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 MyButton() { 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.xml"), MyButton.class); UIManager.setLookAndFeel(slf); } catch (Exception e) { e.printStackTrace(); return; } } }

XML code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <synth> <style id="mybutton"> <state> <imagePainter method="buttonBackground" path="mybutton.png" sourceInsets="3 6 12 20" paintCenter="true" stretch="true"/> <insets top="3" left="6" bottom="12" right="20"/> <font name="Aharoni" size="16"/> </state> <property key="Button.margin" type="insets" value="0 0 5 8"/> </style> <bind style="mybutton" type="region" key="Button"/> </synth>
java访问资源文件
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; 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();// 关闭流 // 获取属性值,sitename已在文件中定义 System.out.println("获取属性值:sitename=" + prop.getProperty("sitename")); // 获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件 System.out.println("获取属性值:country=" + prop.getProperty("country", "中国")); // 修改sitename的属性值 prop.setProperty("sitename", "中国"); // 添加一个新的属性studio prop.setProperty("studio", "Boxcode Studio"); // 文件输出流 FileOutputStream fos = new FileOutputStream("prop.properties"); // 将Properties集合保存到流中 prop.store(fos, "Copyright (c) Boxcode Studio"); fos.close();// 关闭流 } }


资源文件

sitename=\u4E2D\u56FD
siteurl=www.abcjava.com  
studio=Boxcode Studiojava日期处理bean
Java code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.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 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 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_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() { } /** * 把符合日期格式的字符串转换为日期类型 */ 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; } /** * 把符合日期格式的字符串转换为日期类型 <
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics