Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到文件配置文件异常_Java_File Io - Fatal编程技术网

Java 找不到文件配置文件异常

Java 找不到文件配置文件异常,java,file-io,Java,File Io,我正在创建需要Jdbc连接的web应用程序。我想从当前工作目录下的配置文件中提供db详细信息。为此,我创建了一个ReadConfigFile类。当我单独运行这个类时,它会给我正确的当前工作目录。但是,当我从服务器运行web应用程序时,它会显示不正确的当前工作目录,并引发FileNotFound异常。这是我的代码: private static final String pwd=System.getProperty("user.dir"); private static final File co

我正在创建需要Jdbc连接的web应用程序。我想从当前工作目录下的配置文件中提供db详细信息。为此,我创建了一个ReadConfigFile类。当我单独运行这个类时,它会给我正确的当前工作目录。但是,当我从服务器运行web应用程序时,它会显示不正确的当前工作目录,并引发FileNotFound异常。这是我的代码:

private static final String pwd=System.getProperty("user.dir");
private static final File configFile=new File(pwd+File.separator+"configFile.txt");

private static BufferedReader br;

public static String getDBHost() {
    String dbHost=null;
    try {
        br=new BufferedReader(new FileReader(configFile));
        String st;
        while((st=br.readLine())!=null) {
            if(st.trim().startsWith("DB Host: ")) {
                dbHost=st.trim().substring(9, st.trim().length());
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println(e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println(e);
    } finally {
        try {
            if(br!=null) {
                br.close();
            }               
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        }
    }
    return dbHost;
}

有人能告诉我该怎么做吗?

我建议使用由运行web应用程序的容器处理的连接池。您不必关心连接,容器会为您处理连接。 如果您只需要配置参数,那么web.xml将获取这些参数,并使它们在您的web应用程序中可用

以Tomcat和MySQL为例。 我的数据源的名称将是jdbc/mydb

1) 编辑META-INF目录中的contect.xml文件并添加

      <Resource auth="Container" 
      name="jdbc/mydb" 
      type="javax.sql.DataSource"              
      username="username" password="aC00l6a55w0rd" 
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/DBNAME"/>
关闭所有数据库对象(如ResultSet、PreparedStatement)以及与空闲数据库资源的连接非常重要。事实上,连接管理器并没有真正关闭连接。并将能够将已打开的连接提供给下一个使用者


希望这有帮助

你能告诉我怎么做吗?我已经添加了一些解释和代码片段。希望有帮助。它需要任何外部库吗?FacesContext没有类??您不需要带有FacesContext的行。对不起,是我的错。我从我的一个应用程序中获取了它,在那里我需要facescontext来找出渲染阶段。是的,访问数据库只需要JDBC驱动程序。
   <resource-ref>
    <description>application Database</description>
    <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
 Connection con = null;
 try {
        Context ctx = new InitialContext();
        //FacesContext fctx = FacesContext.getCurrentInstance();
        DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/mydb");
        con = ds.getConnection();

         // do some database things, like read-write-update or whatever

  } catch ( SQLException sqlex ) {
      System.out.println(getClass().getName() + " SQL Exception: " + sqlex.getMessage();
  } catch (NamingException nmex) {
        System.out.println(getClass().getName() + " Nameing Exception: " + nmex.getMessage();
  } finally (
      try {
        if ( con != null ) {
           con.close();
        }
      catch ( SQLException e) {
         System.out.println(getClass().getName() + " closing Exception: " + e.getMessage();
       }
 }