我正在尝试使用java在sql中插入数据。我写完了但是当我运行它时我收到了这个错误:
线程“main”中的异常java.lang.NullPointerException
没有为jdbc找到合适的驱动程序:sqlite:C:/ Users / inflamesc / Desktop / pp project / neural.sql 在insertapp.insertapp.insert(insertapp.java:26) 在insertapp.insertapp.main(insertapp.java:44)
这是我的代码:问题是什么?我做错了什么?
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");
}
}
答案 0 :(得分:2)
我猜你忘了更换' 2'在你准备好的声明中:
pstmt.setString(2, firstName);
pstmt.setString(2, lastName);
pstmt.setString(2, userName);
pstmt.setString(2, email);
pstmt.setString(2, password);
pstmt.setString(2, aboutMe);
<强>&#39; 2&#39;需要以增量方式进行相应修改。
以下是确切的代码:
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);
希望这能解决你的问题。