jdbc:客户信息管理系统:工具类,异常类,测试类,数据库配置文件

工具类:mysql

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;
  }
 }
}sql

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 {url

 public CustomerIdCannotBeEmpty() {
  // TODO Auto-generated constructor stub
 }.net

 public CustomerIdCannotBeEmpty(String message) {
  super(message);
  // TODO Auto-generated constructor stub
 }游戏

 public CustomerIdCannotBeEmpty(Throwable cause) {
  super(cause);
  // TODO Auto-generated constructor stub
 }ip

 public CustomerIdCannotBeEmpty(String message, Throwable cause) {
  super(message, cause);
  // TODO Auto-generated constructor stub
 }get

}

测试类:

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.Driverurl=jdbc:mysql://localhost:3306/day17user=rootpassword=sorry

相关文章
相关标签/搜索