jdbc:客户信息管理系统:工具类,异常类,测试类,数据库配置文件
2014-06-29 00:00
806 查看
工具类:
public class JdbcUtil {
private static String driverClass;
private static String url;
private static String user;
private static String password;
static{
//读取配置文件
try {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbcfg.properties");
Properties props = new Properties();
props.load(in);
driverClass = props.getProperty("driverClass");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
Class.forName(driverClass);
} catch (Exception e) {
throw new ExceptionInInitializerError("配置文件读取错误");
}
}
public static Connection getConnection() throws Exception{
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
public class WebUtil {
public static <T>T fillBean(HttpServletRequest request,
Class<T> class1) {
T bean;
try {
bean = class1.newInstance();
BeanUtils.populate(bean, request.getParameterMap());
return bean;
} catch (Exception e) {
throw new RuntimeException();
}
}
}
异常类:
public class CustomerIdCannotBeEmpty extends Exception {
public CustomerIdCannotBeEmpty() {
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
测试类:
public class BussinessServletImplTest {
private BussinessService s=new BussinessServletImpl();
// @Test
// public void testFindAll() {
// s.findAll();
// }
@Test
public void testAddCustomer() {
Customer c=new Customer();
c.setId("1");
c.setName("戴佳伟");
c.setGender("1");
c.setBirthday(new Date());
c.setEmail("djw@qq.com");
c.setCellphone("18768190425");
c.setPreference("玩游戏");
c.setType("vip");
c.setDescription("学生");
s.addCustomer(c);
}
// @Test
// public void testDelCustomer() {
// s.delCustomer("1");
// }
// @Test
// public void testFindCustomerById() {
// s.findCustomerById("1");
// }
// @Test(expected=com.itcast.exception.CustomerIdCannotBeEmpty.class)
// public void testUpdateCustomer() throws CustomerIdCannotBeEmpty {
// Customer c=new Customer();
// c.setId("1");
// c.setName("周贝特");
// c.setGender("1");
// c.setBirthday(new Date());
// c.setEmail("djw@qq.com");
// c.setCellphone("18768190425");
// c.setPreference("玩游戏");
// c.setType("vip");
// c.setDescription("学生");
// s.updateCustomer(c);
// }
}
选择数据库的配置文件:
dbcfg.properties:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day17
user=root
password=sorry
public class JdbcUtil {
private static String driverClass;
private static String url;
private static String user;
private static String password;
static{
//读取配置文件
try {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbcfg.properties");
Properties props = new Properties();
props.load(in);
driverClass = props.getProperty("driverClass");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
Class.forName(driverClass);
} catch (Exception e) {
throw new ExceptionInInitializerError("配置文件读取错误");
}
}
public static Connection getConnection() throws Exception{
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
public class WebUtil {
public static <T>T fillBean(HttpServletRequest request,
Class<T> class1) {
T bean;
try {
bean = class1.newInstance();
BeanUtils.populate(bean, request.getParameterMap());
return bean;
} catch (Exception e) {
throw new RuntimeException();
}
}
}
异常类:
public class CustomerIdCannotBeEmpty extends Exception {
public CustomerIdCannotBeEmpty() {
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public CustomerIdCannotBeEmpty(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
测试类:
public class BussinessServletImplTest {
private BussinessService s=new BussinessServletImpl();
// @Test
// public void testFindAll() {
// s.findAll();
// }
@Test
public void testAddCustomer() {
Customer c=new Customer();
c.setId("1");
c.setName("戴佳伟");
c.setGender("1");
c.setBirthday(new Date());
c.setEmail("djw@qq.com");
c.setCellphone("18768190425");
c.setPreference("玩游戏");
c.setType("vip");
c.setDescription("学生");
s.addCustomer(c);
}
// @Test
// public void testDelCustomer() {
// s.delCustomer("1");
// }
// @Test
// public void testFindCustomerById() {
// s.findCustomerById("1");
// }
// @Test(expected=com.itcast.exception.CustomerIdCannotBeEmpty.class)
// public void testUpdateCustomer() throws CustomerIdCannotBeEmpty {
// Customer c=new Customer();
// c.setId("1");
// c.setName("周贝特");
// c.setGender("1");
// c.setBirthday(new Date());
// c.setEmail("djw@qq.com");
// c.setCellphone("18768190425");
// c.setPreference("玩游戏");
// c.setType("vip");
// c.setDescription("学生");
// s.updateCustomer(c);
// }
}
选择数据库的配置文件:
dbcfg.properties:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day17
user=root
password=sorry
相关文章推荐
- jdbc数据库连接管理封装工具类,不同使用属性文件配置数据库连接信息(1)
- jdbc数据库连接管理封装工具类,不同使用属性文件配置数据库连接信息(3)
- jdbc,数据库案例:客户信息管理系统:业务层接口和实现,数据控制servlet
- jdbc,数据库案例:客户信息管理系统:jsp部分,数据库数据封装类,dao接口和实现
- jdbc数据库连接管理封装工具类,不同使用属性文件配置数据库连接信息(2)
- mini学生管理系统。。。全部代码,4个java文件放在同一个包下面即可。用JDBC调用数据库取出数据。
- jdbc将数据库连接信息放置配置文件中
- Java之数据库MYSQL、JDBC工具类、properties配置文件
- 学生信息管理系统——数据库连接文件
- jdbc工具类通过配置文件连接数据库学习理解
- 话说Java jdbc数据库连接,从配置文件中读取连接基本信息实例
- JavaWeb之JDBC(二)采用读取配置文件方式编写JDBC的工具类,获取数据库的连接
- 一个依赖配置属性文件管理的JDBC工具类。
- 使用配置文件来存储数据库链接信息时---工具类的编写
- JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷
- JDBC连接数据库查询信息的步骤(提取成配置文件方式)
- JDBC 通过从properties配置文件获取信息得到orcale数据库接口
- 学生信息管理系统——数据库配置
- jdbc的数据库连接信息在属性文件中的配置(db.properties)
- javaJDBC(2)-抽取jdbcutil及配置文件读取数据库信息