Exception in thread "main" java.lang.NullPointerException
public class JDBCconnection implements Methots{
public static String URL="jdbc:oracle:thin:@localhost:1521:xe";
public static String USERNAME="hr";
public static String PASSWORD="hr";
public static String DRIVER_CLASS="oracle.jdbc.driver.OracleDriver";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
JDBCconnection jdbc = null; //eger burayi null yaparsak (new JDBCconnection(); olmalı)
List<Employee> list=jdbc.getEmployee(); //burada null olan birseyin methodlarina ulasamayiz
System.out.println(list);
}
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER_CLASS);
Connection conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public List<Employee> getEmployee() throws ClassNotFoundException, SQLException {
String SQL_CODE="SELECT first_name,last_name,employee_id FROM Employees
WHERE employee_id >100";
Employee emp=null;
List<Employee> employeeList=new ArrayList<Employee>();
Connection c=getConnection();
PreparedStatement prs=c.prepareStatement(SQL_CODE);
ResultSet result=prs.executeQuery();
while(result.next()){
String fName=result.getString("first_name");
String lName=result.getString("last_name");
int empId=result.getInt("employee_id");
emp=new Employee();
emp.setFirstName(fName);
emp.setLastName(lName);
emp.setEmployeeId(empId);
employeeList.add(emp);
}
return employeeList;
}
}
Hiç yorum yok:
Yorum Gönder