java面向对象

一,jdbc编程

JDBCJava Database ConnectivityJava数据库连接是一种可以执行SQL语句的Java API

public static void main(String[] args) {
		
		final String DRIVER="com.mysql.jdbc.Driver";
		final String URL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
		final String NAME="root";
		final String PWD="root";//123456
		PreparedStatement ps = null;
		Connection conn =null;
		try {
			//1,注册驱动
			Class.forName(DRIVER);
			//2,获取连接
			conn = DriverManager.getConnection(URL, NAME, PWD);
			//3,编写sql语句
			String sql="insert into user values(null,?,?,?,?)";
			//4,创建sql处理对象
			ps = conn.prepareStatement(sql);
			//5,给sql处理对象设置值
			ps.setString(1, "阳锦辉");
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String riqi = sdf.format(date);
			ps.setString(2, riqi);
			ps.setString(3, "男");
			ps.setString(4, "长沙南方");
			//6,执行sql语句(增删改都是用的executeUpdate,查询用executeQuery)
			int num = ps.executeUpdate();
			System.out.println(num);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//7,关闭资源
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

二,JavaSwing

1,如何创建第一个窗体

创建窗体对象之后再对窗体进行设置

public class Test {
	public static void main(String[] args) {
		JFrame jFrame = new JFrame();
		// 设置窗体的标题
		jFrame.setTitle("这是一个窗体");
		// 设置窗体的位置和大小setBounds(x,y,weight,height)
		jFrame.setBounds(100, 200, 500, 400);
		// 设置窗体图片
		ImageIcon icon = new ImageIcon("D:\\image\\06.png");
		jFrame.setIconImage(icon.getImage());
		// 设置不能调整大小
		jFrame.setResizable(false);
		// 设置窗体关闭时退出程序
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 必须设置窗体可见
		jFrame.setVisible(true);
    }
}

在开发中一般都是通过构造方法设置窗体的属性

public class MainSwing extends JFrame {
	public MainSwing() {
		// 设置窗体的标题
		this.setTitle("这是一个窗体");
		// 设置窗体的位置和大小setBounds(x,y,weight,height)
		this.setBounds(100, 200, 500, 400);
		// 设置窗体图片
		ImageIcon icon = new ImageIcon("D:\\image\\06.png");
		this.setIconImage(icon.getImage());
		// 设置不能调整大小
		this.setResizable(false);
		// 设置窗体关闭时退出程序
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		// 必须设置窗体可见
		this.setVisible(true);
	}
}

2,布局管理器的使用

布局管理器的作用:告诉窗体,其内部控件如何摆放

java中有哪些布局管理器:

/**
         * 流式布局:从左到右,从上到下 
         * 网格布局:表格的形式,需要设置行数和列数 
         * 边框布局:依据方向布局,添加控件的时候要指明方向
         */

2.1流式布局:

public class FlowSwing extends JFrame {
	public FlowSwing() {
		// 设置窗体的标题
		this.setTitle("这是一个窗体");
		// 设置窗体的位置和大小setBounds(x,y,weight,height)
		this.setBounds(100, 200, 500, 400);
		// 设置窗体图片
		ImageIcon icon = new ImageIcon("D:\\image\\06.png");
		this.setIconImage(icon.getImage());
		// 设置不能调整大小
		this.setResizable(false);
		// 设置窗体关闭时退出程序
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		//创建按钮对象
		JButton jButton1 = new JButton("登录");
		JButton jButton2 = new JButton("注册");
		JButton jButton3 = new JButton("帮助");
		JButton jButton4 = new JButton("退出");
		JButton jButton5 = new JButton("新建");
		/**
		 * 设置当前窗体的布局为流式布局
		 * (默认为居中,FlowLayout.RIGHT居右,FlowLayout.LEFT居左,
后面的两个参数表示垂直和水平方向的间距)
		 */
		this.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
		//将按钮添加到窗体:不设置布局器的情况下默认将元素充满整个窗口并依次叠加
		this.add(jButton1);
		this.add(jButton2);
		this.add(jButton3);
		this.add(jButton4);
		this.add(jButton5);
		// 必须设置窗体可见
		this.setVisible(true);
	}

}

2.2网格布局:

public class GridSwing extends JFrame {
	public GridSwing() {
		// 设置窗体的标题
		this.setTitle("这是一个窗体");
		// 设置窗体的位置和大小setBounds(x,y,weight,height)
		this.setBounds(100, 200, 500, 400);
		// 设置窗体图片
		ImageIcon icon = new ImageIcon("D:\\image\\06.png");
		this.setIconImage(icon.getImage());
		// 设置不能调整大小
		this.setResizable(false);
		// 设置窗体关闭时退出程序
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		//创建按钮对象
		JButton jButton1 = new JButton("登录");
		JButton jButton2 = new JButton("注册");
		JButton jButton3 = new JButton("帮助");
		JButton jButton4 = new JButton("退出");
		JButton jButton5 = new JButton("新建");
		JButton jButton6 = new JButton("新建");
		JButton jButton7 = new JButton("新建");
		JButton jButton8 = new JButton("新建");
		JButton jButton9 = new JButton("新建");
		/**
		 * 设置当前窗体的布局为网格布局,传参为行数和列数
		 */
		GridLayout gridLayout = new GridLayout(3,3);
		gridLayout.setHgap(5);//设置水平间距
		gridLayout.setVgap(5);//设置垂直间距
		this.setLayout(gridLayout);
		//将按钮添加到窗体:不设置布局器的情况下默认将元素充满整个窗口并依次叠加
		this.add(jButton1);
		this.add(jButton2);
		this.add(jButton3);
		this.add(jButton4);
		this.add(jButton5);
		this.add(jButton6);
		this.add(jButton7);
		this.add(jButton8);
		this.add(jButton9);
		// 必须设置窗体可见
		this.setVisible(true);
	}
}

2.3边框布局:

public class BorderSwing extends JFrame {
	/**
	 * 流式布局:从左到右,从上到下
	 * 网格布局:表格的形式,需要设置行数和列数
	 * 边框布局:依据方向布局,添加控件的时候要指明方向
	 */
	public BorderSwing() {
		// 设置窗体的标题
		this.setTitle("这是一个窗体");
		// 设置窗体的位置和大小setBounds(x,y,weight,height)
		this.setBounds(100, 200, 500, 400);
		// 设置窗体图片
		ImageIcon icon = new ImageIcon("D:\\image\\06.png");
		this.setIconImage(icon.getImage());
		// 设置不能调整大小
		this.setResizable(false);
		// 设置窗体关闭时退出程序
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		//创建按钮对象
		JButton jButton1 = new JButton("登录");
		JButton jButton2 = new JButton("注册");
		JButton jButton3 = new JButton("帮助");
		JButton jButton4 = new JButton("退出");
		JButton jButton5 = new JButton("新建");
		/**
		 * 设置当前窗体的布局为边框布局,传参为行数和列数
		 */
		BorderLayout borderLayout = new BorderLayout();
		this.setLayout(borderLayout);
		//将按钮添加到窗体:不设置布局器的情况下默认将元素充满整个窗口并依次叠加
		this.add(jButton1,borderLayout.EAST);
		this.add(jButton2,borderLayout.SOUTH);
		this.add(jButton3,borderLayout.WEST);
		this.add(jButton4,borderLayout.NORTH);
		this.add(jButton5);
		// 必须设置窗体可见
		this.setVisible(true);
	}
}

2.4.绝对定位

public class MySwing extends JFrame {
	//文本框
	JLabel jLabel;
	JLabel jLabel2;
	//输入框
	JTextField jt1;
	//密码框
	JPasswordField jp1;
	//按钮
	JButton btn,btn2;
	public MySwing() {
		this.setTitle("控件的使用");
		this.setBounds(200, 200, 500, 400);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//使用绝对定位,这句话不能省
		this.setLayout(null);
		
		jLabel=new JLabel("用户名");
		jLabel.setBounds(150, 50, 50, 20);
		
		jt1=new JTextField(10);
		jt1.setBounds(200, 50, 100, 20);
		
		jLabel2=new JLabel("密码");
		jLabel2.setBounds(150, 100, 50, 20);
		
		jp1=new JPasswordField(10);
		jp1.setBounds(200, 100, 100, 20);
		
		btn=new JButton("提交");
		btn.setBounds(150, 150, 70, 20);
		
		btn2=new JButton("提交");
		btn2.setBounds(250, 150, 70, 20);
		
		this.add(jLabel);
		this.add(jLabel2);
		this.add(jt1);
		this.add(jp1);
		this.add(btn);
		this.add(btn2);
		
		this.setVisible(true);
	}
}

3.常用组件的使用

表格及基本组件的使用

public class MySwing extends JFrame {
	public MySwing() {
		this.setTitle("控件的使用");
		this.setBounds(200, 200, 500, 400);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		//文本框
		JLabel jLabel=new JLabel("用户名");
		//输入框
		JTextField jt1=new JTextField(10);
		//创建图标
		ImageIcon icon = new ImageIcon("D://image/06.png");
		//设置图标的大小
		icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
		//文本框中添加图标
		JLabel jLabel2=new JLabel(icon);
		//密码框
		JPasswordField jp1=new JPasswordField(10);
		//按钮
		JButton btn=new JButton("提交");
		JButton btn2=new JButton("关闭");
		//下拉框
		JComboBox<String> jComboBox = new JComboBox<String>();
		jComboBox.addItem("中国");
		jComboBox.addItem("美国");
		jComboBox.addItem("英国");
		jComboBox.addItem("韩国");
		//设置下拉框的最大显示条数
		jComboBox.setMaximumRowCount(3);
		//创建按钮组
		ButtonGroup buttonGroup = new ButtonGroup();
		//创建单选框
		JRadioButton jRadioButton = new JRadioButton("男");
		JRadioButton jRadioButton2 = new JRadioButton("女");
		//将单选框添加至按钮组
		buttonGroup.add(jRadioButton);
		buttonGroup.add(jRadioButton2);
		//设置表头
		String[] title= {"姓名","年龄","电话"};
		//设置表格关联数据
		Object [][]ob= {
				{"张三","12","243209832405"},
				{"李四","23","243209832405"},
				{"王五","34","243209832405"}
		};
		//创建表格
		JTable jTable = new JTable(ob,title);
		//创建滚动面板
		JScrollPane jScrollPane = new JScrollPane(jTable, 
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		this.add(jLabel);
		this.add(jLabel2);
		this.add(jt1);
		this.add(jp1);
		this.add(btn);
		this.add(btn2);
		this.add(jComboBox);
		this.add(jRadioButton);
		this.add(jRadioButton2);
		this.add(jScrollPane);
		
		this.setVisible(true);
	}
}

菜单条的使用

public class JMenueSwing extends JFrame{
	public JMenueSwing() {
		this.setTitle("菜单条的使用");
		this.setBounds(200, 200, 500, 400);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		//创建菜单条
		JMenuBar jMenuBar = new JMenuBar();
		//创建菜单
		JMenu jMenu1 = new JMenu("文件");
		//创建菜单项
		JMenuItem jMenuItem1 = new JMenuItem("新建");
		JMenuItem jMenuItem2 = new JMenuItem("打开");
		JMenuItem jMenuItem3 = new JMenuItem("保存");
		//将菜单项添加到菜单
		jMenu1.add(jMenuItem1);
		jMenu1.add(jMenuItem2);
		jMenu1.add(jMenuItem3);
		JMenu jMenu2 = new JMenu("编辑");
		JMenu jMenu3 = new JMenu("格式");
		//将菜单添加到菜单条
		jMenuBar.add(jMenu1);
		jMenuBar.add(jMenu2);
		jMenuBar.add(jMenu3);
		//将菜单条添加到窗体
		this.setJMenuBar(jMenuBar);
		//设置窗体显示
		this.setVisible(true);
	}
}

4,项目实践

题目:维护洗衣店消费项数据

语言和环境

实现语言:Java语言。

环境要求:Eclipse或Myeclipse+MySQL。

功能需求

利用Java Swing和JDBC技术维护洗衣店消费项数据。

具体要求如下:

首界面显示洗衣店所有的消费项目信息,如图1所示。

 图1 显示所有消费项信息

点击“新增”按钮,弹出消费项新增界面,如图2所示。输入消费项相关信息之后,点击“新增”按钮,在数据库中新增一条消费项记录,并返回至图1刷新消费项数据;点击“返回”按钮则取消新增,返回至图1。

图2 新增消费项

在图1所示的消费项显示界面中,选中所需修改的消费项,点击“修改”按钮,弹出消费项修改界面,如图3所示。消费项信息修改完毕之后,点击“修改”按钮,完成该消费项信息的更新,并返回至图1刷新消费项数据;点击“返回”按钮,则取消更新,返回至图1。

图3 修改消费项数据

三、数据库设计

数据库名为db_cosume,表名为tb_items,具体表结构见下表。

序号

字段名称

字段说明

类型

属性

1

id

消费项编号

int

主键,自动增长1

2

item_name

消费项名称

varchar(30)

非空

3

unit_price

单价

decimal(4,1)

非空

4

member_price

会员价

decimal(4,1)

非空

四、注意事项:

在工程中添加MySQL驱动。

注意程序逻辑分明、命名规范以及书写有缩进。

添加适当的注释。

消费项测试数据至少三条。

首页设计如下:

public class Index extends JFrame{
	public Index() {
		//设置标题
		this.setTitle("消费项管理");
		//设置大小不可改变
		this.setResizable(false);
		//设置窗体大小和位置
		this.setBounds(200, 200, 500, 400);
		this.setLocationRelativeTo(null);//设置窗体居中
		//关闭窗体停止程序
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//添加布局器
		//FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
		this.setLayout(null);
		
		JButton jButton = new JButton("新增");
		jButton.setBounds(300, 20, 70, 20);
		JButton jButton2 = new JButton("修改");
		jButton2.setBounds(380, 20, 70, 20);
		//调用CosumeDao对象查询数据库
		CosumeDao cosumeDao = new CosumeDao();
		DefaultTableModel model = cosumeDao.findAllItems();
		
		//将数据绑定到表格
		JTable jTable = new JTable(model);
		JScrollPane jScrollPane = new JScrollPane(jTable);
		jScrollPane.setBounds(0, 50, 480, 100);
		//事件处理
		//"新增按钮"
		jButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Add add = new Add(jTable);
				//注销当前窗体
				//dispose();
			}
		});
		//修改按钮
		jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取table选中行
				int selectedRow = jTable.getSelectedRow();
				if(selectedRow>=0) {
					//获取数据传入update窗体
					Object id = jTable.getValueAt(selectedRow, 0);
					Object item_name = jTable.getValueAt(selectedRow, 1);
					Object unit_price = jTable.getValueAt(selectedRow, 2);
					Object member_price = jTable.getValueAt(selectedRow, 3);
					Update update = new Update(jTable,id, item_name, unit_price, member_price);
					//System.out.println(id.toString()+item_name+unit_price+member_price);
				}else {
					JOptionPane.showMessageDialog(null, "请选择一行数据");
				}
				
			}
		});
		
		this.add(jButton);
		this.add(jButton2);
		this.add(jScrollPane);
		//设置窗体可见
		this.setVisible(true);
	}

}

