乖,别哭的薄壳
~一份耕耘,一份收获~
posts - 34,comments - 213,trackbacks - 0

读property文件的例子:

package com.test;

import java.io.InputStream;
import java.util.Properties;

/**
 *
 * CopyRight (C) www.blogjava.net/ilovezmh  All rights reserved.<p>
 *
 * WuHan Inpoint Information Technology Development,Inc.<p>
 *
 * Author zhu<p>
 *
 * @version 1.0    2007-2-2
 *
 * <p>Base on : JDK1.5<p>
 *
 */

public class ReadPro {
 
 public String getPara(String fileName) {
  
  Properties prop= new Properties();
  try {
   //ClassLoader cl = this.getClass().getClassLoader(); 
   //InputStream is = cl.getResourceAsStream(fileName);
   InputStream is = getClass().getResourceAsStream(fileName);
   prop.load(is);
   if(is!=null) is.close();
  }
  catch(Exception e) {
   System.out.println(e+"file "+fileName+" not found");
  }
  return prop.getProperty("ip");
 }
 
 public static void main(String[] args) {
  ReadPro pro = new ReadPro();
  System.out.println(pro.getPara("ip.property"));
 }
 
 //-----------------------------------
 //ip.property内容如下:
 //ip:192.168.0.1
 
}

用jdom读xml文件的例子:
(jdom下载)

package com.test;
import java.io.*;
import java.util.*;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;

/**
 *
 * CopyRight (C) www.blogjava.net/ilovezmh All rights reserved.<p>
 *
 * WuHan Inpoint Information Technology Development,Inc.<p>
 *
 * Author zhu<p>
 *
 * @version 1.0    2007-2-1
 *
 * <p>Base on : JDK1.5<p>
 *
 */
 public class XMLReader {
 
  public void createXML(){
  
   Element root=new Element("books");
  
   Document doc=new Document(root);
   Element book1 = new Element("book");
   //Element name1=new Element("name");
   //name1.setAttribute(new Attribute("hot","true"));
   //name1.addContent("程序员");
   //Element price1=new Element("price");
   //price1.addContent("10.00");
   //book1.addContent(name1);
   //book1.addContent(price1); 
  
   Element book2 = new Element("book");
   //Element name2=new Element("name");
   //name2.setAttribute(new Attribute("hot","false"));
   //name2.addContent("读者");
   //Element price2=new Element("price");
   //price2.addContent("3.00");
   //book2.addContent(name2);
   //book2.addContent(price2);
     
   book1.addContent(new Element("name").addContent("程序员").setAttribute("hot","true"));
   book1.addContent(new Element("price").addContent("10.00"));
   book2.addContent(new Element("name").addContent("青年文摘").setAttribute("hot","false"));
   book2.addContent(new Element("price").addContent("3.00"));
   root.addContent(book1);
   root.addContent(book2);
  
   try
   {
    XMLOutputter outer=new XMLOutputter(Format.getPrettyFormat().setEncoding("gb2312"));
    outer.output(doc,new FileOutputStream("book.xml"));
   }catch(IOException e){
    e.printStackTrace();
   }
  }
 
  public void readXML() throws FileNotFoundException, IOException{
  
   Document myDoc=null;
   try
   {
    SAXBuilder sb=new SAXBuilder();
    myDoc=sb.build(new FileInputStream("book.xml"));
   }catch(JDOMException e){
    e.printStackTrace();
   }catch(NullPointerException e){
    e.printStackTrace();
   }
  
   Element root=myDoc.getRootElement(); //先得到根元素
   List books=root.getChildren();//root.getChildren("book"); 
   for (Iterator iter1 = books.iterator();iter1.hasNext(); ) {
       Element book = (Element) iter1.next();
       System.out.println("bookname:"+book.getChild("name").getText());
       System.out.println("hot:"+book.getChild("name").getAttributeValue("hot"));
       System.out.println("price:"+book.getChild("price").getText());
   }
  
  }
  
  public static void main(String args[]) throws FileNotFoundException, IOException {
  
   XMLReader x=new XMLReader();
   x.createXML();
   x.readXML();

 }

}

生成的book.xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<books>
  <book>
    <name hot="true">程序员</name>
    <price>10.00</price>
  </book>
  <book>
    <name hot="false">青年文摘</name>
    <price>3.00</price>
  </book>
