MS Access DB with Spaces in Column Name

Good Afternoon-

I'm having some serious headaches with a table that I need to extract information from. The powers that be wrote it with the following column names "Contract Number" , "Customer Number", "Description".

Note the spaces in the columns.

Sample code from here: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2

I'm using sample code for connecting to the database and I can access everything using the select statements, except if the column contains a space. I get the following errors:

s.execute("select [Customer Number], Name from Customer");

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

s.execute("select \"Customer Number\", Name from Customer");

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

s.execute("select Customer*Number, Name from Customer");

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.

s.execute("select 'Customer Number' , Name from Customer");

Data from column_name: Customer Number , Unknown

This works, but would require parsing-

s.execute("select * from Customer");

Data from column_name: 5 , Unknown

Suggestions would be truly appreciated.

Jason

[1404 byte] By [purduephotog2a] at [2007-11-27 10:51:18]
# 1

Name might be a keyword. Try putting it in square brackets, too.

%

duffymoa at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

No dice- same error- but thank you!

As an aside, the use of the [name] worked fine for stand alone, so the brackets are being processed correctly.

purduephotog2a at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Is the database Access?

I find that spaces in column and table names are problematic. I avoid them. Can you change it by adding an underscore to make it work?

%

duffymoa at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

Yes, the database is Access. I can't change the spaces to underscores as I would break everyone else's code... and I'm just a peon.

purduephotog2a at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

I found the solution:

Java has this off by default:

s.setEscapeProcessing(true);

http://java.sun.com/j2se/1.3/docs/api/java/sql/Statement.html#setEscapeProcessing(boolean)

Once I enabled it with the above statement, simple brackets around column names with spaces sufficed. IE-

"select [Customer Number] , Name from Customer " ;

returned the correct responses.

Thanks for all the ideas and help!

Jason

purduephotog2a at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

The javadocs say it's turned on by default. Sure that's the only change?

%

duffymoa at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

> "select [Customer Number] , Name from Customer " ;

> returned the correct responses.

Double quoted identifiers, which is part of ANSI SQL should have solved it....

"select \"Customer Number\" , Name from Customer " ;

As mentioned above it has certainly been my experience that DBAs (those with that title) all insist that quoted identifiers should never be used. Thus refactoring the database identifiers is actually the correct solution.

jschella at 2007-7-29 11:30:20 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...