连接Oracle数据库出现NullPointerException错误?
我的代码如下;
import java.sql.*;
public class Dbconn{
private String username;
private String pasword;
public Connection conn;
private String driverName="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:1521:com813";
private String user="java";
private String password="1";
public Connection Dbconn(){
try{
Class.forName(driverName);
conn=DriverManager.getConnection(url,user,password);
return this.conn;
}catch(Exception exception){
System.out.println("error:"+exception);
return null;
}
}
public Connection getConn(){
return this.conn;
}
public static void main(String[] args) {
try{
Dbconn dbconn=new Dbconn();
Connection myconn=dbconn.getConn();
Statement stmt=myconn.createStatement();
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
System.out.println(rs.getString(1));
rs.close();
myconn.close();
}catch(SQLException exception){
System.out.println("error:"+exception);
}
}
}
提示
---------- java ----------
java.lang.NullPointerException
at Dbconn.main(Dbconn.java:31)
Exception in thread "main" Normal Termination
------解决方法--------------------------------------------------------
把Dbconn()
里面的 return 语句去掉
------解决方法--------------------------------------------------------