zoukankan      html  css  js  c++  java
  • Swing入门级项目全程实录第1讲

     惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

    1、搭建开发环境(略)
         
         详见统一开发环境配置
         操作系统               XP
         开发工具               MyEclipse8.5
         数据库                  MySql5.1(编码utf-8)
         数据库图形工具     SQLyog
     
    2、创建数据库
     
         数据库db_book
     
    3、创建数据库连接
         
         3.1创建数据库连接包com.java1234.util
     
         3.2创建数据库连接类DbUtil
     
         3.3构建数据库类库jar包mysql-connector-java-3.1.12-bin
     
         3.4创建数据库连接变量dbUrl、dbUser、dbPassword、dbDriver    
          private String dbUrl="jdbc:mysql://localhost:3306/db_book";
         private String dbUser="root";
         private String dbPassword="123456";
         private String dbDriver="com.mysql.jdbc.Driver";
         3.5创建数据库连接方法getCon
       public Connection getCon() throws Exception{
              Class.forName(dbDriver);
              Connection con=DriverManager.getConnection(dbUrl,dbUser,dbPassword);
              return con;
         }
         3.6创建数据库关闭方法closeCon 
      public void closeCon(Connection con) throws Exception{
              if(con!=null){
                   con.close();
              }
         }
         3.7数据库连接关闭测试方法main
      public static void main(String[] args) {
              DbUtil dbUtil=new DbUtil();
              Connection con=null;
              try {
                   con=dbUtil.getCon();
                   System.out.println("数据库连接成功!");
              } catch (Exception e) {
                   e.printStackTrace();
                   System.out.println("数据库连接失败!");
              }finally{
                   try {
                        dbUtil.closeCon(con);
                        System.out.println("数据库关闭成功!");
                   } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("数据库连接失败!");
                   }
              }
         }

    4、创建登录界面(略)

     
         4.1创建登录界面LogOnFrm,属性title管理员登录
     
         4.2创建3个jLable、1个jTextField、1个jPasswordField、2个jButton控件
              
         4.3修改每个控制的属性及美化(略)
     
    4.3.1去最大化
         resizable去除
    4.3.1设置Frame居中显示
         setLocationRelativeTo(null);
     
    5、创建t_user数据表
     
         5.1创建t_user数据表
     
         5.2数据项id int pk auto、userName varchar 20、password varchar 20
     
         5.3添加数据用户名java1234、密码123456
     
    6、创建user模型
         
         6.1创建com.java1234.model包
         
         6.2创建user类及set、get方法
       /**
         * @return the id
         */
         public int getId() {
              return id;
         }
         /**
         * @param id the id to set
         */
         public void setId(int id) {
              this.id = id;
         }
         /**
         * @return the userName
         */
         public String getUserName() {
              return userName;
         }
         /**
         * @param userName the userName to set
         */
         public void setUserName(String userName) {
              this.userName = userName;
         }
         /**
         * @return the password
         */
         public String getPassword() {
              return password;
         }
         /**
         * @param password the password to set
         */
         public void setPassword(String password) {
              this.password = password;
         }

    7、创建UserDao

     
         7.1创建com.java1234.dao包
     
         7.2创建UserDao类
     
         7.3编写login方法
      public User login(Connection con,User user) throws Exception{
              User resultUser=null;
              String sql="select * from t_user where userName=? and password=?";
              PreparedStatement pstmt=con.prepareStatement(sql);
              pstmt.setString(1, user.getUserName());
              pstmt.setString(2, user.getPassword());
              ResultSet rs=pstmt.executeQuery();
             
              if(rs.next()){
                   resultUser=new User();
                   resultUser.setUserName(rs.getString("userName"));
                   resultUser.setPassword(rs.getString("password"));
              }         
              return resultUser;
         }

    8、编写LogOnFrm事件

         
         8.1编写登录事件jb_logon
         
              8.1.1获取数据并判断数据不为空
          String userName=jt_username.getText();
              String password=new String(jt_password.getPassword());
             
              if(StringUtil.isEmpty(userName)){
                   JOptionPane.showMessageDialog(null, "用户名不能为空");
              }
              if(StringUtil.isEmpty(password)){
                   JOptionPane.showMessageDialog(null, "密码不能为空");
              }
        public static boolean isEmpty(String str){
            if("".equals(str)||str==null){
                return true;
            }else{
                return false;
            }
        }

              8.1.2重新封装user对象并生成构造方法

      User user=new User(userName,password);
       public User(String userName, String password) {
                   super();
                   this.userName = userName;
                   this.password = password;              
    
       }
              8.1.3连接数据库并验证登录信息
           DbUtil dbUtil = new DbUtil();
             UserDao userDao = new UserDao();
          Connection con=null;
              try {
                   con=dbUtil.getCon();
                   User currentUser=userDao.login(con, user);
                   if(currentUser!=null){
                        JOptionPane.showMessageDialog(null, "登录成功");
                   }else{
                        JOptionPane.showMessageDialog(null, "登录失败");
                   }              
              } catch (Exception e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();               JOptionPane.showMessageDialog(null, "登录失败");
              }finally{
                   try {
                        dbUtil.closeCon(con);
                   } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                   }
              }
      8.2、编写重置事件jb_reset
         jt_username.setText("");
         jt_password.setText("");
  • 相关阅读:
    域名、主机名、网站名以及 URL 基础概念
    c# oracle 数据库连接以及参数化查询
    c# 委托(Func、Action)
    xcode pod install 安装失败,提示缺少文件
    一个服务器的IIS只能绑定一个HTTPS也就是443端口
    APP UI设计及切图规范
    Day7 字符串和常用数据结构
    Day6 函数和模块的使用
    Day5 练习
    python 疑难杂症
  • 原文地址:https://www.cnblogs.com/cnmotive/p/3123546.html
Copyright © 2011-2022 走看看