Updating/Changing JDBC 中 h2 数据库的密码

Updating/Changing password of h2 database in JDBC

目前我正在使用 h2 嵌入式数据库为学生数据库开发 Java 应用程序。我只想知道如何使用用户输入更新密码。

Scanner sc=new Scanner(System.in);
String username,password,user="admin",pass;
System.out.println("User name:");
username=sc.next();
System.out.println("Password:");
password=sc.next();
Class.forName("org.h2.Driver");
Connection   c=DriverManager.getConnection("jdbc:h2:database",username ,password);
Statement s=c.createStatement();
System.out.println("Enter new password:");
pass=sc.next();
s.execute("alter user "+user+" identified by "+pass);

上面给出的是显示以下执行错误的部分代码段:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "ALTER USER ADMIN IDENTIFIED[*] BY POPPER "; expected "SET, RENAME, ADMIN"; SQL statement: alter user admin identified by popper [42001-176]

您尝试了 Oracle 语法。试试 h2 语法

ALTER USER admin SET PASSWORD 'aaa';

有关详细信息,请参阅 http://www.h2database.com/html/grammar.html#alter_user_set_password

编辑 1

您的代码需要更改为

s.execute("alter user "+user+" set password '"+pass + "'");

尝试以下方法

s.execute("alter user "+user+" set password "+pass);