</books>

posted on 2007-02-01 17:17 小祝 阅读(3159) 评论(20)  编辑  收藏 所属分类: java技术

FeedBack:
# re: 一个用jdom读写xml文件的简单例子
2007-02-01 22:42 | 施伟
呵呵,8错8错,我正要用这呢
持续关注  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 11:24 | 梅颖
5555....  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 13:04 | 小祝
哭什么呀?谁欺负你了?  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 14:26 | 梅颖
他洒,抢我的位置。。  回复  更多评论
  
# re: 一个用jdom读写xml文件的简单例子
2007-02-02 14:30 | 小祝
呵呵  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-05 19:43 | 睿不可当
呵呵,mark!  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-05 22:24 | 施伟
继续加油啊,更新啊更新啊 呵呵  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 09:29 | 梅颖
发表一点感言好不?  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 11:02 | sinoly
@梅颖
@施伟
你们真是blogjava应该聘请的人才。。。绝对可以支撑一片天空滴说  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-06 11:44 | 小祝
9494
你们太强了。。。  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-07 10:46 | 梅颖
又怎么了啊?呵呵,有人招我们?  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 15:27 | kk
第一个例子根本没用  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 16:21 | 睿不可当
第一个例子根本没用?不是吧!  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 17:33 | 梅颖
人气渐涨啊  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:09 | 睿不可当
我以小祝的
用jdbc操作数据库的简单例子
为例吧
http://www.blogjava.net/ilovezmh/archive/2007/02/01/97247.html  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:10 | 睿不可当
首先建个database.properties

#default
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:test
username=
password=

#Access
Access01_driver=sun.jdbc.odbc.JdbcOdbcDriver
Access01_url=jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\TDdownload\\area\\area.mdb
Access01_username=
Access01_password=

#SQLServer
SqlServer01_driver=net.sourceforge.jtds.jdbc.Driver
SqlServer01_url=jdbc:jtds:sqlserver://localhost:1433/test;SelectMethod=cursor;characterEncoding=GBK;
SqlServer01_username=sa
SqlServer01_password=sa  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:11 | 睿不可当
建个Config类来读取properties 文件

package jdbcSimple;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;

public class Config {

private static String CONFIG_FILENAME = "database.properties";

private static Properties properties = null;

private static final Logger logger = Logger.getLogger(Config.class);

public synchronized static void init() {
if (isInit())
return;
loadProperties();
}

public static String getProperty(String key) {
init();
return properties.getProperty(key);
}

private static String getProperty(String key, String defaultValue) {
init();
return properties.getProperty(key, defaultValue);
}

public static synchronized void setProperty(String key, String value) {
init();
properties.setProperty(key, value);
}

/**
* 检查系统是否已正确配置.
*
* @return boolean return true if system was configured ok
*/
public static boolean isInit() {
return properties != null;
}

/**
* Load the config properties file
*/
private static void loadProperties() {
try {
Properties cfgProp = new Properties();
cfgProp.load(Config.class.getResourceAsStream(CONFIG_FILENAME));
properties = cfgProp;
logger.info("读取系统配置文件正常..");
} catch (Exception e) {
logger.error("配置文件丢失,系统可能无法运行: " + e.toString());
}
}

public static String getFilePath(){
return getProperty("leaveMessageFile");
}
}
  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:13 | 睿不可当
建个ConnectionManager类来创立连接

package jdbcSimple;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.log4j.Logger;