添加界面

public class Add extends JFrame {
	public Add(JTable jTable) {
		// 设置标题
		this.setTitle("新增消费项");
		// 设置大小不可改变
		this.setResizable(false);
		// 设置窗体大小和位置
		this.setBounds(200, 200, 500, 400);
		this.setLocationRelativeTo(null);// 设置窗体居中
		// 关闭窗体停止程序
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 添加布局器
		//FlowLayout flowLayout = new FlowLayout();
		this.setLayout(null);
		
		JLabel jLabel = new JLabel("名称:");
		jLabel.setBounds(100, 50, 50, 20);
		JTextField jTextField = new JTextField(10);
		jTextField.setBounds(170, 50, 100, 20);
		
		JLabel jLabel2 = new JLabel("单价:");
		jLabel2.setBounds(100, 100, 50, 20);
		JTextField jTextField2 = new JTextField(10);
		jTextField2.setBounds(170, 100, 100, 20);
		
		JLabel jLabel3 = new JLabel("会员价:");
		jLabel3.setBounds(100, 150, 50, 20);
		JTextField jTextField3 = new JTextField(10);
		jTextField3.setBounds(170, 150, 100, 20);
		
		JButton jButton = new JButton("新增");
		jButton.setBounds(100, 200, 70, 20);
		JButton jButton2 = new JButton("返回");
		jButton2.setBounds(200, 200, 70, 20);
		
		this.add(jLabel);
		this.add(jTextField);
		this.add(jLabel2);
		this.add(jTextField2);
		this.add(jLabel3);
		this.add(jTextField3);
		this.add(jButton);
		this.add(jButton2);
		//事件处理
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取文本输入框的值
				String item_name = jTextField.getText();
				Double unit_price = Double.valueOf(jTextField2.getText());
				Double member_price = Double.valueOf(jTextField3.getText());
				//操作数据库
				CosumeDao cosumeDao = new CosumeDao();
				int num = cosumeDao.addItems(item_name, unit_price, member_price);
				if(num>0) {
					//弹窗
					JOptionPane.showMessageDialog(null, "新增成功");
					//调用CosumeDao对象重新查询数据库
					DefaultTableModel model = cosumeDao.findAllItems();
					jTable.setModel(model);
					//注销当前窗体
					dispose();
					//Index index = new Index();
				}else {
					JOptionPane.showMessageDialog(null, "新增失败");
				}
				System.out.println(num);
			}
		});
		
		jButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
		});
		
		
		//设置窗体可见
		this.setVisible(true);
	}
}

