[英]How can i get jdbc connection from org.springframework.jdbc.datasource.DriverManagerDataSource class
I'm trying to get jdbc connection using the below code. 我正在尝试使用以下代码获取jdbc连接。
I use mysql database jpa2 and spring 4. How can I get jdbc connection and retrieve this value from mysql database 我使用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;
}
}
When is run above this code then i get this exception 当在此代码上方运行时,我得到此异常
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)
Specify your database configuration in context file as below 在上下文文件中指定数据库配置,如下所示
<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>
and then start inject this datasource object into the JDBCTest class using spring dependency injection. 然后使用spring依赖注入开始将此数据源对象注入JDBCTest类。 ex:
例如:
@autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
or 要么
<bean id="jdbcTest" class="JDBCTest"><property name="dataSource" ref="datasource"/></bean>
Your bean is a Jsf Managed bean so your JdbcTemplate
property should be annotated with @ManagedProperty
else nothing will be injected. 您的bean是Jsf托管bean,因此您的
JdbcTemplate
属性应使用@ManagedProperty
注释,否则将不会注入任何内容。
Fist add a JdbcTemplate
as a bean to the application context. 首先,将
JdbcTemplate
作为bean添加到应用程序上下文中。
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
Then in your bean annotate your JdbcTemplate
with @ManagedProperty
and create a setJdbcTemplate
method. 然后在您的bean中,用
@ManagedProperty
注释您的JdbcTemplate
并创建一个setJdbcTemplate
方法。
@ManagedProperty("#{jdbcTemplate}")
private JdbcTemplate jdbcTemplate;
The JdbcTemplate
is designed to make working with JDBC easier. JdbcTemplate
旨在JdbcTemplate
JDBC的使用。 So use it. 因此使用它。 What you have constructed is a very complex way to do the same ugly thing.
您所构造的是做同样丑陋事情的非常复杂的方法。 Use the
JdbcTemplate
correctly and for what it is intended to. 正确使用
JdbcTemplate
以及它的用途。
Your code should be something like this. 你的代码应该是这样的。
getJdbcTemplate().query("select * from global_class", new RowCallbackHandler() {
public void procesRow(ResultSet rs, int row) {
System.out.println("Class Name :" + rs.getString(1));
}
});
This does the same as your code, but uses the JdbcTemplate
as it is intended. 这与您的代码相同,但是按预期使用
JdbcTemplate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.