JDBC简单程序

时间:2013-12-17 18:30:52

标签: jdbc

这是我简单的JDBC程序,但我无法建立连接。类路径设置为ojdbc6.jar

package Demo;
import java.sql.*;

public class JdbcDemo {
    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
        System.out.println("hi");
        Class.forName("oracle.jdbc.driver.OracleDriver");
        System.out.println("hi1");

        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:system","scott","tiger");
    }
}

这是输出和我得到的例外:

Exception in thread "main" java.sql.SQLRecoverableException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:101)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:173)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:229)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:458)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Demo.JdbcDemo.main(JdbcDemo.java:13)

1 个答案:

答案 0 :(得分:1)

请检查以下

  1. 您是否已启动Oracle TNS监听器,如果不是由lsnrctl实用程序启动。
  2. 您是否放置了正确的端口
  3. 数据库服务器中的主机名是否正确?
  4. 问题可能是上述任何一个问题。