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

相關文章
相關標籤/搜索