Java Forum / Databases / March 2007
Two of three SQL stmts execute: third results in table does not exist exception
GGP - 16 Mar 2007 07:10 GMT I attempt to collect data from a database three times in one method, all using one connection. The first two queries execute without any problem. However, the third fires an sql exception:
java.sql.SQLSyntaxErrorException: Table/View 'TABLENAME' does not exist.
I connect to the database using a connection class, as follows:
public class MyDBConnection {
public static final String DRIVER_NAME = "org.apache.derby.jdbc.ClientDriver"; public static final String DATABASE_URL = "jdbc:derby://localhost: 1527/cfdb"; public Connection myConnection;
public MyDBConnection() {}
public void init() { Class.forName(DRIVER_NAME); myConnection = DriverManager.getConnection(DATABASE_URL,"usnm","pswd"); } ... [getters and setters deleted]
I then query the database from within a UI constructor like this:
public class MainUCInterface extends javax.swing.JFrame {
MyDBConnection myConnection = new MyDBConnection(); Connection con = null; Statement stmt = null; ResultSet rs = null;
public MainUCInterface() throws SQLException { initComponents();
myConnection.init(); con = myConnection.getMyConnection();
stmt = con.createStatement(); rs = stmt.executeQuery("select CONTYPEID, CONTYPENAME from CONTYPES order by CONTYPENAME"); //This query executes fine
Statement stmt2 = null; ResultSet rs2 = null; stmt2 = con.createStatement(); rs2 = stmt2.executeQuery("select CONTYPEID, CONTYPENAME from CONTYPES where CONTYPEID = " + iConvTyp); //iConvTyp is an integer, and this query also executes fine.
ResultSet rs3 = null; Statement stmt3 = null; stmt3 = con.createStatement(); String sqlListData = "select " + unitID + ", " + unitName + " from " + tableName; rs3 = stmt3.executeQuery(sqlListData); //This is where the error occurs. All the strings in stmt3 are fine (correct spelling, etc.)
A couple of additional notes:
1. The first two queries are on the same table. I tried reconstructing the third table, and I just finished rebuilding the whole database from scratch--it didn't help.
2. There is no relational structure to the database. It's just a group of tables that hold data I need to retrieve to provide functionality to the app. I don't know if this is a relevant point or not.
3. Metadata on the database indicates that there are no tables present (which would account for the error, but not the successful queries). I'm not sure why con.getMetaData isn't seeing my tables.
Any ideas? I really appreciate the help.
Greg.
David Harper - 16 Mar 2007 08:09 GMT > I attempt to collect data from a database three times in one method, > all using one connection. The first two queries execute without any [quoted text clipped - 4 lines] > > I connect to the database using a connection class, as follows: [SNIP]
> ResultSet rs3 = null; > Statement stmt3 = null; [quoted text clipped - 4 lines] > error occurs. All the strings in stmt3 are fine (correct spelling, > etc.) [SNIP]
> Any ideas? I really appreciate the help. You do not show us how the variable "tableName" has been defined, so it is almost impossible to offer advice.
However, I'm guessing that somewhere in your code, you have set
String tableName = "TABLENAME";
Perhaps you intended to set the correct value elsewhere in your code, but you forgot to do so. Since we can't see the rest of your class definition, it's impossible to say for sure.
David Harper Cambridge, England
GGP - 16 Mar 2007 13:07 GMT > > I attempt to collect data from a database three times in one method, > > all using one connection. The first two queries execute without any [quoted text clipped - 31 lines] > David Harper > Cambridge, England David,
Thanks for the reply. Yes, I do set TABLENAME almost as you suggest. The difference is that I have to construct the name by extracting the root from one of the previous two queries and concatenating a suffix. For example, all my table names end with "CF" and one of the tables may relate to 'mass' (info extracted from one of the other queries). In that case, TABLENAME would actually be "MassCF". The variable definition is there in the code, and is:
String TABLENAME = strRoot + "CF";
The actual table "MassCF" does exist and spelling is correct (in both the code and actual table names). During the debugging process, I checked to see if names were being generated properly, and they appear to be fine (which is why I suggested that the first two queries worked). I'd be happy to provide the full class definition if you think it would help.
Thanks again for your help,
Greg.
David Harper - 16 Mar 2007 20:12 GMT [SNIP]
> Thanks for the reply. Yes, I do set TABLENAME almost as you suggest. > The difference is that I have to construct the name by extracting the [quoted text clipped - 12 lines] > worked). I'd be happy to provide the full class definition if you > think it would help. Is it possible that your database system uses table names that are case-sensitive, and it's failing to match your constructed table name "MassCF" with the actual name "MASSCF"?
David Harper Cambridge, England
GGP - 16 Mar 2007 23:13 GMT > [SNIP] > [quoted text clipped - 21 lines] > David Harper > Cambridge, England Interesting. I also thought of that, but I don't think it's the case. In fact, the table names are in the format MassCF, DistanceCF, etc., and when the queries spit back their results, they're in the format MASSCF, DISTANCECF, etc. I checked the documentation that's supplied with the database (http://db.apache.org/derby/docs/10.1/ ref/), and it says in there that SQL statements (and output, apparently) are not case sensitive.
I also tested your suggestion directly by trying to run the query using variable names in allcaps. I received the same error (i.e., it doesn't seem to matter that the SQL parameters are all upper-case, lower-case, or mixed-case, the same error occurs).
Thanks again for taking the time to think about this. I'm stumped, too.
Greg.
Lew - 17 Mar 2007 02:50 GMT > java.sql.SQLSyntaxErrorException: Table/View 'TABLENAME' does not > exist. How come the error doesn't say "Table/View 'MASSCF' does not exist."?
David Harper <devn...@obliquity.u-net.com> wrote:
> However, I'm guessing that somewhere in your code, you have set > > String tableName = "TABLENAME"; > > Perhaps you intended to set the correct value elsewhere in your code, but you forgot to do so. Since we can't see the rest of your class definition, it's impossible to say for sure. David's points remain unaddressed.
-- Lew
Lew - 17 Mar 2007 03:50 GMT From comp.lang.java.databases:
GGP wrote:
>> java.sql.SQLSyntaxErrorException: Table/View 'TABLENAME' does not >> exist.
> How come the error doesn't say "Table/View 'MASSCF' does not exist."? David Harper <devn...@obliquity.u-net.com> wrote:
>> However, I'm guessing that somewhere in your code, you have set >> [quoted text clipped - 3 lines] >> but you forgot to do so. Since we can't see the rest of your class >> definition, it's impossible to say for sure.
> David's points remain unaddressed. And the folks in the other groups to whom you multiposted deserve to know about it.
f-u set to comp.lang.java.help
-- Lew
GGP - 18 Mar 2007 13:59 GMT > From comp.lang.java.databases: > [quoted text clipped - 18 lines] > > -- Lew Thanks, Lew. You're right, I should have kept everyone up to speed (I'm quite new to this, so please forgive me). So, since I've asked a confusing question, I'll try to clarify here. The code has changed a little since I originally asked the question, so I've supplied the entire class definition below (warts and all).
public class ListHandler {
int iConvTyp;
/** Creates a new instance of ListHandler */ public ListHandler(int conversionTypeSelectedID) { int iConvTyp = conversionTypeSelectedID; }
public Vector lstModelVector (WindowEvent evt, int conversionTypeSelected) throws SQLException { int x = 0; setIConvTyp(conversionTypeSelected);
//Open a new connection to the ConversionFactors database MyDBConnection myConnection = new MyDBConnection(); myConnection.init(); Connection con = myConnection.getMyConnection();
//Prepare a new SQL statement, result set, and string variables to hold the SQL data String strConvTypID = null; String strConvTyp = null;
//Create and execute an SQL statement to determine what kind of conversion is initially set by the combobox. //Ultimately, this determines which units need to be displayed in each listbox. Statement stmt = con.createStatement();
//The first SQL stmt is to find out which conversion table to use (e.g., mass, area, distance, etc.). int conversionTypeID = getIConvTyp(); ResultSet rs = stmt.executeQuery("select CONTYPEID, CONTYPENAME from CONTYPES where CONTYPEID = " + conversionTypeID);
//Read the data from the results of the SQL query. while (rs.next()) { strConvTypID = rs.getString("CONTYPEID"); strConvTyp = rs.getString("CONTYPENAME"); }
//Declare some utility variables that hold the appropriate table name, conversion-type name, and the //conversion-type ID (index). String tableName = strConvTyp + "CF"; String unitName = strConvTyp + "Name"; String unitID = strConvTyp + "ID";
String sqlListData = "select " + unitID + ", " + unitName + " from " + tableName; //This is where the problem occurs System.out.println(sqlListData);
//Create and execute the SQL statement Statement stmtList = con.createStatement(); ResultSet rsList = stmtList.executeQuery(sqlListData);
//Advance cursor one position so it points to the appropriate data (it starts just before the first record) rsList.next(); int dID = rsList.getInt(unitID); String distID = rsList.getString(unitName);
//Define a vector to hold the list data String strUName = null; int intUIndx = 0;
//Read the result set and populate the vector Vector vecList = new Vector(); while (rsList.next()) { strUName = rsList.getString(unitName); intUIndx = rsList.getInt(unitID); vecList.addElement(makeObj(strUName)); }
//Close the database connection con.close();
return vecList; }
There are now only two queries (not three, as in the original post), but the problem remains the same. Someone suggested that I should print out sqlListData and execute the statement directly. I did that, which resulted in the same basic error.
The error generated was: java.sql.SQLSyntaxErrorException: Table/View 'DISTANCECF' does not exist.
The statement I used in an attempt to recreate the error (i.e., sqlListData) was "select DistanceID, DistanceName from DistanceCF". However, when I query the table directly using the following syntax, the data I require are generated properly: select "DistanceID", "DistanceName" from "GREGP"."DistanceCF"
I really don't understand why this particular sql statement requires such a different syntax from the first. Moreover, I don't know how to generate that sort of syntax (with embedded quotation marks) to define sqlListData.
Finally, someone else mentioned that it seemed like a wasted database given that I have no relational structure, and I'm just using the database tables as means to hold data ("just use files for god sakes"). I agree completely! The data need to be updated (i.e., new rows added), edited, and deleted. I wish I knew how to do that with files (I do with database tables)! It definitely would be a better option.
Thanks to everyone who has responded.
Sincerely,
Greg.
P.S. I'm sorry I don't remember who asked, but 'con' is just my connection variable (see code above).
David Harper - 18 Mar 2007 15:29 GMT [SNIP]
> The error generated was: java.sql.SQLSyntaxErrorException: Table/View > 'DISTANCECF' does not exist. [quoted text clipped - 9 lines] > generate that sort of syntax (with embedded quotation marks) to define > sqlListData. Can I suggest that you write a simple test class which reads an SQL statement from System.in and tries to execute it. That would allow you to test whether the table name is case-sensitive (which I still think is a possible explanation) or whether you need to prefix the table name with the schema or put quotes around all of the identifiers.
You'd be surprised how often a simple test class can help you track down the real cause of the problem inside a more complex application :-)
> Finally, someone else mentioned that it seemed like a wasted database > given that I have no relational structure, and I'm just using the [quoted text clipped - 3 lines] > files (I do with database tables)! It definitely would be a better > option. You might want to look at the java.util.prefs package or even java.util.Properties if you simply need to be able to store and retrieve values keyed by name.
David Harper Cambridge, England
Lew - 18 Mar 2007 17:05 GMT GGP wrote:
> [SNIP] >> The error generated was: java.sql.SQLSyntaxErrorException: Table/View [quoted text clipped - 10 lines] >> generate that sort of syntax (with embedded quotation marks) to define >> sqlListData.
> Can I suggest that you write a simple test class which reads an SQL > statement from System.in and tries to execute it. That would allow you > to test whether the table name is case-sensitive (which I still think is > a possible explanation) or whether you need to prefix the table name > with the schema or put quotes around all of the identifiers. I wonder about the schema prefix also. I noticed that the table is "DistanceCF" in the generated query and "GREGP.DistanceCF" in the reference query.
-- Lew
David Harper - 18 Mar 2007 20:02 GMT > GGP wrote: [SNIP]
> I wonder about the schema prefix also. I noticed that the table is > "DistanceCF" in the generated query and "GREGP.DistanceCF" in the > reference query. Some database systems require the schema prefix (e.g. Oracle) and others don't (e.g. MySQL). I don't know what Apache Derby expects, though Greg's code
> ResultSet rs = stmt.executeQuery("select CONTYPEID, > CONTYPENAME from CONTYPES where CONTYPEID = " + conversionTypeID); omits the prefix, and apparently works just fine, since he tells us that this query yields the table name from which he constructs the query which mysteriously fails.
Curioser and curioser, said Alice.
David Harper Cambridge, England
GGP - 19 Mar 2007 21:52 GMT > > GGP wrote: > [SNIP] [quoted text clipped - 17 lines] > David Harper > Cambridge, England Hi everyone,
Problem solved! It turns out that David was right all along--it had everything to do with variable case. Because I was extracting the basic root of my concatenated variables from the first query from a table where the information was stored in a mixed-case format (e.g., Mass, Distance, etc.), and that the table names I was trying to access were also mixed case (e.g., MassCF, DistanceCF, etc.), the second query kept failing. The first query worked because I created it using an SQL statement (i.e., CREATE TABLE ...), resulting in both the table name and the variable names automatically generated in an upper-case format. I constructed other tables 'by hand,' and therefore, both variable names and table names were mixed-case.
Taking David's advice, I wrote a simple test class to try all sorts of different permutations on my sql string. It wasn't until I converted tableName.toUpperCase() that the table name issue went away, and I was suddenly faced with a variable name issue (same problem). I reconstructed all the tables using an upper-case format for naming the tables and variables (in addition to converting the result set from the initial query .toUpperCase()), and everything now works just fine.
I would like to thank everyone who helped with this problem. I'm new to Java and on a very steep learning curve at the moment. This exercise had me toying with data/file streams (yes, I can almost read an Excel file now, but ran into exactly the same problem as I did reading my database tables--at least now I know how to fix it), writing small (and very valuable) test classes, and doing all sorts of things I wouldn't otherwise be doing if it wasn't for your expert advice. So, thank you--and I hope I can repay the favour sometime down the road!
Cheers,
Greg.
David Harper - 19 Mar 2007 22:41 GMT > Problem solved! Hooray! Drinks for everyone! ;-)
> It turns out that David was right all along--it had everything to do > with variable case. ...
> I'm new to Java and on a very steep learning curve at the moment. Don't be too hard on yourself. I've been programming for 25 years, which means that I've made most of the usual mistakes already, several times over, so it's easier for me to recognise them :-)
Keep at it.
David Harper Cambridge, England
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|