对数据库的查询操作:

  private static String DriverName="com.mysql.jdbc.Driver";
  private static String UName="root";
  private static String Upwd="1108.shjzh..sql.lq";
  private static String Url="jdbc:mysql://localhost:3306/login";
  public boolean exit(int sno)   //根据学号查询学生是否存在 如果存在返回true,不存在返回false
  {
      return query(sno)==null?false:true;
  }
  public Student query(int sno)   //查询学生是否存在
  {
      Student student=null;
      Connection conn=null;
      PreparedStatement pstmt=null;
      ResultSet result=null;
      try {
          Class.forName(DriverName);
          conn=DriverManager.getConnection(Url, UName, Upwd);
          String sql="select * from student where sno=?";
          pstmt= conn.prepareStatement(sql);
          pstmt.setInt(1, sno);
          result=pstmt.executeQuery();
          if(result.next())
          {
              int no=result.getInt("sno");
              String name=result.getString("name");
              int age=result.getInt("age");
              String address=result.getString("address");
              student=new Student(no, name, age, address);
          }
          return student;
      }
      catch(ClassNotFoundException e)
      {
          e.printStackTrace();
          return null;
      }
      catch (SQLException e)
      {
        e.printStackTrace();
        return null;
    }
      catch(Exception e)
      {
          e.printStackTrace();
          return null;
      }
      finally {
          try {
              if(result!=null) result.close();
              if(pstmt!=null) pstmt.close();
              if(conn!=null) conn.close();
          }
          catch(SQLException e)
          {
              e.printStackTrace();
          }
      }
  }
   private final static String DriverName="com.mysql.jdbc.Driver";
   private final static String Uname="root";
   private final static String Upwd="1108.shjzh..sql.lq";
   private final static String DbURL="jdbc:mysql://localhost:3306/login";
  public static int Connect(LoginBean loginbean)   //查询输入信息是否正确
  {
      int  flag=-1;   //flag=1(登录成功)
      Connection conn=null;
      PreparedStatement pstmt=null;
      ResultSet result=null;
      int count=-1;
      try {
          Class.forName(DriverName);
          conn=DriverManager.getConnection(DbURL, Uname, Upwd);
          String sql="select count(*) from user where name=? and pwd=?";
          pstmt=conn.prepareStatement(sql);
          pstmt.setString(1, loginbean.getName());
          pstmt.setString(2, loginbean.getPwd());
          result=pstmt.executeQuery();
          if(result.next())
          {
              count=result.getInt(1);
          }
          if(count>0)
          {
              return 1;    //登录成功
          }
          else
          {
              return 0;  //登录失败(用户名,密码有误)
          }
      }
      catch(ClassNotFoundException  e)
      {
          e.printStackTrace();
          return -1;
      }
      catch(SQLException e)
      {
          e.printStackTrace();
          return -1;
      }
      catch(Exception e)
      {
          e.printStackTrace();
          return -1;
      }
      finally
      {
          try
          {
              result.close();
              pstmt.close();
              conn.close();
          }
          catch(SQLException e)
          {
              e.printStackTrace();
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  }

对数据库的添加操作:

 
private static String DriverName="com.mysql.jdbc.Driver";
private static String UName="root";
private static String Upwd="1108.shjzh..sql.lq";
private static String Url="jdbc:mysql://localhost:3306/login";

public boolean add(Student student) //添加学生
  {
      Connection conn=null;
      PreparedStatement pstmt=null;
      try {
          Class.forName(DriverName);
          conn=DriverManager.getConnection(Url, UName, Upwd);
          String sql="insert into student values(?,?,?,?)";
          pstmt= conn.prepareStatement(sql);
          pstmt.setInt(1, student.getSno());
          pstmt.setString(2, student.getName());
          pstmt.setInt(3, student.getAge());
          pstmt.setString(4, student.getAddress());
          int connt=pstmt.executeUpdate();
          if(connt>0)  return true;
          else return false;
      }
      catch(ClassNotFoundException e)
      {
          e.printStackTrace();
          return false;
      }
      catch (SQLException e)
      {
        e.printStackTrace();
        return false;
    }
      catch(Exception e)
      {
          e.printStackTrace();
          return false;
      }
      finally {
          try {
              if(pstmt!=null) pstmt.close();
              if(conn!=null) conn.close();
          }
          catch(SQLException e)
          {
              e.printStackTrace();
          }
      }
  }

   Add add=new Add();
   public boolean addStudent(Student student)
   {
      if(!add.exit(student.getSno()))
      {
        add.add(student);
        return true;
      }
      else
      {
          System.out.println("此人已存在!");
           return false;
      }
   }

 
01-18 04:31