连接数据库查询数据时出现异常java.sql.SQLException: Io 异常: The Network Adapter could not establ
请各位高手看看 为什么 package jdbc_example.i3102;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCModel {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:PBDEMO", "DBDEMO",
"SA");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM us");
System.out.println("姓名\t性别\t");
while (rs.next()) {
String name = rs.getString("username");
int gender = rs.getInt("易做图");
System.out.println(name+"\t"+gender+"\t");
}
con.close();
} catch (ClassNotFoundException e) {
System.out.println("无法找到驱动类");
}catch (SQLException e) {
e.printStackTrace();
}
}
}
java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at jdbc_example.i3102.JDBCModel.main(JDBCModel.java:13) --------------------编程问答-------------------- Class.forName("oracle.jdbc.OracleDriver").newInstance;
在检查一下数据库有没启动,用sqlplus连接一下看看。如果sqlplus连接不上就是数据库问题咯。 --------------------编程问答-------------------- oracle.jdbc.OracleDriver改成oracle.jdbc.driver.OracleDriver --------------------编程问答-------------------- Class.forName("oracle.jdbc.driver.OracleDriver"); --------------------编程问答-------------------- 检查数据库驱动
补充:Java , Eclipse