Java SE 1.5
Spring-2.5.1
mysql-connector-java-5.1.5.zip
Mysql 5.x
一、创建项目,加载程序用到的工具包和驱动。并在源代码目录中添加Spring的配置文件ApplicationContext.xml,配置内容如下:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--数据库的数据源定义-->
<bean id="rptds" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<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>root</value>
</property>
<property name="password">
<value>leizhimin</value>
</property>
</bean>
</beans>
DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user (
id int(11) NOT NULL auto_increment,
firstname varchar(50) default NULL,
lastname varchar(50) default NULL,
zipcode varchar(10) default NULL,
tel varchar(20) default NULL,
address varchar(200) default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
insert into t_user(id,firstname,lastname,zipcode,tel,address) values
(1,'haha','hehe','234444','13577788999','阿斯地方'),
(2,'lei','aaa','450000','13939012107','郑州市经三路');
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* File: ContextHelper.java
* User: leizhimin
* Date: 2008-2-21 14:20:46
* Spring Bean容器环境获取工具类
*/
public final class ContextHelper {
private static ClassPathXmlApplicationContext _ctx;
static {
_ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
private ContextHelper() {
}
public static ClassPathXmlApplicationContext getContext() {
return _ctx;
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;
/**
* Created by IntelliJ IDEA.
* File: DBUtil.java
* User: leizhimin
* Date: 2008-2-21 14:26:30
* 数据库工具
*/
public final class DBUtil {
private static final Log log = LogFactory.getLog(DBUtil.class);
/**
* 获取系统的数据源
*
* @return DataSource
*/
public static DataSource getDataSource() {
DataSource dataSource = null;
try {
dataSource = (DataSource) ContextHelper.getContext().getBean("rptds");
} catch (Exception e) {
log.error("获取数据源出错,请检查Spring数据源配置!");
}
return dataSource;
}
/**
* 获取数据库连接
*
* @return Connection
*/
public static Connection makeConnection() {
Connection conn = null;
try {
conn = getDataSource().getConnection();
} catch (SQLException e) {
log.error("通过数据源获取数据库连接发生异常!");
e.printStackTrace();
}
return conn;
}
/**
* 执行没有参数的SQL过程
*
* @param procedureName 存储过程名字
* @return boolean 返回存储过程执行的结果,true表示执行成功,false表示执行失败.
*/
public static boolean executeBSDProcedure(String procedureName) {
boolean flag = false;
String sqlStr = "{call " + procedureName + "()}";
CallableStatement cs;
Connection conn = makeConnection();
try {
cs = (CallableStatement) conn.prepareStatement(sqlStr);
cs.executeUpdate(sqlStr);
flag = true;
} catch (SQLException e) {
log.error("调用存储过程" + sqlStr + "失败!");
e.printStackTrace();
}
return flag;
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by IntelliJ IDEA.
* File: Test.java
* User: leizhimin
* Date: 2008-2-21 14:41:49
* Spring 数据源应用测试
*/
public class Test {
private static final Log log = LogFactory.getLog(Test.class);
public static void main(String args[]) {
Test.test();
}
public static void test() {
String testSql = "select * from t_user";
Connection conn = DBUtil.makeConnection();
Statement stmt = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet rs = stmt.executeQuery(testSql);
while (rs.next()) {
String firstName = rs.getString("firstname");
String lastName = rs.getString("lastname");
System.out.println(firstName + " " + lastName);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.info("关闭Statement对象出现异常!");
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.error("关闭数据库连接失败!");
e.printStackTrace();
}
}
}
}
}
lei aaa
Process finished with exit code 0
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--数据库的数据源定义-->
<bean id="rptds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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>root</value>
</property>
<property name="password">
<value>leizhimin</value>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--数据库的数据源定义-->
<bean id="dataSource"
class="org.springframework.indi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/testds</value>
</property>
</bean>
</beans>
Spring 2.0开发文档
Spring 技术手册
祝各位元宵节快乐!!!