DB.DRIVER=com.mysql.jdbc.Driver
DB.CONNURL=jdbc:mysql://192.168.1.11:3306/dxt?useUnicode=true&characterEncoding=utf-8
DB.USERNAME=root
DB.PASSWORD=pass
DB.MAXIMUMACTIVECONNECTIONS=10
DB.MAXIMUMIDLECONNECTIONS=5
DB.MAXIMUMWAIT=60000以上是个配置文件properties.
用java怎样读取以上配置  得到一个 DataSource ,,关键是不知道 怎样得到 DataSource ,现在不要jndi。

解决方案 »

  1.   

      1。使用java.util.Properties类的load()方法示例:InputStreamin=lnewBufferedInputStream(newFileInputStream(name));Propertiesp=newProperties();p.load(in);  2。使用java.util.ResourceBundle类的getBundle()方法示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());  3。使用java.util.PropertyResourceBundle类的构造函数示例:InputStreamin=newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);  4。使用class变量的getResourceAsStream()方法示例:InputStreamin=JProperties.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);  5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法示例:InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);Propertiesp=newProperties();p.load(in);  6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);  补充  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例:InputStreamin=context.getResourceAsStream(path);Propertiesp=newProperties();p.load(in); 
      

  2.   

    一般来说可以这样读取一个属性文件,这个例子的helloworld.properties属性文件里内容是这样的
    helloworld = "Hello World!"
    String propfilename = "helloworld.properties";
    Properties properties = new Properties();
    InputStream is = getClass().getClassLoader().getResourceAsStream(
                     propfilename);
    properties.load(is);
    is.close();
    helloworld = properties.getProperty("helloworld");
    获得数据源一般来说可以这样获得InitialContext initCtx = new InitialContext();
    DataSource ds = (DataSource) initCtx.lookup(strJNDIName);
      

  3.   

    Java有很多开源的连接池实现,如:Jakarta DBCP、DBPool等等,你可以上网查查他们的用法。
      

  4.   

    import org.apache.commons.dbcp.BasicDataSource;
    import org.apache.commons.dbcp.BasicDataSourceFactory;import java.sql.SQLException;
    import java.sql.Connection;
    import java.util.Properties;public class ConnectionSource {
        private static BasicDataSource dataSource = null;    public ConnectionSource() {
        }    public static void init() {        if (dataSource != null) {
                try {
                    dataSource.close();
                } catch (Exception e) {
                    //
                }
                dataSource = null;
            }        try {
                Properties p = new Properties();
                p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
                p.setProperty("url", "jdbc:oracle:thin:@192.168.0.1:1521:testDB");
                p.setProperty("password", "scott");
                p.setProperty("username", "tiger");
                p.setProperty("maxActive", "30");
                p.setProperty("maxIdle", "10");
                p.setProperty("maxWait", "1000");
                p.setProperty("removeAbandoned", "false");
                p.setProperty("removeAbandonedTimeout", "120");
                p.setProperty("testOnBorrow", "true");
                p.setProperty("logAbandoned", "true");            dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);        } catch (Exception e) {
                //
            }
        }
        public static synchronized Connection getConnection() throws  SQLException {
            if (dataSource == null) {
                init();
            }
            Connection conn = null;
            if (dataSource != null) {
                conn = dataSource.getConnection();
            }
            return conn;
        }
    }
      

  5.   

    private Properties props=new Properties();
    props.load(new FileInputStream("DataSource.properties");
    String url=props.getProperty("DB.CONNURL");
    url+=";username="+props.getProperty("DB.USERNAME")+";password="+props.getProperty("DB.PASSWORD");
    Hashtable env=new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.RegistryContextFactory");
    env.put(Context.PROVIDER_URL,url);
    InitialContext ctx=new InitialContext(env);
    DataSource ds=(DataSource)ctx.lookup("strJNDIName");
      

  6.   

    看看这个方法思路:
    首先,你可以把你的那些定义到一个属性文件中的,假设是db.peoperties.然后,你得想办法读取这个属性文件了.
    public class Resouse { public static final String resource = "db.properties";//这里定义下面要区那找文件的,下面就是个文件操作流的问题了
    Properties pr = new Properties();
    public Resouse() {
    try {
    InputStream is = null;
    is = this.getClass().getResourceAsStream(resource);
    pr.load(is);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    } public String getString(String str) {
    return pr.getProperty(str);
    }}
    第三:得到相关设定的参数了.
    public Connection getConnection(){
        Resouse re=new Resouse();
         try {
           Class.forName(re.getString("driver"));
           con=DriverManager.getConnection(re.getString("url"),re.getString("name"),re.getString("password"));
         }
         catch (Exception ex) {
           ex.printStackTrace();
         }
          return con;
    }