【问题标题】:how to insert object to h2如何将对象插入h2
【发布时间】:2016-12-05 06:11:16
【问题描述】:

我已阅读有关在数据库中存储对象的 H2 文档。有特殊的 SQL 类型 OTHER 和方法 setObjectgetObject。我试过这段代码:

PreparedStatement statement = null;
try {
    statement = connection.prepareStatement("CREATE TABLE PUBLIC.foo (name VARCHAR(64) NOT NULL, data OTHER NULL);");
    statement.execute();
} finally {
    statement.close();
}

statement = null;

try {
    statement = connection.prepareStatement("INSERT INTO PUBLIC.foo (name, data) VALUES(?,?);");
    statement.setString(1, "lololo");
    statement.setObject(2, new String[]{"foo", "bar"});
    statement.execute();
}finally {
    statement.close();
}

但我有例外:

org.h2.jdbc.JdbcSQLException: ШеÑ�тнадцѰтиричнаÑ�Ñ�трок¡°Ñ�о¡ ´ÐµÑ€Ð¶Ð¸Ñ, нешеÑÑ,надцаÑ,иричные Ñ�имволы: "(foo, bar)" 十六进制字符串包含非十六进制字符:“(foo, bar)”; SQL 语句: INSERT INTO PUBLIC.foo (name, data) VALUES(?,?) -- (?1, ?2) [90004-191]

怎么了?

【问题讨论】:

标签: java sql database h2


【解决方案1】:

我相信这就是你要找的东西(甚至我也是)。

您只需要在表格中创建一个类型为“其他”的列。 请参阅'创建表 testobj2(obj other)'

看看我的示例代码:

    static String DB_DRIVER = "org.h2.Driver";
    static String DB_CONNECTION = "jdbc:h2:./test2";
    static String DB_USER = "";
    static String DB_PASSWORD = "";

    public static void benchmarkH2Inserts()   {
        try {
            Class.forName(DB_DRIVER);
            Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
            String createQry = "create table testobj2(obj other)";
            String insertQuery = "insert into testobj2(obj) values(?)";
            String selectQuery = "select * from testobj2";

//            dbConnection.setAutoCommit(false);
            dbConnection.prepareStatement(createQry).executeUpdate();
            long lStartTime = System.nanoTime();

            for(int i=0; i<10000; i++) {
                dbConnection.setAutoCommit(false);
                CloudElement_Circuit obj = new CloudElement_Circuit();
                obj.setNrm8DesignId(1230L);

                PreparedStatement preparedStatement = dbConnection.prepareStatement(insertQuery);
                preparedStatement.setObject(1,obj);
                preparedStatement.execute();
                dbConnection.commit();
            }
            long lEndTime = System.nanoTime();
            long output = lEndTime - lStartTime;
            System.out.println("benchmarkH2Inserts() : Elapsed time in nanoseconds: " + output);
            System.out.println("benchmarkH2Inserts() : Elapsed time in milliseconds: " + output / 1000000);

            //Selecting
            PreparedStatement preparedStatement = dbConnection.prepareStatement(selectQuery);
            ResultSet rs = preparedStatement.executeQuery();
            while(rs.next())    {
                CloudElement_Circuit obj = (CloudElement_Circuit) rs.getObject("obj");
                System.out.println("Fetched Object : " + obj.getNrm8DesignId());
            }

            dbConnection.close();

        } catch (Exception e)   {
            e.printStackTrace();
        }
    }

请注意,“CloudElement_Circuit”是一个序列化类。 在此处查看“其他类型”:http://www.h2database.com/html/datatypes.html H2 示例:https://www.javatips.net/blog/h2-database-example

【讨论】:

    【解决方案2】:

    试试这个方法

    List<String> genre = new ArrayList<String>();
    String comma="";
     StringBuilder allGenres = new StringBuilder();
     for (String g: genre) {
        allGenres.append(comma);
        allGenres.append(g);
        comma = ", ";
     }
    

    那你就可以这样传下去了

    preparedStmt.setString (2, allGenres.toString());
    

    【讨论】:

    • 那么,如何在一个字段中插入任​​何类型?字符串、整数、对象列表等?我需要将任何对象插入数据库。
    • 您可以使用序列化。看看这个页面,他们有一个很好的例子。java2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多