can somebody tell me how can I check whether a table exists in microsoft access by using java database connectivity. After searching I go something like
If Not IsNull(DlookUp(“Name”,”MSysObjects”,”Name=’TableName'”)) Then
‘Table Exists
I tried to implement the same thing in jdbc program
import java.sql.*;
class CheckTable{
public static void main(String [] rak){
boolean flag;
try{
flag=false;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName="test";
String dbURL="jdbc:odbc:"+dataSourceName;
Connection con=DriverManager.getConnection(dbURL,"","");
Statement s=con.createStatement();
//s.execute("create table xyz (name text, hiredate date)");
//s.execute("insert into xyz values('xz1','22-dec-2005')");
// s.execute("insert into xyz values('pr','2-21-2009')");
s.execute(" If Not IsNull(DlookUp('Name','MSysObjects',"Name='xyz'")) Then "+ flag=true + ");
/*
ResultSet rs=s.getResultSet();
if (rs!=null){
while(rs.next()){
System.out.println(rs.getString(1)+rs.getString(2));
}}
*/
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(" table exists :"+flag);
}
}
I am getting error at s.execute() statement please help me with the syntax.
the errors I am getting are:
‘)’ expected
illegal start of expression
unclosed character literal
not a statement
; excepted
all the errors are on line s.execute(” If Not IsNull(DlookUp(‘Name’,’MSysObjects’,”Name=’xyz'”)) Then “+ flag=true + “);
Try
select 1 from TABLE_NAME. This will throw anSQLExceptionif the table doesn’t exist. Examine theSQLStateanderrorCode– there will be a specific value for this case.Unfortunately, the codes are different for every database, so you will have to print them once on the console and then copy the correct values in your exception handler.