java dbutils自动建表_java使用dbutils工具类实现小程序 管家婆记账软件

packagecom.company.gjp.view;/** 视图层,用户看到和操作的界面

* 数据传递给controller层实现

* 成员位置,创建controller对象*/

importjava.util.List;importjava.util.Scanner;importcom.company.gjp.controller.ZhangWuController;importcom.company.gjp.domain.ZhangWu;public classMainView {private ZhangWuController controller=newZhangWuController();/** 实现界面效果

* 接受用户的输入

* 根据数据,调用不同的功能方法*/

public voidrun(){//创建Scanner类对象,反复键盘输入

Scanner sc=newScanner(System.in);while(true){

System.out.println("---------------管家婆家庭记账软件---------------");

System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");

System.out.println("请输入要操作的功能序号[1-5]:");//接受用户的选择

int choose=sc.nextInt();//对选择的菜单判断,调用不同的功能

switch(choose){case 1://选择添加账务,调用添加账务的方法

addZhangWu();break;case 2:

editZhangWu();//编辑账务,调用编辑账务的方法

break;case 3:

deleteZhangWu();//选择的删除账务,调用删除账务方法

break;case 4:

selectZhangWu();//选择查询账务,调用查询账务的方法

break;case 5:

System.exit(0);

}

}

}/*** 接受键盘输入的账务id

**/

public voiddeleteZhangWu(){

selectAll();

System.out.println("选择的是删除功能,请输入你要删除的账务ID");

Scanner sc=newScanner(System.in);int zwid=sc.nextInt();

controller.deleteZhangWu(zwid);

System.out.println("删除账务成功!");

}/*** 接受键盘输入

* 数据的信息,封装成ZhangWu对象

* 调用控制层的方法,传递ZhangWu对象,实现编辑*/

public voideditZhangWu(){//用查询所有账务数据的功能,显示出来//看到所有数据,从中选择一项,进行修改

selectAll();

System.out.println("选择的是编辑功能,请输入数据");

Scanner sc=newScanner(System.in);//接受用户的数据

System.out.println("输入ID:");int zwid=sc.nextInt();

System.out.print("请输入类别:");

String flname=sc.next();

System.out.print("请输入账户:");

String zhanghu=sc.next();

System.out.print("请输入金额:");

Double money=sc.nextDouble();

System.out.print("请输入时间:");

String createtime=sc.next();

System.out.print("请输入说明:");

String description=sc.next();//将所有用户输入的数据,封装到ZhangWu对象中//输入的ID,必须封装对象

ZhangWu zw=newZhangWu(zwid,flname,money,zhanghu,createtime,description);//调用controller层中的方法,编辑账务

controller.editZhangWu(zw);

System.out.println("账务编辑成功!");

}/*** 接受键盘输入,5项输入,调用controller层方法*/

public voidaddZhangWu(){

Scanner sc= newScanner(System.in);

System.out.print("请输入类别:");

String flname=sc.next();

System.out.print("请输入账户:");

String zhanghu=sc.next();

System.out.print("请输入金额:");

Double money=sc.nextDouble();

System.out.print("请输入时间:");

String createtime=sc.next();

System.out.print("请输入说明:");

String description=sc.next();//将接收到的数据,调用controller层的方法,传递参数//将用户输入的所有参数,封装成ZhangWu对象

ZhangWu zw=new ZhangWu(0,flname,money,zhanghu,createtime,description);

controller.addZhangWu(zw);

System.out.println("恭喜你添加账务成功");

}/** 定义方法selectZhangWu()

* 显示查询的方式1.所有查询 2.条件查询

* 接受用户的选择*/

public voidselectZhangWu(){

System.out.println("1.查询所有 2.条件查询");

Scanner sc=newScanner(System.in);int selectChoose=sc.nextInt();//判断根据用户的选择,调用不同的功能

switch(selectChoose){case 1:

selectAll();break;case 2:

select();break;

}

}/*** 定义方法,实现查询所有的账务数据*/

public voidselectAll(){//调用控制中的方法,查询账务的数据

List list=controller.selectAll();//输出表头

print(list);

}/*** 定义方法,实现条件查询账务数据

* 提供用户的输入日期,开始日期结束日期

* 就2个日期,传递到controller层

* 调用controller的方法,传递2个日期参数

* 获取controller查询的结果集,打印出来*/

public voidselect(){

System.out.println("选择条件查询,格式xxxx-xx-xx");

Scanner sc=newScanner(System.in);

System.out.println("请输入开始日期:");

String startDate=sc.nextLine();

System.out.println("输入结束的日期:");

String endDate=sc.nextLine();//调用controller层的方法,传递日期,获取查询结果集

List list=controller.select(startDate, endDate);if(list.size()!=0){

print(list);

}else{

System.out.println("没有查询到数据");

}

}//输出账务数据方法,接受List集合,遍历集合,输出表格

private void print(Listlist) {

System.out.println("ID\t\t类别\t\t账户\t\t金额\t\t时间\t\t说明");//遍历集合,结果输出控制台

for(ZhangWu zw:list){

System.out.println(zw.getZwid()+"\t\t"+zw.getFlname()+"\t\t"+zw.getZhanghu()+"\t\t"+zw.getMoney()+"\t\t"+zw.getCreatetime()+"\t\t"+zw.getDescription());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.parddu.dao; import java.io.IOException; import java.sql.*; import java.util.Properties; /** * 数据库功能类 * @author parddu * @version Sep 29, 2010 9:49:31 AM */ class DButil { private String driver=null; //驱动 private String dbName=null; //数据库名 private String host=null; //主机名 private String point=null; //端口 private String userName=null; //登录帐号 private String userPass=null; //登录密码 private static DButil info = null; private DButil(){} /** * 初始化方法,加载数据库连接信息 * @throws IOException */ private static void init() throws IOException{ Properties prop = new Properties(); prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop.getProperty("host"); info.point = prop.getProperty("point"); info.userName = prop.getProperty("userName"); info.userPass = prop.getProperty("userPass"); } /** * 得到数据库连接对象 * @return 数据库连接对象 */ static Connection getConn(){ Connection conn=null; if(info == null){ try { init(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } if(info!=null){ try { Class.forName(info.driver); String url="jdbc:sqlserver://" + info.host + ":" + info.point + ";databaseName=" + info.dbName; conn=DriverManager.getConnection(url,info.userName,info.userPass); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } else{ throw new RuntimeException("读取数据库配置信息异常!"); } return conn; } /** * 关闭查询数据库访问对象 * @param rs 结果集 * @param st 上下文 * @param conn 连接对象 */ static void closeConn(ResultSet rs, Statement st,Connection conn){ try { rs.close(); } catch (Exception e) {} try { st.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e) {} } /** * 关闭增、删、该数据库访问对象 * @param st 上下文对象 * @param conn 连接对象 */ static void closeConn(Statement st ,Connection conn){ try{ st.close(); conn.close(); }catch(Exception e){} } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值