我正在使用OSGI bundle,它使用JDBC连接来将行更新到数据库中。这是源代码:
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()
?你知道我怎么解决这个问题吗?
Bets Wishes
答案 0 :(得分:8)
导入错误的数据源:
import javax.activation.DataSource;
正确的是来自javax.sql的Datasource:
import javax.sql.DataSource;