The Artima Developer Community
Sponsored Link

Java Answers Forum
Linking Records to Files on a Server

6 replies on 1 page. Most recent reply: Oct 12, 2004 2:33 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Dar Nessar

Posts: 4
Nickname: dardaman
Registered: Oct, 2004

Linking Records to Files on a Server Posted: Oct 8, 2004 7:18 AM
Reply to this message Reply
Advertisement
I work for an engineering firm and I have a table in my Access 2002 database that holds data regarding engineering drawings (i.e, drawing #, drawing size, project #, etc.).

I have a .PDF file for each drawing and these .PDF files are located on one of the servers at work. Each record in my table (each drawing) has an associated .PDF fle, or each record could have more than one .PDF file associated with it.

I need to link each record in my table (each drawing) to its .PDF file, which is on a server. I've added a 'text' column to my table and I also have the full path to where the actual .PDF files are.

I need help on how I can get the .PDF file name for each record and insert it into my table.


Can anyone please help????


Thanks in advance.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Linking Records to Files on a Server Posted: Oct 8, 2004 8:39 AM
Reply to this message Reply
Your problem is to find the corresponding pdf for each record?

If so:
You can't expect a computer to do magic or reading minds.
You just have to type in the url's. If you have a name convention for the pdf files, you can write an angorithm based on it.




If your problem is to write the data to the database, you can use an jdbc driver like jadozoom (30 days free trial).




For the records with more than one pdf you'll have a problem if the text field can contain only one value. Than you have add another table for a 1:n relationship.
If the field can contain more than one path, just use a separator char wich can't be used by your file system.

Dar Nessar

Posts: 4
Nickname: dardaman
Registered: Oct, 2004

Re: Linking Records to Files on a Server Posted: Oct 8, 2004 12:31 PM
Reply to this message Reply
I have over 100,000 records and so manually typing in the path to where each file is will not be feasible.

Is there anyway through code (Java or some other language) that I can grab the file name from the server for each record and insert it in the database? If there is, how would the code be? I realize I sound incompetent, but I've never done anything like this before.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Linking Records to Files on a Server Posted: Oct 9, 2004 2:22 AM
Reply to this message Reply
This has nothing to do with incompetence, I just don't understand what your problem is.

Do you have a name convention: What is the logical connection between record and filename?

If you would do it by hand, how would you decide wich filename applies to wich record?

What do you mean by "grabbing" a filename?

Do you need help with the syntax or the algorithm?

Dar Nessar

Posts: 4
Nickname: dardaman
Registered: Oct, 2004

Re: Linking Records to Files on a Server Posted: Oct 11, 2004 6:26 AM
Reply to this message Reply
The connection between a record and its file is that each record (each engineering drawing data) has it's associated electronic file in a .PDF format. So for example, if there's a record, let's say drawing number 123, there's a .PDF file for it on the server called 123.PDF.

When I say I need to grab the file name, I need to actually get the text '123.PDF' and insert it into a column in the table for the record that has 123 as the drawing number.

So for example, I have a table that has data about all engineering drawings. One of the columns in this table is called 'pathToFile'. I have a folder on a server that has all of the electronic files for each actual drawing in a folder. I need help with syntax and algorithm to write code to loop through each record in my table, get the drawing number and based on the drawing number, go to the folder on the server and get it's .PDF file and insert in into my 'pathToFile' column. So for the example above, if the drawing number is 123, then the entry in my 'pathToFile' column for that record would be '\\server_name\folder_name\123.pdf'.

I hope this makes sense.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Linking Records to Files on a Server Posted: Oct 12, 2004 1:13 AM
Reply to this message Reply
Suppose your table is called DRAWINGS, with a column called DRAWINGNUMBER.

You can write a simple SQL statement to update the PATHTOFILE column:

UPDATE DRAWINGS SET PATHTOFILE='\\server_name\folder_name\' + DRAWINGNUMBER + '.pdf'

You might need to escape the backslashes; I forget how Access handles that. That is, you may have to convert '\\' to '\\\\', and '\' to '\\'.

This doesn't help if--as described in your original post--there is more than one file per drawing. It also doesn't do anything to verify that the file actually exists.

By the way, I don't think you should consider Java for any part of this solution. The Access 2002 and/or VBA Developer's Handbook will have everything you need in it. If the firm you're working for expects you to jump into this stuff, then they should be willing to spend the $99 for reference material.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Linking Records to Files on a Server Posted: Oct 12, 2004 2:33 AM
Reply to this message Reply
Assuming that everyone of your records has exactly one pdf file, you could use a simple SQL command in Access:

UPDATE [engineering drawings] SET [engineering drawings].[pathToFile] = "\\server_name\folder_name\" & [drawing number] & ".pdf";
Just open your db in Access and create a new Query. Than select the SQL-View mode and copy this command there.




But ... you said every record can have multiple files.
So we have to do it the hard way.

The following program creates a list of all existing files following certain patterns ("123.pdf", "...123a.pdf", "...123b.pdf") and puts "; " between them ("file1; file2; file3").

Create a class "UpdateProgram" and copy the folling source code into it.

Than you need a JDBC/Access driver to create the connection.


import java.io.File;
import java.sql.*;
import java.util.Vector;

public class UpdateProgram {
private static void doTheUpdate() {
//You have to create a Connection to your Database here
//I use Jadozoom (IZMADO), maybe ther e are better JDBC/Access drivers around
Connection myConnection = .......

String sqlString = "SELECT [pathToFile] FROM [engineering drawings]";
boolean statementOk = false;
try {
Statement stmt = myConnection.createStatement();
statementOk = true;
ResultSet rs = stmt.executeQuery(sqlString);
final String path = "\\\\server_name\\folder_name\\";
String[] pattern = new String[] {".pdf", "a.pdf", "b.pdf"};
Vector v = new Vector();
while (rs.next()) {
String recordID = rs.getString("drawing number");
boolean fileFound = false;
if (recordID != null && recordID.length() > 0) {
//Every "line" contains the id in String format, then a list of all existing files.
Vector line = new Vector();
line.add(recordID);
for (int i = 0; i < pattern.length; i++) {
String fileName = path + recordID + pattern[i];
File pdfFile = new File (fileName);
if (pdfFile.exists()) {
fileFound = true;
line.add(fileName);
}
if (fileFound)
v.add(line);
}
}
}
rs.close();
rs = null;
// I splitted this two parts because my Access-JDBC driver supports only on open connection and only one statement at the same time
myConnection.setAutoCommit (false);
for (int i = 0; i < v.size(); i++) {
Vector line = (Vector)v.get(i);
String id = (String)line.firstElement();
String fileList = (String)line.get(1);
for (int j = 2; j < line.size(); j++)
fileList+= "; " + (String)line.get(j);
sqlString = "UPDATE [engineering drawings] SET [engineering drawings].[pathToFile] = \"" + fileList + "\" WHERE [drawing number] = " + id;
//I assume, that the id is a integer. If it is a String, then use ... WHERE [drawing number] = \"" + id + "\"";
stmt.executeUpdate(sqlString);
}
myConnection.commit();
stmt.close();
stmt = null;
myConnection.close();
myConnection = null;
} catch (SQLException e) {
//You land here if there was a error with the connection, statement or the sql string.
System.out.println (e.getMessage());
if (statementOk)
System.out.println ("lastSQLString used: " + sqlString);
else
System.out.println ("Coudln't even create the statement. Is the connection ok?");
}
}

public static void main(String[] args) {
doTheUpdate();
}
}


I sure hope I didn't make too many errors.


Someone knows a free Access JDBC driver? Jadozoom is free for 30 days, but there must be some alternative to it.

Flat View: This topic has 6 replies on 1 page
Topic: Performance comparison between methods of TreeMap Previous Topic   Next Topic Topic: FAT filestore implementation in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use