public class ConnectionManager {

private static final Logger logger = Logger
.getLogger(ConnectionManager.class);

//默认情况下
public synchronized static Connection getConnection() {
Connection conn = null;
String JdbcDriver = null;
String JdbcUrl = null;
String UserName = null;
String Password = null;
/* 开始读取配置文件中有关数据库的配置项 */
try {
JdbcDriver = Config.getProperty("driver");
JdbcUrl = Config.getProperty("url");
UserName = Config.getProperty("username");
Password = Config.getProperty("password");
} catch (Exception exception) {
logger.error("读取配置文件时出错:" + exception.toString());
return null;
}
try {
Class.forName(JdbcDriver);
conn = DriverManager.getConnection(JdbcUrl, UserName, Password);
conn.setAutoCommit(true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("应用系统连接数据库异常: " + ex.toString());
return null;
}
return conn;
}

public synchronized static Connection getConnection(String prefix) {
Connection conn = null;
String JdbcDriver = null;
String JdbcUrl = null;
String UserName = null;
String Password = null;
/* 开始读取配置文件中有关数据库的配置项 */
try {
JdbcDriver = Config.getProperty(prefix+"_driver");
JdbcUrl = Config.getProperty(prefix+"_url");
UserName = Config.getProperty(prefix+"_username");
Password = Config.getProperty(prefix+"_password");
} catch (Exception exception) {
logger.error("读取配置文件时出错:" + exception.toString());
return null;
}
try {
Class.forName(JdbcDriver);
conn = DriverManager.getConnection(JdbcUrl, UserName, Password);
conn.setAutoCommit(true);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("应用系统连接数据库异常: " + ex.toString());
return null;
}
return conn;
}

public synchronized static void closeConnection(Connection conn) {
try {
if (conn != null)
conn.close();
} catch (Exception ex) {
logger.error("关闭Connection时异常: " + ex.toString());
} finally {
conn = null;
}
}

public synchronized static void closeStatement(Statement stmt) {
try {
if (stmt != null)
stmt.close();
} catch (Exception ex) {
logger.error("关闭Statement异常: " + ex.toString());
} finally {
stmt = null;
}
}

public synchronized static void closePreparedStatement(
PreparedStatement pstmt) {
try {
if (pstmt != null)
pstmt.close();
} catch (Exception ex) {
logger.error("关闭PreparedStatement异常: " + ex.toString());
} finally {
pstmt = null;
}
}

public synchronized static void closeResultSet(ResultSet rs) {
try {
if (rs != null)
rs.close();
} catch (Exception ex) {
logger.error("关闭ResultSet异常: " + ex.toString());
} finally {
rs = null;
}
}

public synchronized static void closeCallableStatement(
CallableStatement cstmt) {
try {
if (cstmt != null)
cstmt.close();
} catch (Exception ex) {
logger.error("关闭CallableStatement异常: " + ex.toString());
} finally {
cstmt = null;
}
}
}
  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:14 | 睿不可当
City类处理业务(没有考虑事务啊)

package jdbcSimple;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class City {
private class Tbcity {
int code;
String name;
int parentid;
int type;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentid() {
return parentid;
}
public void setParentid(int parentid) {
this.parentid = parentid;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}

public static void main(String arg[]) throws IOException, SQLException {
List list = new ArrayList();
City city = new City();

Connection conn1 = ConnectionManager.getConnection("Access01");
Statement ps = conn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select * from area";
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
Tbcity tbcity = city.new Tbcity();
tbcity.setCode(rs.getInt(2));
tbcity.setName(rs.getString(3));
tbcity.setParentid(rs.getInt(4));
tbcity.setType(rs.getInt(4));
list.add(tbcity);
}

Connection conn2 = ConnectionManager.getConnection("SqlServer01");
sql = "insert into tbcity(code,name,parentid,type) values (?,?,?,?)";
PreparedStatement pstmt = conn2.prepareStatement(sql);
for (Iterator it = list.iterator(); it.hasNext();) {
Tbcity tbcity = (Tbcity) it.next();
pstmt.setInt(1, tbcity.getCode());
pstmt.setString(2, tbcity.getName());
pstmt.setInt(3, tbcity.getParentid());
pstmt.setInt(4, tbcity.getType());
pstmt.addBatch();
}
pstmt.executeBatch();

System.out.println("转换完成!谢谢使用");
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(ps);
ConnectionManager.closePreparedStatement(pstmt);
ConnectionManager.closeConnection(conn1);
ConnectionManager.closeConnection(conn2);
}
}
  回复  更多评论
  
# re: java读配置文件(xml、property)的简单例子
2007-02-08 18:45 | 小祝
谢谢大家支持啊~呵呵。
阿睿你的想法不错,都用类封装了,写的不错。有很多直接用jdbc操作数据库的都是用这样的结构。
我这里写的都是些简单的例子,只是说有时候会很有用的,扩展的话还是靠自己了。算是留个印像在这里了,用的时候也不致于忘记,呵呵。
thank you !
  回复  更多评论
  



标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-05-28 16:45 编辑过
 
 
相关链接:
网站导航: