您的位置:首页 > 数据库

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