JavaCore

上传人:壹****1 文档编号:497800525 上传时间:2023-09-12 格式:DOC 页数:12 大小:169KB
返回 下载 相关 举报
JavaCore_第1页
第1页 / 共12页
JavaCore_第2页
第2页 / 共12页
JavaCore_第3页
第3页 / 共12页
JavaCore_第4页
第4页 / 共12页
JavaCore_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《JavaCore》由会员分享,可在线阅读,更多相关《JavaCore(12页珍藏版)》请在金锄头文库上搜索。

1、 重点代码一:用户登录 1.DAO 代码部分 /判断用户登录是否成功(到数据库中查询用户名和密码是否存在)public boolean find(String userName, String userPwd) String sql = select * from T_User where user_name=? and user_pwd=?;Connection connection = DBManager.getConnection();try PreparedStatement pst = connection.prepareStatement(sql);pst.setString(1,

2、 userName);pst.setString(2, userPwd);ResultSet rs = pst.executeQuery();if (rs!=null&rs.next() return true;DBManager.closeConnection(connection);DBManager.closeStatement(pst);DBManager.closeResultSet(rs); catch (SQLException e) e.printStackTrace();return false; 2.SERVICE 代码部分 /判断用户登录是否成功public boolea

3、n find(String userName, String userPwd) return userDao.find(userName, userPwd); 3.UI 登录按钮的事件处理程序/1、获得用户输入的用户名和密码String userName = jTextField1.getText();String userPwd = jPasswordField1.getText();/2、调用UserService处理业务逻辑判断用户登录是否成功(查询用户名和密码是否存在)UserService userService = new UserService();boolean flag =

4、userService.find(userName, userPwd);/3、根据查询结果弹出提示信息if (flag) JOptionPane.showMessageDialog(this, 恭喜你,登录成功!, 系统登录,JOptionPane.INFORMATION_MESSAGE);this.dispose();/释放当前窗体new MainFrame().setVisible(true);/跳转到主界面 else JOptionPane.showMessageDialog(this, 用户名或密码错误,请重新输入!, 系统登录,JOptionPane.ERROR_MESSAGE);二

5、:用户注册 1.DAO 代码部分 /新用户注册,插入新用户信息到数据库public boolean insert(String userName, String userPwd) boolean flag = true;String sql = insert into T_User values(?,?);Connection connection = DBManager.getConnection();try PreparedStatement pst = connection.prepareStatement(sql);pst.setString(1, userName);pst.setS

6、tring(2, userPwd);pst.execute();DBManager.closeConnection(connection);DBManager.closeStatement(pst); catch (SQLException e) flag = false;e.printStackTrace();return flag; 2.SERVICE 代码部分 /新用户注册public boolean insert(String userName,String userPwd) return userDao.insert(userName, userPwd); 3.UI 注册按钮的事件处

7、理程序 /1、获得用户输入的用户名和密码String userName = jTextField1.getText();String userPwd = jPasswordField1.getText();/2、调用UserService完成业务逻辑处理UserService userService = new UserService();boolean flag = userService.insert(userName, userPwd);/3、处理结果if(flag) JOptionPane.showMessageDialog(this, 恭喜你,注册成功!,系统注册,JOptionPa

8、ne.INFORMATION_MESSAGE);this.dispose();new LoginFrame();/跳转到登录窗口 else JOptionPane.showMessageDialog(this, 注册失败,请重新注册!,系统注册,JOptionPane.ERROR_MESSAGE);三:查询所有用户信息 1.DAO 代码部分 /查找所有用户信息public List findAll() List userList = new ArrayList();String sql = select * from T_User;Connection connection = DBManag

9、er.getConnection();try PreparedStatement pst = connection.prepareStatement(sql);ResultSet rs = pst.executeQuery();while(rs!=null&rs.next() /将数据封装到user对象中User user = new User();user.setUserId(rs.getInt(user_id);user.setUserName(rs.getString(user_name);user.setUserPwd(rs.getString(user_pwd);/将user对象添加

10、到List集合中userList.add(user);DBManager.closeResultSet(rs);DBManager.closeStatement(pst);DBManager.closeConnection(connection); catch (SQLException e) e.printStackTrace();return userList; 2.SERVICE 代码部分 /查询所有用户信息public List findAll() return userDao.findAll();四:增加数据到数据库并显示到表格1.AO 代码部分 /增加(insert)public

11、void insert(Product product) String sql = insert into t_product values(?,?);Connection connection = DBManager.getConnection();try PreparedStatement pst = connection.prepareStatement(sql);pst.setString(1, product.getProductName();pst.setFloat(2, product.getProductPrice();pst.execute();/执行更新语句(包含inser

12、t/delete/update) /pst.executeQuery();/执行查询语句(仅包含select) catch (SQLException e) e.printStackTrace(); 2.SERVICE 代码部分 /增加(insert)public void insert(Product product) productDao.insert(product); 3.UI 添加按钮的事件处理程序 /获得用户输入的新增商品的数据String productName = jTextField2.getText();String productPrice = jTextField3.g

13、etText();/将数据封装到Product对象中Product product = new Product();product.setProductName(productName);product.setProductPrice(Float.parseFloat(productPrice);/调用ProductService完成新增商品的业务逻辑ProductService productService = new ProductService();productService.insert(product);4. 将添加的数据显示到表格中 String columnNames = 商品编号, 商品名称, 商品价格 ;/创建表模式DefaultTableModel model = new DefaultTableModel(columnNames, 0); public void showTable() /刷新Table/1、清空tablewhile (model.getRowCount() 0) model.removeRow(model.getRow

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号