create table `customer` ( `cust_id` int(10) unsigned not null auto_increment, `name` varchar(100) not null, `age` int(10) unsigned not null, primary key (`cust_id`) ) engine=innodb auto_increment=1 default charset=utf8;
package com.h3.customer.model; import java.sql.timestamp; public class customer { int custid; string name; int age; //getter and setter methods }
customer dao 接口.
package com.h3.customer.dao; import com.h3.customer.model.customer; public interface customerdao { public void insert(customer customer); public customer findbycustomerid(int custid); }
package com.h3.customer.dao.impl; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import javax.sql.datasource; import com.h3.customer.dao.customerdao; import com.h3.customer.model.customer; public class jdbccustomerdao implements customerdao { private datasource datasource; public void setdatasource(datasource datasource) { this.datasource = datasource; } public void insert(customer customer){ string sql = "insert into customer " + "(cust_id, name, age) values (?, ?, ?)"; connection conn = null; try { conn = datasource.getconnection(); preparedstatement ps = conn.preparestatement(sql); ps.setint(1, customer.getcustid()); ps.setstring(2, customer.getname()); ps.setint(3, customer.getage()); ps.executeupdate(); ps.close(); } catch (sqlexception e) { throw new runtimeexception(e); } finally { if (conn != null) { try { conn.close(); } catch (sqlexception e) {} } } } public customer findbycustomerid(int custid){ string sql = "select * from customer where cust_id = ?"; connection conn = null; try { conn = datasource.getconnection(); preparedstatement ps = conn.preparestatement(sql); ps.setint(1, custid); customer customer = null; resultset rs = ps.executequery(); if (rs.next()) { customer = new customer( rs.getint("cust_id"), rs.getstring("name"), rs.getint("age") ); } rs.close(); ps.close(); return customer; } catch (sqlexception e) { throw new runtimeexception(e); } finally { if (conn != null) { try { conn.close(); } catch (sqlexception e) {} } } } }
file : spring-customer.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerdao" class="com.h3.customer.dao.impl.jdbccustomerdao"> <property name="datasource" ref="datasource" /> </bean> </beans>
file : spring-datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <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/h3java" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> </beans>
file : spring-module.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="database/spring-datasource.xml" /> <import resource="customer/spring-customer.xml" /> </beans>
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.h3.customer.dao.customerdao; import com.h3.customer.model.customer; public class app { public static void main( string[] args ) { applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml"); customerdao customerdao = (customerdao) context.getbean("customerdao"); customer customer = new customer(1, "h3",29); customerdao.insert(customer); customer customer1 = customerdao.findbycustomerid(1); system.out.println(customer1); } }
输出结果:
customer [age=29, custid=1, name=h3]