使用jdbc 登录数据库,出现 java.sql.SQLException: Io 异常: Unknown host specified 异常 !求高手解决
我的代码:packagepack_like;importjava.sql.Connection;importjava.sql.DriverManager;importja...
我的代码:
package pack_like;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class student {
public void getstudent(){
ResultSet rs=null;
Statement stmt=null;
Connection connection=null;
try {
//1;class.forname()加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2:通过DriverManager.getConnection(url,用户名,密码)获取数据库的连接(Connection)
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger");
//3:获得Statement对象,执行SQL语句
String sql="select *from student ";
stmt=connection.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
int id= rs.getInt("id");
String name=rs.getString("name");
int phone=rs.getInt("phone");
String address=rs.getString("address");
System.out.print(id+"\t"+name+"\t"+phone+"\t"+address);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
rs.close();
stmt.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[]args){
student stu=new student();
stu.getstudent();
}
}
抛出的异常:
java.sql.SQLException: Io 异常: Unknown host specified
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(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at pack_like.student.getstudent(student.java:18)
at pack_like.student.main(student.java:52)
Exception in thread "main" java.lang.NullPointerException
at pack_like.student.getstudent(student.java:40)
at pack_like.student.main(student.java:52) 展开
package pack_like;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class student {
public void getstudent(){
ResultSet rs=null;
Statement stmt=null;
Connection connection=null;
try {
//1;class.forname()加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2:通过DriverManager.getConnection(url,用户名,密码)获取数据库的连接(Connection)
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","tiger");
//3:获得Statement对象,执行SQL语句
String sql="select *from student ";
stmt=connection.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
int id= rs.getInt("id");
String name=rs.getString("name");
int phone=rs.getInt("phone");
String address=rs.getString("address");
System.out.print(id+"\t"+name+"\t"+phone+"\t"+address);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
rs.close();
stmt.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[]args){
student stu=new student();
stu.getstudent();
}
}
抛出的异常:
java.sql.SQLException: Io 异常: Unknown host specified
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(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at pack_like.student.getstudent(student.java:18)
at pack_like.student.main(student.java:52)
Exception in thread "main" java.lang.NullPointerException
at pack_like.student.getstudent(student.java:40)
at pack_like.student.main(student.java:52) 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询