Announcement Announcement Module
Collapse
No announcement yet.
How to use the GeneratedKeyHolder ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use the GeneratedKeyHolder ?

    PHP Code:
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.PreparedStatementCreator;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.jdbc.support.GeneratedKeyHolder;
    import org.springframework.jdbc.support.KeyHolder;

    public class 
    GeneratedKeyHolderTest
    {
        public static 
    void main(String[] args)
        {
            
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
            
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            
    dataSource.setUrl("jdbc:mysql://192.168.1.11:3306/sampledb");
            
    dataSource.setUsername("root");
            
    dataSource.setPassword("mypassword");

            
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
            
    jdbcTemplate.setDataSource(dataSource);

            final 
    String sql "INSERT INTO t_user (user_id, user_name, credits, password, last_visit, last_ip) VALUES (null, 'degui.chen', 9999999, '821208', '2009-09-30 11:25:42', '202.96.128.9')";
            
    KeyHolder keyHolder = new GeneratedKeyHolder();
            
    jdbcTemplate.update(new PreparedStatementCreator()
            {
                public 
    PreparedStatement createPreparedStatement(Connection connthrows SQLException
                
    {
                    
    PreparedStatement preparedStatement conn.prepareStatement(sql);
                    return 
    preparedStatement;
                }
            }, 
    keyHolder);
            
    System.out.println(keyHolder.getKey().intValue());
        }

    use the spring-framework-2.5.6.SEC01 .
    -------------------------------------------------------------------------
    Exception:

    log4j:WARN No appenders could be found for logger (org.springframework.jdbc.datasource.DriverManager DataSource).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.springframework.dao.TransientDataAccessResourc eException: PreparedStatementCallback; SQL []; !Statement.GeneratedKeysNotRequested!; nested exception is java.sql.SQLException: !Statement.GeneratedKeysNotRequested!
    at org.springframework.jdbc.support.SQLStateSQLExcept ionTranslator.doTranslate(SQLStateSQLExceptionTran slator.java:106)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:80)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:607)
    at org.springframework.jdbc.core.JdbcTemplate.update( JdbcTemplate.java:824)
    at GeneratedKeyHolderTest.main(GeneratedKeyHolderTest .java:26)
    Caused by: java.sql.SQLException: !Statement.GeneratedKeysNotRequested!
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:926)
    at com.mysql.jdbc.StatementImpl.getGeneratedKeys(Stat ementImpl.java:1814)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInP reparedStatement(JdbcTemplate.java:829)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:591)

  • #2
    are you sure you can insert a null value to user_id?

    Comment


    • #3
      are you sure you can insert a null value to user_id?
      yes.

      ---------------------------------------------------------------------
      ok. thx all.

      update mysql-connector-java-5.1.7.jar to mysql-connector-java-5.1.10.jar

      modify code :
      PHP Code:
          import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.SQLException;
      import java.sql.Statement;

      import org.springframework.jdbc.core.JdbcTemplate;
      import org.springframework.jdbc.core.PreparedStatementCreator;
      import org.springframework.jdbc.datasource.DriverManagerDataSource;
      import org.springframework.jdbc.support.GeneratedKeyHolder;
      import org.springframework.jdbc.support.KeyHolder;

      public class 
      GeneratedKeyHolderTest
      {
          public static 
      void main(String[] args)
          {
              
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
              
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
              
      dataSource.setUrl("jdbc:mysql://192.168.1.11:3306/sampledb");
              
      dataSource.setUsername("root");
              
      dataSource.setPassword("mypassword");

              
      JdbcTemplate jdbcTemplate = new JdbcTemplate();
              
      jdbcTemplate.setDataSource(dataSource);

              final 
      String sql "INSERT INTO t_user (user_id, user_name, credits, password, last_visit, last_ip) VALUES (null, ?, ?, ?, ?, ?)";
              
      KeyHolder keyHolder = new GeneratedKeyHolder();
              
      jdbcTemplate.update(new PreparedStatementCreator()
              {
                  public 
      PreparedStatement createPreparedStatement(Connection connthrows SQLException
                  
      {
                      
      PreparedStatement preparedStatement conn.prepareStatement(sqlStatement.RETURN_GENERATED_KEYS);
                      
      preparedStatement.setString(1,"degui.chen");
                      
      preparedStatement.setInt(29999999);
                      
      preparedStatement.setInt(3821208);
                      
      preparedStatement.setString(4"2009-09-30 11:25:42");
                      
      preparedStatement.setString(5"202.96.128.9");                
                      return 
      preparedStatement;
                  }
              }, 
      keyHolder);
              
      System.out.println(keyHolder.getKey().intValue());
          }

      Comment

      Working...
      X