В БД есть таблица user, изначально пустая, создается методом createTable()
public void createTable() throws SQLException {
connect = DriverManager.getConnection(DB_URL, LOGIN, PASSWORD);
Statement statement = connect.createStatement();
statement.execute("create table user(" +
"login varchar(100) primary key, " +
"password varchar(100));");
statement.close();
connect.close();
Имеется метод registrateUser()
public void registrateUser() throws SQLException {
String login = "login";
String password = "password";
addUser(login, password);
из этого метода вызывается метод addUser(String login, String password), который добавляет запись в БД
public void addUser(String login, String password) throws SQLException {
connect = DriverManager.getConnection(DB_URL, LOGIN, PASSWORD);
PreparedStatement statement = connect
.prepareStatement("insert into user(login, password) values(?, ?)");
statement.setString(1, login);
statement.setString(2, password);
statement.executeUpdate();
}
При компиляции вылетает ошибка: CompletionException: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "LOGIN"; SQL statement: insert into user(login, password) values(?, ?)
Не могу понять откуда там NULL берется и почему не работает.
UPDATE (имена немного поменялись в процессе, но на суть проблемы не повлияло):
public class UserDB {
public static final String DB_URL = "jdbc:h2:~/test";
public static final String LOGIN = "sa";
public static final String PASSWORD = "";
private static Connection connect = null;
public void initUserDB() throws SQLException {
connect = DriverManager.getConnection(DB_URL, LOGIN, PASSWORD);
Statement statement = connect.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS user(login varchar(20) primary key, password varchar(20));");
}
public void addUser(String login, String password) throws SQLException {
connect = DriverManager.getConnection(DB_URL, LOGIN, PASSWORD);
PreparedStatement statement = connect
.prepareStatement("insert into user(login, password) values(?, ?)");
statement.setString(1, login);
statement.setString(2, password);
statement.executeUpdate();
}
public class HomeController extends Controller {
private static UserDB registeredUsers = new UserDB();
public Result registrateUser() throws SQLException {
String login = request().getQueryString("login");
String password = request().getQueryString("password");
registeredUsers.addUser(login, password);
return ok("Successful registration");
}
public void dbInit() throws SQLException {
registeredUsers.initUserDB();
}
}
Вот такой стектрейс по этой ошибке выдает фреймворк:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "LOGIN"; SQL statement:
insert into user(login, password) values(?, ?) [23502-192]]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "LOGIN"; SQL statement:
insert into user(login, password) values(?, ?) [23502-192]
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "LOGIN"; SQL statement:
insert into user(login, password) values(?, ?) [23502-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:311)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:784)
at org.h2.command.dml.Insert.insertRows(Insert.java:151)
at org.h2.command.dml.Insert.update(Insert.java:114)
at org.h2.command.CommandContainer.update(CommandContainer.java:98)
at org.h2.command.Command.executeUpdate(Command.java:258)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:160)
Вопрос: Зачем Bundle нужен? чтобы создать связку ключ-значение? или у него есть еще функции?
package firstTC; import javautil
Доброго времени суток! Вот форма