⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectdata.java~55~

📁 我从网上下的,好像还行,和大家分享一下,
💻 JAVA~55~
字号:
package atm;
import java.sql.*;
import javax.swing.JOptionPane;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ConnectData {
  //声明用户的相关信息变量值
  public static String passWord=null;
  public static String ID=null;
  public static int money=0;
  public static int getMoney=0;
  //数据库相关变量
   Connection con=null;
   Statement stmt=null;
   ResultSet rs=null;
  //判断是否是存在的用户
  boolean isTrue=false;

  /**
   * 定义连接数据库的方法
   */
  public void connect()throws Exception{

    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(ClassNotFoundException e){
        System.out.println("加载驱动出错");
    }

   try{
       con=DriverManager.getConnection("jdbc:odbc:atm");
    }catch(SQLException e){
        System.out.println("建立连接失败");
      }

   try{
      stmt=con.createStatement();
    }catch(SQLException e){
         System.out.println("建立对象失败");
     }

  }


  /**
   *   定义用户登录判断方法
   */

    public boolean isUser(String ID,String passWord){

    //定义查找输入的用户ID和密码是在数据库中处在的SQL语句
    String sql="select * from userInfo where ID='"+ID+"'and passWord='"+passWord+"'";
    //调用连接数据库的方法
    try{
      connect();
    }catch(Exception e){
      System.out.println("登录时建立数据库连接出错");
    }
    //执行查找
    try{
      rs=stmt.executeQuery(sql);
     }catch(SQLException e){
      System.out.println("登录时查找用户失败");
    }

    try{
      if(rs.next()){
        isTrue=true;
        this.ID=rs.getString("ID");
        this.passWord=rs.getString("passWord");
        this.money=rs.getInt("money");
      }
      else
        JOptionPane.showMessageDialog(null,"对不起,您输入的信息不正确,请重新输入","信息提示",1);
     }catch(SQLException e){
        System.out.println("登录时读取数据出错");
    }
    try{
      rs.close();
      stmt.close();
      con.close();
    }catch(SQLException e){
      System.out.println("登录时关闭对象连接失败");
    }
    return isTrue;
  }
  /**
   * 定义修改用户密码的方法
   */
  public void changePass(String newPass){
    con=null;
    stmt=null;
    rs=null;
    int value=0;
    //定义修改密码的SQL语句
    String sql1="update userInfo set passWord='"+newPass+"'where ID='"+ID+"'";
    //调用连接数据库的方法
    try{
      connect();
    }catch(Exception e1){
       System.out.println("修改密码时更新数据库出错");
    }

   //执行查找
    try{
       value=stmt.executeUpdate(sql1);
       this.passWord=newPass;
     }catch(SQLException e){
      System.out.println("执行查询失败");
     }
    try{
      stmt.close();
      con.close();
    }catch(SQLException e){
      System.out.println("关闭对象连接失败");
    }

  }

  //定义取款或存款的方法
  public void saveOrFetch(int m){
    //update
     con=null;
     stmt=null;
     rs=null;
    getMoney=m+this.money;
    //定义取款SQL语句
    String sql2="update userInfo set money = "+getMoney+" where ID='"+ID+"'";
 //调用连接数据库的方法
   try{
     connect();
   } catch(Exception e){
     System.out.println("存取款时更新数据库出错");
  }


   try{
      stmt.executeUpdate(sql2);
      this.money=getMoney;
    }
   catch(SQLException e){
       System.out.println("存取款失败");
   }
 try{
   stmt.close();
   con.close();
 }catch(SQLException e){
   System.out.println("关闭对象连接失败");
 }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -