【问题标题】:how to read file from web content folder using java for DB connectivity如何使用 Java 从 Web 内容文件夹中读取文件以进行 DB 连接
【发布时间】:2012-04-04 04:54:15
【问题描述】:

txt 文件,这里我将所有 DB 设置称​​为数据库名称、服务器名称、用户名、密码,此文件位于 Web 内容目录 -config.txt 中, 但它无法从 java 类文件中读取。

public 语句 myStatement() 抛出异常 {

    File file=new File("DBSetting.txt");
    FileInputStream fis = null;                                           // declares a file input stream
    //BufferedInputStream bis = null;
    DataInputStream dis = null;
    BufferedReader buffread=null;
    try{
        fis = new FileInputStream(file);                                   // file inputstream opens file
        dis = new DataInputStream(fis);                              // data input stream extracts data from file input stream
        buffread=new BufferedReader(new BufferedReader(new InputStreamReader(dis)));   // bufferedreader reads data from data input
        //dis = new DataInputStream(bis);
        connection=removeSpaces(buffread.readLine());    // reads data from file into variables
        port=removeSpaces(buffread.readLine());
        database=removeSpaces(buffread.readLine());
        username=removeSpaces(buffread.readLine());
        password=removeSpaces(buffread.readLine());

        System.out.println("DB Settings -:Server Name and SQL Port - " +connection+ ", Port - "+port+", DB Name - "+database+", UserName - "+username+" , PWD-"+password);

        //System.out.println(ay[ay.length-1]);
          //System.out.println(connection);
        dis.close();
    }
    catch(Exception ex){System.out.println(ex);}


    Class.forName("com.mysql.jdbc.Driver").newInstance();

    String url = "jdbc:mysql://"+connection + ":" + port + "/"+database;
    System.out.println(url);
    return DriverManager.getConnection(url, username ,password).createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

}

public static String removeSpaces(String s) {                    // removes white spaces present in strings
  StringTokenizer st = new StringTokenizer(s," ",false);
  String t="";
  while (st.hasMoreElements()) t += st.nextElement();
  return t;
}
public static void main(String args[])throws Exception
{
    ConnectionManager con = new ConnectionManager();
    con.myStatement();
}

}

【问题讨论】:

    标签: java mysql jsp


    【解决方案1】:

    很难理解你的问题,但我认为你可以在这里找到你正在寻找的答案: How to load resource from jar file packaged in a war file?

    【讨论】:

      【解决方案2】:

      在 webcontent 目录中?真的吗?这样,最终用户将能够通过 URL 打开文件。不要在公共场合公开这些信息。而是将其放在类路径中。也宁愿让它成为一个普通的.properties file,这样你就不需要自己解析文件了。

      例如db.properties:

      driver = com.mysql.jdbc.Driver
      url = jdbc:mysql://localhost:3306/dbname
      username = foo
      password = bar
      

      然后就可以按如下方式加载和获取数据了:

      Properties properties = new Properties();
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"));
      String driver = properties.getProperty("driver");
      String url = properties.getProperty(url);
      String username = properties.getProperty(username);
      String password = properties.getProperty(password);
      // ...
      

      【讨论】:

        猜你喜欢
        • 2012-01-29
        • 1970-01-01
        • 2012-11-27
        • 2019-08-03
        • 1970-01-01
        • 2019-11-27
        • 2016-03-17
        • 2022-01-08
        • 1970-01-01
        相关资源
        最近更新 更多