2

I'm trying to insert data in sql with java. I finished writing but when i run it i receive this error :

Exception in thread "main" java.lang.NullPointerException

No suitable driver found for jdbc:sqlite:C:/Users/inflamesc/Desktop/pp project/neural.sql at insertapp.insertapp.insert(insertapp.java:26) at insertapp.insertapp.main(insertapp.java:44)

here is my code: What is the problem ? what im doing wrong ?

package insertapp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;



public class insertapp {


private Connection connect() {

    String url = "jdbc:sqlite:C:/Users/inflamesc/Desktop/pp project/neural.db";
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return conn;
}
public void insert(String id, String firstName, String lastName, String userName, String email, String password, String aboutMe ) {
    String sql = "INSERT INTO USERS(id,firstName,lastName,userName,email,password,aboutMe) VALUES(?,?,?,?,?,?,?)";

    try (Connection conn = this.connect();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
        pstmt.setString(1, id);
        pstmt.setString(2, firstName);
        pstmt.setString(3, lastName);
        pstmt.setString(4, userName);
        pstmt.setString(5, email);
        pstmt.setString(6, password);
        pstmt.setString(7, aboutMe);

        pstmt.executeUpdate();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

public static void main(String[] args) {

    insertapp app = new insertapp();
    app.insert("1", "Jedi","TheLast","TheLastJedi","jedi@example.com","12345","slacker");
    app.insert("2", "Chew","Becca","ChewBecca","chew@example.com","12345","noisy");
}

}

inflamesc
  • 21
  • 2
  • Welcome to SO. Post the entire stack trace. Note that you set `pstmt.setString(2, ....);` multiple times. – c0der Nov 26 '17 at 11:02
  • Hint: You are doing `pstmt.setString(2, ...)` everywhere – Tim Biegeleisen Nov 26 '17 at 11:02
  • Well thats a mistake but still the same problem. – inflamesc Nov 26 '17 at 11:08
  • Well then read [mcve] and enhance your question accordingly. If that flaw in your code does not fix the bug you are seeing... Then enable us to help you by providing a real [mcve]. And as long as that is missing, the typical npe duplicate should do – GhostCat Nov 26 '17 at 11:26

1 Answers1

2

I guess you forgot to replace '2' in your prepared statment:

pstmt.setString(2, firstName);
pstmt.setString(2, lastName);
pstmt.setString(2, userName);
pstmt.setString(2, email);
pstmt.setString(2, password);
pstmt.setString(2, aboutMe);

'2' needs to be modified accordingly in an incremental manner.

Here is the exact code:

pstmt.setString(1, id);
pstmt.setString(2, firstName);
pstmt.setString(3, lastName);
pstmt.setString(4, userName);
pstmt.setString(5, email);
pstmt.setString(6, password);
pstmt.setString(7, aboutMe);

Hope this solves your problem.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Safeer Ansari
  • 772
  • 4
  • 13