2016-07-30 33 views
2

einfügen Ich habe H2 Dokumente über das Speichern von Objekten in der Datenbank gelesen. Es gibt einen speziellen SQL-Typ OTHER und die Methoden setObject und getObject. Ich habe diesen Code versucht:wie Objekt an 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(); 
} 

Aber ich habe die Ausnahme bekam:

org.h2.jdbc.JdbcSQLException: ШÐμÑÑ,нР° Ð'Ñ † Ð ° Ñ,Ð¸Ñ € Ð¸Ñ ‡ нР° Ñ ÑÑ,Ñ € окР° ÑоÐ'ÐμÑ € жиÑ, нÐμÑÐμÑÑ,нР° Ð'Ñ † Ð ° Ñ,и Ñ ​​€ Ð¸Ñ ‡ нÑ

Was ist falsch?

+1

Werfen Sie einen Blick hier: http://stackoverflow.com/questions/31964209/h2-other-data-type-throws-exception-when-storing -string-or-boolean – Seelenvirtuose

+0

Vielen Dank. –

Antwort

0

diesen Ansatz glauben Versuchen

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

Dann ist das ich

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

Also, wie kann ich einen beliebigen Typ in ein Feld einfügen? String, Int, Liste der Objekte usw.? Ich muss jedes Objekt in db einfügen. –

+0

Sie können Serialisierung verwenden. Sieh dir diese Seite an, sie haben ein schönes Beispiel. [Java2] (http://www.java2s.com/Code/Java/Database-SQL-JDBC/HotoserializedeserializeaJavaobjecttheMySQLdatabase.htm) –

1

es wie diese passieren können, was man sucht waren (Auch ich war).

Sie müssen nur eine Spalte in Ihrer Tabelle mit dem Typ 'andere' erstellen. See 'Tabelle testobj2 (obj andere) erstellen'

Sehen Sie sich meine Beispielcode:

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(); 
     } 
    } 

Beachten Sie, dass 'CloudElement_Circuit' eine serialisierte Klasse. Blick auf 'andere Art' hier: http://www.h2database.com/html/datatypes.html H2 Beispiel: https://www.javatips.net/blog/h2-database-example