读取properties数据库配置文件

读取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,很抱歉,我只是为自己看的。

你可能感兴趣的:(sql,jdbc,Microsoft)