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

本文详细介绍了如何通过JDBC连接数据库并执行操作,同时利用WebUtil进行Bean对象的填充,包括数据库配置文件读取、数据库连接获取、释放资源等关键步骤。此外,还展示了如何使用WebUtil将HttpServletRequest转换为JavaBean对象,适用于前端数据交互场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具类:

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

转载于:https://my.oschina.net/u/1589656/blog/285191

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值