论坛首页 入门技术论坛

读取properties数据库配置文件

浏览 1952 次
该帖已经被评为新手帖
作者 正文
   发表时间:2009-06-18   最后修改:2009-06-19
读取properties配置文件:
java如下:
public class TestJdbc {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	
		Properties serverJdbc=new Properties();   
        InputStream inputStream=new FileInputStream("src/jdbc.properties");   
        serverJdbc.load(inputStream);   
        //System.out.println(serverJdbc.getProperty("jdbc.url"));  
        
		String sql="select * from oa.dbo.staffnfo";
		
        try {
        	String drviver=serverJdbc.getProperty("jdbc.driver");
        	String url=serverJdbc.getProperty("jdbc.url");
        	String user=serverJdbc.getProperty("jdbc.username");
        	String pwd=serverJdbc.getProperty("jdbc.password");
          
          Class.forName(drviver);
          Connection conn=DriverManager.getConnection(url,user,pwd);
          Statement stm=conn.createStatement();
          ResultSet rs=stm.executeQuery(sql);
          while(rs.next()){
        	  System.out.println(rs.getString("empCode"));
          }    
          rs.close();
          stm.close();
          conn.close();
      } catch (Exception e) {
           e.printStackTrace();
           System.out.println(e);
      }
	
	}

}


jdbc.properties如下:
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://192.168.212.8
jdbc.username=sa
jdbc.password=sa


sorry,很抱歉,我只是为自己看的。
   发表时间:2009-06-18  
看完了,但是不是知道想要表达什么意思,楼主能不能解释一下...
Class.forName(drviver).newInstance();这一句好像多了..
只要Class.forName(driver)就可以了...
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics