2016-04-01 15 views
1

MS Access DBのテーブルにアクセスしようとすると、以下のエラーが表示されています。WebアプリケーションからMS Access DBにアクセスする

私はDBを別途作成しました。

以下はコードです。

public String ValidateLogin(String un, String pw) 
{ 
    try 
    { 

     // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      String url = "jdbc:ucanaccess://D:/questions1.accdb"; 
      Connection con = DriverManager.getConnection(url, "", ""); 
      Statement s =con.createStatement(); 

      String query="select count(*) from Users where UserName="+un; 
      System.out.println(query); 
      s.execute(query); 

      ResultSet rs= s.getResultSet(); 
      if(rs==null) 
      { 
       result="failure"; 
      } 
      else 
      { 
       String a1=rs.getString(3); 
       if(a1.equals(pw)) 
       { 
        result="success"; 
       } 
       else 
       { 
        result="failure"; 
       } 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    return result; 
} 

レコードの数を調べようとするとうまくいきます。しかし、私は1人のユーザーのレコードを取得しようとすると、それは私にエラーを与えている。

何をする必要があるか教えてください。

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.4 user lacks privilege or object not found: AAA 
    at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:154) 
    at com.infy.dao.LoginActionDAO.ValidateLogin(LoginActionDAO.java:32) 
    at com.infy.HelloWorldAction.execute(HelloWorldAction.java:33) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) 
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) 
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488) 
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: AAA 
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) 
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source) 
    at net.ucanaccess.jdbc.Execute.executeWrapped(Execute.java:56) 
    at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:152) 
    at net.ucanaccess.jdbc.Execute.execute(Execute.java:46) 
    at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:152) 
    ... 66 more 
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: AAA 
    at org.hsqldb.error.Error.error(Unknown Source) 
    at org.hsqldb.error.Error.error(Unknown Source) 
    at org.hsqldb.ExpressionColumn.checkColumnsResolved(Unknown Source) 
    at org.hsqldb.QueryExpression.resolve(Unknown Source) 
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source) 
    at org.hsqldb.ParserCommand.compilePart(Unknown Source) 
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source) 
    at org.hsqldb.Session.executeDirectStatement(Unknown Source) 
    at org.hsqldb.Session.execute(Unknown Source) 
    ... 72 more 
+1

クエリ文字列に値を連結 'PreparedStatement'の代わりに、(誤っ!)を使用します。基本的な問題は、ユーザー名の値を引用符で囲まないので、値ではなく列名と見なされますが、実際には準備済みの文を使用するべきです。 –

+0

ありがとうございました! :) あなたが正しいです。 –

答えて

1

あなたはエラーに

net.ucanaccess.jdbc.UcanaccessSQLExceptionを得ている:UCAExc ::: 3.0.4ユーザーが権限を持たないか、オブジェクトが見つかりません:AAA

あなたのためにコードは

select count(*) from Users where UserName=AAA 

AAAがあるSQLクエリを構築しています文字列値ではなく列名として解釈されます。

適切な解決策は、より次のようになりますパラメータ化クエリを実行するためにPreparedStatementを使用することです:

String query = "SELECT * FROM Users WHERE UserName=?"; 
PreparedStatement s = con.prepareStatement(query); 
s.setString(1, un); 
ResultSet rs = s.executeQuery(); 
if (rs.next()) { 
    // matching row found, so continue processing 
} else { 
    // no matching row found, so inform user and/or log error 
} 
+0

コードを共有していただきありがとうございます。出来た!!! –

関連する問題