繁体   English   中英

我如何从org.springframework.jdbc.datasource.DriverManagerDataSource类获取jdbc连接

[英]How can i get jdbc connection from org.springframework.jdbc.datasource.DriverManagerDataSource class

我正在尝试使用以下代码获取jdbc连接。

我使用mysql数据库jpa2和spring4。如何获得jdbc连接并从mysql数据库检索此值

    import java.io.Serializable;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.JdbcTemplate;

    @ManagedBean
    @ViewScoped
    public class JDBCTest implements Serializable{
        private JdbcTemplate jdbcTemplate;
     void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }
        private static final long serialVersionUID = 1L;
        public void testDB(){
            Connection con=null;
            try {
                con = getJdbcTemplate().getDataSource().getConnection();
                PreparedStatement pst=con.prepareStatement("select * from global_class");
                ResultSet st=pst.executeQuery();
                while(st.next()){
                    System.out.println("Class Name :"+st.getString(1));
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate;
        }

        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }   
    }

当在此代码上方运行时,我得到此异常

WARNING: #{jDBCTest.testDB}: java.lang.NullPointerException
javax.faces.FacesException: #{jDBCTest.testDB}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:64)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)

在上下文文件中指定数据库配置,如下所示

 <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

然后使用spring依赖注入开始将此数据源对象注入JDBCTest类。 例如:

 @autowired
 public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

要么

 <bean id="jdbcTest" class="JDBCTest"><property name="dataSource" ref="datasource"/></bean>

您的bean是Jsf托管bean,因此您的JdbcTemplate属性应使用@ManagedProperty注释,否则将不会注入任何内容。

首先,将JdbcTemplate作为bean添加到应用程序上下文中。

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

然后在您的bean中,用@ManagedProperty注释您的JdbcTemplate并创建一个setJdbcTemplate方法。

@ManagedProperty("#{jdbcTemplate}")
private JdbcTemplate jdbcTemplate;

JdbcTemplate旨在JdbcTemplate JDBC的使用。 因此使用它。 您所构造的是做同样丑陋事情的非常复杂的方法。 正确使用JdbcTemplate以及它的用途。

你的代码应该是这样的。

getJdbcTemplate().query("select * from global_class", new RowCallbackHandler() {
    public void procesRow(ResultSet rs, int row) {
        System.out.println("Class Name :" + rs.getString(1));
    }
});

这与您的代码相同,但是按预期使用JdbcTemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM