I am writing a program to connect my Oracle database with Java on Eclipse. I keep getting the error mentioned above. I've tried so many thing but nothing worked. any help would be appreciated.
This is the error that I am getting:
ava.lang.NullPointerException
at learning.ConnectionClass.getEmployee(ConnectionClass.java:32)
at learning.Driver.main(Driver.java:10)
Here is my code:
import java.sql.*;
import java.sql.Connection;
import javax.swing.JOptionPane;
public class ConnectionClass {
private static Connection conn;
public static Connection dbconnect()
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
System.out.println("Connection established");
return conn;
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
public static Person getEmployee(String input)
{
try {
PreparedStatement pst = conn.prepareStatement("Select * from person where First_Name =?");
pst.setString(1, input);
ResultSet rs = pst.executeQuery();
Person per = null;
if(rs.next()) {
String cnic = rs.getString("CNIC_no");
String First_Name = rs.getString("First_Name");
String Last_Name = rs.getString("Last_Name");
String Gender = rs.getString("Gender");
String Contact_no = rs.getString("Contact_no");
int Age = rs.getInt("Age");
String Contact_id = rs.getString("Contact_id");
per = new Person(cnic, First_Name, Last_Name, Gender, Contact_no, Age, Contact_id );
}
return per;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}