【问题标题】:How can i get jdbc connection from org.springframework.jdbc.datasource.DriverManagerDataSource class如何从 org.springframework.jdbc.datasource.DriverManagerDataSource 类获取 jdbc 连接
【发布时间】:2016-03-26 06:18:03
【问题描述】:

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

我使用 mysql 数据库 jpa2 和 spring 4。如何获取 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)

【问题讨论】:

  • 请发布您的完整代码,以便我为您提供帮助。
  • “当我执行 this 代码时,我得到...” 这里的this 是什么?代码在哪里?
  • 你从spring容器中注入数据源对象了吗?
  • 我将使用@ManagedProperty("#{jdbcTemplate}") 解决这个问题

标签: java spring hibernate jdbc


【解决方案1】:

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

 <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>

【讨论】:

  • 我将使用您提供的代码成功创建 jdbc 连接 和 @ManagedProperty("#{jdbcTemplate}") 谢谢先生给我宝贵的时间
  • 您好,@Nalla Srinivas,我正在做与您的回答类似的事情,但是我得到了java.lang.NullPointerException,我查看了堆栈跟踪,异常表明它在Connection conn = jdbcTemplate.getDataSource().getConnection(); 中。你介意分享你对这个问题的建议吗?谢谢。
【解决方案2】:

您的 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 旨在简化 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

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 2015-03-25
    • 2012-11-04
    • 2021-06-23
    • 2017-09-03
    • 2017-11-09
    • 2015-02-08
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多