I tried to insert some data into my database table using JDBC, but it throws some exception like--> java.lang.NullPointerException
There is no error in my connection I guess!
Here is the code-->
package batch2;
public class Database {
Connection con;
Statement stm;
Database(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javabatch2", "root", "");
}
catch (Exception ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void addStudent(String name, String phone, String email){
String sql= "insert into student(name, phone,email) values ('"+name+"', '"+phone+"', '"+email+"')";
try{
stm.execute(sql);
}catch(Exception ex){
System.out.println(ex); ***//exception trows here***
}
}
}
Here is my main class-->
package batch2;
public class Main1 {
public static void main(String[] args){
Database obj = new Database();
obj.addStudent("hola", "875423", "hola@gmail.com");
System.out.println("Added into student table DONE");
}
}