2016-07-30 59 views
2

我读过关于在数据库中存储对象的H2文档。有特殊的SQL类型OTHER和方法setObjectgetObject。我试过这段代码:如何将对象插入到h2

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:ШÐμÑÑ,наÐ'цd° ÑõиричннÑÑÑÑÑÑÑÑÑÑмÐвоÐвÐнÐнÐнÐнÐннÐнÐнÐÐнÐÐÐнÐÑ “(foo,bar)” 十六进制字符串包含非十六进制字符:“(foo,bar)”; SQL语句:(?) INSERT INTO PUBLIC.foo(名称,数据)VALUES - (?1,2)[90004-191]

有什么不对?

+1

看看这里:http://stackoverflow.com/questions/31964209/h2-other-data-type-throws-exception-when-storing -string-or-boolean – Seelenvirtuose

+0

非常感谢。 –

回答

0

试试这个办法

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()); 
+0

那么,我怎么能插入任何类型到一个领域?字符串,诠释,对象列表等?我需要插入任何对象到数据库。 –

+0

您可以使用序列化。检查thiis页面,他们有一个很好的例子。[java2](http://www.java2s.com/Code/Java/Database-SQL-JDBC/HowtoserializedeserializeaJavaobjecttotheMySQLdatabase.htm) –

1

我相信这就是你找(即使我)。

您只需要在表格中创建一个类型为'其他'的列。 参见 '创建表testobj2(OBJ等)'

看看我的示例代码:

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