2016-07-30 60 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