Peter Penzov
Peter Penzov

Reputation: 1588

Cannot find DataSource.getConnection()

I'm working on OSGI bundle which uses JDBC connection in order to update rows into database. This is the source code:

package org.DX_57.osgi.SH_27.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.activation.DataSource;
import javax.annotation.Resource;
import org.DX_57.osgi.SH_27.api.SessionHandle;

public class SessionHandleImpl implements SessionHandle {
    
  @Resource(name="jdbc/Oracle") DataSource ds;
    
  @Override
  public String sayHello(String name) {
    return "test2 " + name;
  }
        
  @Override
  public String CheckUserDB(String userToCheck) {
    String storedPassword = null;
    String error_Message = null;
    String SQL_Statement = null;
    String error_Database = null;                
 
    Connection conn = ds.getConnection();
    if (conn == null) throw new SQLException(error_Database = "No connection");      
      
    try {
      conn.setAutoCommit(false);
      boolean committed = false;
      try {
        SQL_Statement = "SELECT Passwd from USERS WHERE Username = ?";
                   
        PreparedStatement passwordQuery = conn.prepareStatement(SQL_Statement);
        passwordQuery.setString(1, userToCheck);
         
        ResultSet result = passwordQuery.executeQuery();
                       
        if(result.next()){
          storedPassword = result.getString("Passwd");
        }
                                     
        conn.commit();
        committed = true;
      } finally {
        if (!committed) conn.rollback();
      }
    } finally {               
      conn.close();
    }  
  
    /** if the user is not found or password don't match display error message*/
    if (storedPassword == null) {
      error_Message = "Invalid Username!";
    } else {
      error_Message = "Invalid Password!";
    }
       
    return storedPassword;       
  }
}

When I try to compile the bundle I get this error message:

This is the error in NetBeans:

Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
/home/rcbandit/Desktop/NetBeans/Prototype_3/SH_27/SH_27-impl/src/main/java/org/DX_57/osgi/SH_27/impl/SessionHandleImpl.java:[42,27] error: cannot find symbol

Full error log: http://pastebin.com/zDpy8RpL

It seems that getConnection() cannot be found? Do you know how I can fix the problem?

Upvotes: 1

Views: 1165

Answers (1)

oers
oers

Reputation: 18704

You import the wrong datasource:

import javax.activation.DataSource;

The correct one is Datasource from javax.sql:

import javax.sql.DataSource;

Upvotes: 8

Related Questions