修改界面

public class Update extends JFrame {
	public Update(JTable jTable,Object id,Object item_name,Object unit_price,Object member_price) {
		// 设置标题
		this.setTitle("修改消费项");
		// 设置大小不可改变
		this.setResizable(false);
		// 设置窗体大小和位置
		this.setBounds(200, 200, 500, 400);
		this.setLocationRelativeTo(null);// 设置窗体居中
		// 关闭窗体停止程序
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 添加布局器
		// FlowLayout flowLayout = new FlowLayout();
		this.setLayout(null);

		JLabel jLabel = new JLabel("名称:");
		jLabel.setBounds(100, 50, 50, 20);
		JTextField jTextField = new JTextField(10);
		jTextField.setBounds(170, 50, 100, 20);
		jTextField.setText((String)item_name);

		JLabel jLabel2 = new JLabel("单价:");
		jLabel2.setBounds(100, 100, 50, 20);
		JTextField jTextField2 = new JTextField(10);
		jTextField2.setBounds(170, 100, 100, 20);
		jTextField2.setText(unit_price.toString());
		
		JLabel jLabel3 = new JLabel("会员价:");
		jLabel3.setBounds(100, 150, 50, 20);
		JTextField jTextField3 = new JTextField(10);
		jTextField3.setBounds(170, 150, 100, 20);
		jTextField3.setText(member_price.toString());
		
		JButton jButton = new JButton("修改");
		jButton.setBounds(100, 200, 70, 20);
		JButton jButton2 = new JButton("返回");
		jButton2.setBounds(200, 200, 70, 20);

		this.add(jLabel);
		this.add(jTextField);
		this.add(jLabel2);
		this.add(jTextField2);
		this.add(jLabel3);
		this.add(jTextField3);
		this.add(jButton);
		this.add(jButton2);
		//修改按钮
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//确认框
				int result = JOptionPane.showConfirmDialog(null, "你真的要改吗");
				//System.out.println(result);
				if(result==0) {
					//重新获取输入框的值
					String item_name = jTextField.getText();
					Double unit_price =Double.parseDouble(jTextField2.getText());
					Double member_price =Double.parseDouble(jTextField3.getText());
					//操作数据库
					CosumeDao cosumeDao = new CosumeDao();
					int num=cosumeDao.updateItem(id,item_name,unit_price,member_price);
					if(num>=1) {
						JOptionPane.showMessageDialog(null, "修改成功");
						DefaultTableModel model = cosumeDao.findAllItems();
						jTable.setModel(model);
						dispose();
					}else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}else {
					
				}
				
			}
		});
		
		
		this.setVisible(true);
	}
}

