Properties 类型未定义方法 getProperty(String)

The method getProperty(String) is undefined for the type Properties

我有 DBUtil class,其中包含数据库配置。我有 属性 包含数据库详细信息的文件。我正在尝试使用此 class 加载 属性 文件,但我在 load() 方法上收到错误提示 The method getProperty(String) is undefined for the type Properties。我真的不知道怎么了。

DBUtil class

package com.varun.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.arjuna.ats.internal.arjuna.recovery.Connection;
import com.sun.xml.fastinfoset.sax.Properties;

public class DbUtil {
 
 private static Connection connection = null;
 public static Connection getConnection(){
  if(connection!=null)
  {
   return connection;
  }
  else
  {
   try{
    
    Properties prop=new Properties();
    InputStream inputStream=DbUtil.class.getClassLoader().getResourceAsStream("/db.properties");
    prop.load(inputStream); // The method load(InputStream) is undefined for the type Properties
    String driver =  prop.getProp("");//The method getProp(String) is undefined for the type Properties
                String url = prop.getProperty("url");//The method getProperty(String) is undefined for the type Properties
                String user = prop.getProperty("user"); //The method getProperty(String) is undefined for the type Properties
                String password = prop.getProperty("password"); //The method getProperty(String) is undefined for the type Properties
                Class.forName(driver);
                connection = (Connection) DriverManager.getConnection(url, user, password);
    
   }catch(ClassNotFoundException e)
   {
     e.printStackTrace();
   }
   catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
   return connection;
  }
 }

}

我已将错误与该行一起注释掉。

您需要 import java.util.Properties; 并且导入错误 class - com.sun.xml.fastinfoset.sax.Properties

class 名称相同,但它们在不同的包中。您可能使用了一些自动执行导入的 IDE,但它导入了错误的类型。

我怀疑你输入错了Properties

尝试换出

import com.sun.xml.fastinfoset.sax.Properties;

为了

import java.util.Properties;

替换导入行:

import com.sun.xml.fastinfoset.sax.Properties;

与:

import java.util.Properties;

更改导入而不是 import com.sun.xml.fastinfoset.sax.Properties 使用 import java.util.Properties