Home » Java » mysql not pushing insert into database with netbeans

mysql not pushing insert into database with netbeans

Posted by: admin October 29, 2018 Leave a comment

Questions:

So, after 1 day trying to solve the problem, researched tons in SO , youtube , and search engines , changed execute types and everything i cant get into the point of what is happening.

Recently im just learning some html , jsp and servlets for a university project, the thing is that i made a database into mysql workbench with id primary key, auto increment , then some filds like username, password, firstname lastname and so on.

The goal is to make a login page and register page, for some reason if i push data with mysqlworkbench into the database it will let me retrieve it with my login form and my select statment, but for some reason im doing the same thing with register but in this case with the query INSERT.

So, after research, i did preparestatment and changed the executeQuery to executeUpdate and everything, but my log says a nullPointerException somewhere, i know it may be a simple and silly error that im not seeing, but understand that im new at this. this is what i have made so far to insert data into my database

public static UserBean registarUsuario(UserBean bean){

            //preparing some objects for connection 
     Statement stmt = null;    
     ResultSet rs = null;  

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException ex) {
        System.out.println("Error al cargar el driver");
        System.out.println(ex.getMessage());
    }

     String firstname = bean.getFirstName();    
     String lastname = bean.getLastName(); 
     String username = bean.getUsername();    
     String password = bean.getPassword();   
     boolean admin = bean.isAdmin(); 
     int tipo = bean.getType(); 
        String insertQuery =
           "insert into idusuario (firstname,lastname,username,password,admin,tipo) values ('"+firstname+"','"+lastname+"','"+username+"','"+password+"','"+admin+"','"+tipo+"')";

        System.out.println("Firstname is " + firstname);          
  System.out.println("Surname is " + lastname);
  System.out.println("Query: "+insertQuery);

  try 
  {
     //connect to DB 
     currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/usuarios", "root", "admin");
     rs = stmt.executeQuery(insertQuery);
...

my output

Info: Query: insert into idusuario
(firstname,lastname,username,password,admin,tipo) values
(‘jhon’,’marston’,’jmar’,’123′,’true’,’0′) Info: Error :
java.lang.NullPointerException

the thing is that netbeans dosnt even tell me where the NPE is happening so im kind of confused , i dont know if the query is wrong or if something else is, because as i can see in my output , the query seems ok

i leave you here my database structure

enter image description here

Thanks in advanced

Answers: