zoukankan      html  css  js  c++  java
  • Spring SqlRowSet example--转载

    原文地址:http://www.roseindia.net/tutorial/spring/spring3/jdbc/sqlrowset.html

    The 'SqlRowSet' is used to handle the result fetched (very similar to ResultSet in core java). The main difference is that SQL exception is never thrown by it. The 'SqlRowSet' throws 'org.springframework.jdbc.InvalidResultSetAccessException' if needed. For using it in your class file you need to import ' org.springframework.jdbc.support.rowset.SqlRowSet ' package. You can use next() and getString() method as you are using with 'ResultSet' of core java. Given below is example related to it :

    sqlrowset.java

    package net.roseindia;
    
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.support.rowset.SqlRowSet;
    
    public class sqlrowset {
        private JdbcTemplate jdbcTemplate;
    
        public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }
    
        public void doExecute() {
            SqlRowSet srs =jdbcTemplate.queryForRowSet("select * from customer");
            int rowCount = 0;
            while (srs.next()) {
              System.out.println(srs.getString("id") + " - " + srs.getString("first_name")+ " - " + srs.getString("last_name")+ " - " + srs.getString("last_login"));
              rowCount++;
            }
            System.out.println("Number of records : "+rowCount);
        }
    
    }

    DataTable.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <bean id="CreateTable" class="net.roseindia.CreateTable">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <bean id="sqlrowset" class="net.roseindia.sqlrowset">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
            <property name="user" value="root" />
            <property name="password" value="root" />
        </bean>
        <context:property-placeholder location="jdbc.properties" />
    </beans>

    sqlrowsetMain.java

    package net.roseindia;
    
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    
    public class sqlrowsetMain {
    
        public static void main(String[] args) {
    
            XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("DataTable.xml"));
    
            sqlrowset myBean = (sqlrowset) beanFactory.getBean("sqlrowset");
    
            myBean.doExecute();
        
            }
        
    }

    OUTPUT

    Data in Sql Table :

    After executing code output in Eclipse's console :

  • 相关阅读:
    JSP中Session的使用
    深入了解父类引用指向子类
    Gamma校正及其OpenCV实现
    cocos2d-x3.0之请求网络(phpserver)
    Quartz使用-入门使用(java定时任务实现)
    ExtJs--15--Ext.is*各种类型推断的方法,简单看源代码就能够明确了
    小谈边界问题
    VS2010旗舰版安装图解
    LSPCI具体解释分析
    兔子--gradle安装和配置
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4540312.html
Copyright © 2011-2022 走看看