Solved

Select Query of Access database

Posted on 2011-03-22
11
686 Views
Last Modified: 2012-05-11
I have developed an application in java with Access database.
In that I am using select query as

ResultSet rs = stmt.executeQuery("select stestid,mtestid,stestname,"+docid+" from Subtest");

Here I have table 'subtest' which contains columns stestid,mtestid,stestname,1,2,3,4,5,6,7,8,9,10,11

I want to select value from one of the column 1,2,3,4,5,6,7,8,9,10,11 dynamically. Thats why I took variable 'docid' for same.
But if I take docid=1 or anything between 1,2,3,4,5,6,7,8,9,10,11  I get wrong result.
e.g. If I take docid=5 then it results '5' insteade of value from table '5'. Please help

____
Added MS Access and SQL Syntax zones
mbizup
Access Zone Advisor
0
Comment
Question by:Aurii
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35188821
Very strange column names - asking for trouble. You might try


ResultSet rs = stmt.executeQuery("select stestid,mtestid,stestname,"\"" + docid + "\"" from Subtest");

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35188829
0
 
LVL 61

Expert Comment

by:mbizup
ID: 35188877
With those numeric column "names", you need to enclose the field name (a number) in brackets, otherwise wou will simply return the number from the query.

Anytime you have unusual column names (numbers, special characters, spaces, etc), you need to use brackets to define it as a column name:

Try this:

ResultSet rs = stmt.executeQuery("select stestid,mtestid,stestname,["+docid+"] from Subtest");
0
 

Author Comment

by:Aurii
ID: 35189894
None of the solutions are working
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35189943
have you tried prepared statement?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 61

Expert Comment

by:mbizup
ID: 35190166
What are the exact issues you are having with each of the suggestions?
0
 
LVL 10

Expert Comment

by:Luke Chung
ID: 35191185
Sounds like a data normalization problem for data stored in a spreadsheet format.

Can the data be transformed so they reside in one column rather than all those columns?  A select query on one field would be very fast and scalable.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35194338

perhaps you can try to select it this way:

select Subtest.stestid, Subtest.mtestid,Subtest.stestname, Subtest.1, Substest.2,... from Subtest

and also you can try with aliases for the fields

select Subtest.stestid, Subtest.mtestid,Subtest.stestname, Subtest.1 as column_one, Substest.2 as column_two,... from Subtest

0
 
LVL 92

Expert Comment

by:objects
ID: 35194950
Agree with the suggestion to use a PreparedStatement, handles escaping and protects you from sql injection. But in your case you unfortunately can't.

what was the result of mbizup suggestion?
0
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 35196214
Use "select * from Subtest" in a statement and Use ResultSetMetaData to get the value for a specific column.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35196353
That's unnecessary and will simply result in your db doing more work than necessary
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now