2012-02-01 23 views
0

我正在使用OSGI綁定,它使用JDBC連接爲了將行更新到數據庫中。這是源代碼:JDBC - 無法找到getConnection()

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;  
    } 
} 

當我試圖編譯包我收到此錯誤信息:

這是NetBeans的錯誤堆棧:http://pastebin.com/zDpy8RpL

似乎getConnection()無法找到?你知道我該如何解決這個問題嗎?

投注祝願

回答

7

您導入了錯誤的數據源:

import javax.activation.DataSource; 

正確的是javax.sql中Datasource

import javax.sql.DataSource; 
相關問題