Solved

Select Query of Access database

Posted on 2011-03-22
11
685 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
Comment Utility
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
Comment Utility
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
None of the solutions are working
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
have you tried prepared statement?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 61

Expert Comment

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

Expert Comment

by:LukeChung-FMS
Comment Utility
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
Comment Utility

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
Comment Utility
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 20

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
Comment Utility
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
Comment Utility
That's unnecessary and will simply result in your db doing more work than necessary
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

18 Experts available now in Live!

Get 1:1 Help Now