Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using SQL with MS Access 1

Status
Not open for further replies.

mcklsn

Programmer
Mar 16, 2003
40
US
I am querying an MS Access database from Java. I want to find out if a certain table already exists in the database, or whether the program has to create it. I would like to know if there is an SQL query that will tell the program if a certain table exists or not. Any help would be greatly appreciated.

Thanks
 
You can find it in the hidden table MSysObjects.

SELECT Name, type
FROM MSysObjects where MSysObjects.Name = "dbo_products" and type = 1

This table has all the object names so check the type also, since a form named "dbo_products" will have a different type. Linked tables will be a different type i.e. 4.
 
I am trying to read the MSysObjects table as you suggested, but I get an error saying I don't have permission. How do I go about getting permission to read this table? I'm pretty new at this and I don't have a clue as to how to handle this problem. Again, any help would be greatly appreciated.
 
It is hard to tell from the information available.

How are you connecting to the Access database. Show your connection string.

Are you passing a login and password in the connection string.

You could try assigning your login to the admin group in the access database.
 
Here is a copy of the little test program I'm trying to get running so that I can make sure that I know how to do this:

<%@ page contentType=&quot;text/html; charset=iso-8859-1&quot; language=&quot;java&quot; import=&quot;java.sql.*&quot; errorPage=&quot;&quot; %>
<!-- This is Mark's JSP for loging in. -->
<%-- This is the java code for the JSP. --%>

<%
String commodity = request.getParameter(&quot;select_1&quot;).trim();
String strategy = request.getParameter(&quot;select_2&quot;).trim();
String contractYear = request.getParameter(&quot;ContractYear&quot;).trim();

%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;>
<p align=&quot;center&quot;><font size=&quot;5&quot;>We are watching strategy &quot;<%= strategy %>&quot; for <%= commodity %></font></p>
<p align=&quot;center&quot;><font size=&quot;5&quot;>in the year <%= contractYear %></font></p>
</form>
<%
String sqlTestForTable = &quot;SELECT Name, type FROM MSysObjects where MSysObjects.Name = \&quot;Table1\&quot; and type = 1&quot;;
%>
<%= sqlTestForTable %>
<%
try {
String driverName = &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;;
String connectionURL = &quot;jdbc:eek:dbc:Commodities&quot;;
String userName = null;
String password = null;
Class.forName(driverName);
out.println(&quot;<br>Before conn <br>&quot;);
Connection connection = DriverManager.getConnection(connectionURL, userName, password);
out.println(&quot;After conn<br>&quot;);
Statement statement = connection.createStatement();
out.println(&quot;Before exec<br>&quot;);
statement.execute(sqlTestForTable);
out.println(&quot;After Exec<br>&quot;);
ResultSet rs = statement.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
int numCol = rsmd.getColumnCount();
out.println(&quot;<tr>&quot;);
if (rs.next())
{
System.out.println(&quot;Table found<br>&quot;);
}
else
{
System.out.println(&quot;Table not found<br>&quot;);
}
rs.close();
statement.close();
connection.close();
}
catch (Exception e ) {
out.println(&quot;<br> had this problem: &quot; + e.getMessage());
e.printStackTrace();
}
%>
</table>
</body>
</html>
<p>&nbsp;</p>
</body>
</html>

Does this explain what I'm doing wrong? By the way, I'm logged in as the system administrator for my system, so I would have thought that that would give me the permissions I need. Again, thanks for your help.
 
The user name and password refer to the access database so it is the permission these have in the access database. I don't use JSP. To find somebody more familiar it is probably better to post the question on the ASP Forum here on tek-tips.
 
The easiest method to check would be in your Java code

TRY {
//Attempt to open the table
CATCH (SQLExceptionError e) {
//Prompt the usee
}

This is how we handled table validation when I helped a friend write a Java/MySQL system for his Uni dissertation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top