jdbc工具类

public class DBUtile {
	private static final String Driver="com.mysql.jdbc.Driver";
	private static final String Url="jdbc:mysql://127.0.0.1:3306/db_cosume?characterEncoding=utf-8";
	private static final String Name="root";
	private static final String Pwd="root";//123456
	private static Connection conn = null;
	private static PreparedStatement ps =null;
	private static ResultSet rs = null;
	public static int insertDeleteUpdate(String sql,Object...ob) {
		try {
			Class.forName(Driver);
			conn = DriverManager.getConnection(Url,Name,Pwd);
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < ob.length; i++) {
				ps.setObject(i+1, ob[i]);
			}
			int num = ps.executeUpdate();
			return num;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public static HashMap<String, Object> query(String sql,Object...ob) {
		try {
			Class.forName(Driver);
			conn = DriverManager.getConnection(Url,Name,Pwd);
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < ob.length; i++) {
				ps.setObject(i+1, ob[i]);
			}
			rs = ps.executeQuery();
			HashMap<String, Object> obs = new HashMap<String, Object>();
			obs.put("rs", rs);
			obs.put("ps", ps);
			obs.put("conn", conn);
			return obs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void close(HashMap<String, Object> obs) {
		 rs = (ResultSet)obs.get("rs");
		 ps = (PreparedStatement)obs.get("ps");
		 conn = (Connection)obs.get("conn");
		 try {
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

dao操作

public class CosumeDao {
	public DefaultTableModel findAllItems() {
		// 查询数据库
		HashMap<String, Object> comsumes = DBUtile.query("SELECT * FROM tb_items");
		ResultSet rs = (ResultSet) comsumes.get("rs");
		// 重构表格数据
		Vector<Vector<Object>> vectors = new Vector<Vector<Object>>();
		try {
			while (rs.next()) {
				Vector<Object> vector = new Vector<Object>();
				vector.add(rs.getInt("id"));
				vector.add(rs.getString("item_name"));
				vector.add(rs.getDouble("unit_price"));
				vector.add(rs.getDouble("member_price"));
				vectors.add(vector);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 重构表头
		Vector<String> biaotou = new Vector<String>();
		biaotou.add("序号");
		biaotou.add("消费项");
		biaotou.add("单价");
		biaotou.add("会员价");
		DefaultTableModel model = new DefaultTableModel();
		model.setDataVector(vectors, biaotou);
		return model;
	}
	
	public int addItems(String item_name,Double unit_price,Double member_price) {
		String sql="insert into tb_items VALUES(null,?,?,?)";
		return DBUtile.insertDeleteUpdate(sql,item_name,unit_price,member_price );
	}

	public int updateItem(Object id, String item_name, Double unit_price, Double member_price) {
		String sql="update tb_items t set  t.item_name=?,t.unit_price=?,t.member_price=? where t.id=?";
		int num = DBUtile.insertDeleteUpdate(sql, item_name,unit_price,member_price,id);
		return num;
	}
}

主类

public class Test {

	public static void main(String[] args) {
		Index index = new Index();

	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值