使用oracle 11g与Java程序的连接问题

时间:2014-10-07 18:35:41

标签: java jdbc oracle11g

我的java程序与使用ojdbc7.jar与oracle 11g交互的连接问题。我在Windows 7平台上使用jdk 1.8。我的连接出错了。

我正在编写下面的整个程序:

package mydao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AdminDAO {
    Connection con;
    public void connectionMethod() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracleEE", "system", "tiger");
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
    public void loginCheck(String uid, String pwd) {
        connectionMethod();
        try {
            String sql = "SELECT * FROM ADMIN_LOGIN WHERE ADMINUID=? AND ADMINPWD=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, uid);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                rs.getString(1);
                rs.getString(2);
                rs.getString(3);
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        AdminDAO a = new AdminDAO();
        a.loginCheck("avi", "avi3");
    }
}         

但是我得到这样的错误:

    run:
    java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at mydao.AdminDAO.connectionMethod(AdminDAO.java:33)
    at mydao.AdminDAO.loginCheck(AdminDAO.java:43)
    at mydao.AdminDAO.main(AdminDAO.java:74)

    Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:445)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:464)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:594)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:229)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ... 9 more

    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:162)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:411)
    ... 14 more

    Exception in thread "main" java.lang.NullPointerException
    at mydao.AdminDAO.loginCheck(AdminDAO.java:61)
    at mydao.AdminDAO.main(AdminDAO.java:74)
    Java Result: 1

任何人都可以帮助我摆脱这个问题吗?我很困惑如何从这个错误中恢复过来。

2 个答案:

答案 0 :(得分:0)

请尝试在services.msc中启动TNS-Listener服务。

答案 1 :(得分:0)

服务器似乎没有侦听指定的端口或主机名。

检查您的服务是否正在运行,如果是,请检查它正在侦听的端口。如果您的oracle服务正在侦听不同的端口,请在代码中更新相同内容并进行检查。