#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Jersey
    Posts
    100
    Rep Power
    12

    Getting Paths of Files, Folders, Databases


    In my Java program, I would like to be able to get the absolute path name of the current file, file contents of folders, and relative path name for a database. I know that Java API java.io.File has information but I don’t know the proper syntax to use.
    ************************
    ASP Example for getting absolute path

    strPathInfo = Request.ServerVariables(“SCRIPT_NAME”)
    strPhysicalPath = Server.MapPath(strPathInfo)

    ******************************
    ASP Example for getting folder contents

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objFile = objFSO.GetFile(strPhysicalPath)

    Set objFolder = objFile.ParentFolder
    Set objFolderContents = objFolder.Files

    For each objFileItem In objFolderContents

    Response.Write objFileItem.Name
    Response.Write objFileItem.Size
    Response.Write objFileItem.DateLastModified

    Next
    ****************************
    ASP Example for Accessing Database

    Set objConn=Server.CreateObject("ADODB.Connection")

    objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source =" &_
    Server.mappath("databases\ex_database.mdb")
    ***************************
    Can you provide some assistance?

    Robin
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    There are several methods that let you get pieces of information about the path, unfortunately they are hard to remember what each one does. This page has some of the standard CGI variables and their equivelent method calls in java. You'll notice that SCRIPT_NAME is listed. That is the one you want.

    What you then use is the getServletContext().getRealPath() method to get the file system path of that file. From there you can use the File object.
    Code:
    *****************
    JSP example of getting absolute path
    
    String jsp_path = request.getServletPath();
    String real_path = getServletConfig().getServletContext().getRealPath(jsp_path);
    
    *****************
    JSP example for getting folder contents
    
    <% 
    	String path = request.getServletPath();
    	String real_path = getServletConfig().getServletContext().getRealPath(path);
    	File f = new File(real_path);
    	File folder = f.getParentFile();
    	File[] files = folder.listFiles(); %>
    	<table border=1>
    		<tr>
    			<td>
    				File Name
    			</td>
    			<td>
    				Size
    			</td>
    			<td>
    				Last Modified
    			</td>
    		</tr>
    	<%	for (int i = 0; i < files.length; i++) { %>
    		<tr>
    			<td>
    				<%= files[i].getName() %>
    			</td>
    			<td>
    				<%= files[i].length() %>
    			</td>
    			<td>
    				<%= new Date(files[i].lastModified()) %>
    			</td>
    		</tr>
    	<% } %>
    	</table>
    As for the JDBC database connection, here is some code I cobbed from some guys site that had a ready example, with some comments added by me:
    Code:
            //this is the url to your database
            String url="jdbc:z1MySQL://luna.oit.unc.edu/CES";
            //Connection object to the database
            Connection con;
            String query = "SELECT * FROM alex_course";
            //A statement is what does the querying of the database
            Statement stmt;
    
            try {
                //load the class file into memory
                Class.forName("twz1.jdbc.mysql.jdbcMysqlDriver");
            }
            catch (java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }
    
            try {
                // get the actual connection
                con = DriverManager.getConnection (url, "username", "password");
                //create the statement from the connection
                stmt = con.createStatement();
                //execute your query
                ResultSet result = stmt.executeQuery(query);
                //get your data
                while (result.next()) {
                    String name = result.getString(1) + " " + result.getString(2);
                    System.out.println(name);
                }
                //close your resources
                result.close();
                stmt.close();
                con.close();
            }
            catch(SQLException ex) {
                System.err.print("SQLException: ");
                System.err.println(ex.getMessage());
            }
    Hope this helps.

    JDBC tutorial

IMN logo majestic logo threadwatch logo seochat tools logo