我正在学习Java,我正在通过JDBC使用H2数据库,但它不工作,它说不允许操作它

gupuwyp2  于 2023-03-06  发布在  Java
关注(0)|答案(3)|浏览(103)

错误代码:

IO Exception: "java.nio.file.FileSystemException: /Users/Franco/Desktop/h2: Operation not permitted"; "/Users/Franco/Desktop/h2"
IO Exception: "java.nio.file.FileSystemException: /Users/Franco/Desktop/h2: Operation not permitted"; "/Users/Franco/Desktop/h2" [90031-212] 90031/90031])

Screenshot
IDE中的代码:

import java.sql.*;

public class Main {
    public static void main(String[] args) throws Exception{
        Class.forName("org.h2.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:h2:"+ "/Users/Franco/Desktop/h2/bin", "root", "myPassword");
        Statement stmt = con.createStatement();

        //Código para crear una tabla. Elimina la tabla si esta ya existe y la
        //vuelve a crear
        String createSql = """
                DROP TABLE IF EXISTS TEST;
                CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255));
                INSERT INTO TEST VALUES(1, 'Hello');
                INSERT INTO TEST VALUES(2, 'World');
                """;
        stmt.execute(createSql);

        //Codigo para consultar todos los registros de la tabla TEST
        String sql = "select * from TEST";
        ResultSet rd = stmt.executeQuery(sql);

        //Código para recorrer el resultado de la consulta
        while(rd.next()) {
            System.out.println(rd.getInt(1) + rd.getString(2));
        }
        con.close();
    }
}

我想可能是访问请求,我有一台MacOS Ventura 13. 2. 1的Mac

tquggr8v

tquggr8v1#

我认为问题出在您的JDBC连接字符串中(缺少file:),请尝试:

Connection con = DriverManager.getConnection("jdbc:h2:file:"+ "/Users/Franco/Desktop/h2/bin", "root", "myPassword");

它可能还需要驱动器,例如C:

Connection con = DriverManager.getConnection("jdbc:h2:file:"+ "C:/Users/Franco/Desktop/h2/bin", "root", "myPassword");

C:/用户/Franco/桌面/h2/bin是否存在?

oaxa6hgo

oaxa6hgo2#

我在site中做了一些类似的代码
连接c = drm.getConnection(“jdbc:h2:文件:“+“/binlbr”,“管理员”,“通过”);
而且成功了!

r6l8ljro

r6l8ljro3#

解决办法是:

Connection con = DriverManager.getConnection("jdbc:h2:"+ "~/h2", "root", "myPassword");

相关问题