Spring jdbcTemplateorg.springframework.jdbc.core.JdbcTemplate怎样可以取的 table column name?
可否给例子?

解决方案 »

  1.   

    用这个就是使用的原生SQL 查询了那么获取table Name,table comment,column Name, column comment等等信息,主要是依赖于你的SQL语句去数据库中查询。SQL语句有了之后,由JdbcTemplate 去执行查询,然后根据获取结果去进行你需要的操作。对应不同的数据库,需要的查询语句是不一样的,
    给你个查询的MySQL例子,其他的你自己去查吧:查询 columns :
    select * from information_schema.columns where table_schema = ? and table_name = ?查询 tables :
    select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ? and TABLE_TYPE = 'BASE TABLE'
      

  2.   

    import javax.sql.DataSource; 
    import java.sql.*; 
    import org.springframework.jdbc.core.JdbcTemplate; public class UserDAO { 
        private DataSource dataSource;     public void setDataSource(DataSource dataSource) { 
            this.dataSource = dataSource; 
        } 
        
        public void insertUser(User user) { 
            Connection conn = null; 
            Statement stmt = null; 
            try { 
                conn = dataSource.getConnection(); 
                stmt = conn.createStatement(); 
                stmt.executeUpdate("INSERT INTO USER VALUES('"+ user.getId() + "', '" 
                                                              + user.getName() + "', '" 
                                                              + user.getSex() + "', '" 
                                                              + user.getAge() + "')");            
            } 
            catch(SQLException e) { 
                e.printStackTrace(); 
            } 
            finally { 
                if(stmt != null) { 
                    try { 
                        stmt.close(); 
                    }    
                    catch(SQLException e) { 
                        e.printStackTrace(); 
                    } 
                } 
                
                if(conn != null) { 
                    try { 
                        conn.close(); 
                    } 
                    catch(SQLException e) { 
                        e.printStackTrace(); 
                    } 
                } 
            } 
        } 
    }
    2
    <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
    <beans> 
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
            <property name="driverClassName"> 
                <value>com.mysql.jdbc.Driver</value> 
            </property> 
            <property name="url"> 
                <value>jdbc:mysql://localhost:3306/TestDB</value> 
            </property> 
            <property name="username"> 
                <value>caterpillar</value> 
            </property> 
            <property name="password"> 
                <value>123456</value> 
            </property> 
        </bean> 
                                        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
            <property name="dataSource"> 
                <ref bean="dataSource"/> 
            </property> 
        </bean> 
                                                                                    
        <bean id="userDAO" class="onlyfun.caterpillar.UserDAO"> 
            <property name="jdbcTemplate"> 
                <ref bean="jdbcTemplate"/> 
            </property> 
        </bean> 
    </beans>