超简单的球员MIS(JSP)----ConnDB bean和Player bean

    这是ConnDB bean的源代码:
        -------------------------------------------------------------------------------------------------------------------------------------------------------------
  package com.JSPExperiment.javabean;
  import java.sql.*;
  /**
     * * @author Administrator
  */
  public class ConnDB {
  //the usename of database
   String dbName="root";
   //the password of the database user
   String dbPassword="liao";
   //the URL of the database
   String url="jdbc:mysql://localhost:3306/jspex?useUnicode=true&characterEncoding=GB2312";
   //define a connection object
   Connection dbConn;
   //error statement
   Statement statement;
   ResultSet resultSet;
   /*
            * @initialization operation,including DB connection
     */
  public ConnDB(){
   statement=null;
   resultSet=null;
   //connect DB
   try{
    //声明所用的类包
    Class.forName("com.mysql.jdbc.Driver");
    //获得数据库的连接对象
    dbConn=DriverManager.getConnection(url,dbName,dbPassword);
   }catch(SQLException ex){
    //打印出异常信息
    System.out.println(ex.toString());
   }catch(ClassNotFoundException ex){
    //打印出异常信息
    System.out.println(ex.toString());
   }
  }
  /*
  * @对数据库执行SQL语句,主要是插入和更新操作,返回一个布尔变量值
  */
  public boolean exeSql(String sqlString){
   try{
    statement=dbConn.createStatement();
    statement.executeUpdate(sqlString);
    return true;
   }catch(Exception ex){
    //打印异常信息
    System.out.println(ex.toString());
    return false;
   }
  }
  /*
  * @对数据库执行查询语句,返回一个ResultSet类型的对象
  */
  public ResultSet exeSqlQuery(String sqlString){
   try{
    statement=dbConn.createStatement();
    resultSet=statement.executeQuery(sqlString);
   }catch(Exception ex){
    //打印异常信息
   System.out.println(ex.toString());
   resultSet=null;
   } return resultSet;
  }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------


以下是Player bean的源代码:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.JSPExperiment.javabean;

import java.util.*;
import java.sql.*;
import java.text.*;

/**
*
* @author Administrator
*/
public class Player extends ConnDB {

    private int PlayerID ;
    private String PlayerName;
    private String PlayerPwd ;
    private String sqlString;
   
    public Player(){
        PlayerID=0;
        PlayerName="";
        PlayerPwd="";
        sqlString="";
    }
   
     /**
     * @return the PlayerID
     */
    public int getPlayerID() {
        return PlayerID;
    }

    /**
     * @param PlayerID the PlayerID to set
     */
    public void setPlayerID(int PlayerID) {
        this.PlayerID = PlayerID;
    }

    /**
     * @return the PlayerName
     */
    public String getPlayerName() {
        return PlayerName;
    }

    /**
     * @param PlayerName the PlayerName to set
     */
    public void setPlayerName(String PlayerName) {
        this.PlayerName = PlayerName;
    }

    /**
     * @return the PlayerPwd
     */
    public String getPlayerPwd() {
        return PlayerPwd;
    }

    /**
     * @param PlayerPwd the PlayerPwd to set
     */
    public void setPlayerPwd(String PlayerPwd) {
        this.PlayerPwd = PlayerPwd;
    }
    /*
     * @登录时判断用户名和密码是否正确
     */
    public boolean checkLogin() {
        sqlString = "select * from player where  PlayerName='" + PlayerName + "' and PlayerPwd='" + PlayerPwd + "' ";
        ResultSet resultSet = null;
        boolean isValid = false;
        try {
            resultSet = super.exeSqlQuery(sqlString);
            while (resultSet.next()) {
                this.PlayerID=resultSet.getInt("PlayerID");
                isValid = true;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        return isValid;
    }
  
     /*
     * @用户注册
     */
     public boolean playerReg(){
        String sqlStringMaxID = "select MAX(PlayerID) AS PlayerID from player";
        ResultSet resultSet = null;
        boolean isRegSucces = false;
        try {
            resultSet = super.exeSqlQuery(sqlStringMaxID);
            while (resultSet.next()) {
                this.PlayerID=resultSet.getInt("PlayerID")+1;
                 sqlString = " insert into player (PlayerID,PlayerName,PlayerPwd) values ('"+PlayerID+"','"+PlayerName+"','"+PlayerPwd+"') ";
                isRegSucces = super.exeSql(sqlString);
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        return isRegSucces;
     }


    /*
     * @运动员信息查询
     */
    public boolean getPlayerInfo(int a) {
        sqlString = "select * from player where PlayerID ='" +a+ "'";
        try {
            ResultSet resultSet = super.exeSqlQuery(sqlString);
            if (resultSet.next()) {
                this.setPlayerID(resultSet.getInt("PlayerID"));
                this.setPlayerName(resultSet.getString("PlayerName"));
                this.setPlayerPwd(resultSet.getString("PlayerPwd"));
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
    }

    /*
     * @修改个人密码
     */
    public boolean pwdChange(){
        sqlString = "update player set PlayerPwd='"+PlayerPwd+"' where PlayerID='"+PlayerID+"'";
        boolean isValid = false;
        try {
            isValid = super.exeSql(sqlString);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        return isValid;
    }
   
    /*
     * @删除运动员的个人账户
     */
    public boolean accountDel(){
        sqlString = "delete from player where PlayerID='"+PlayerID+"'";
        boolean isValid = false;
        try {
            isValid = super.exeSql(sqlString);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        return isValid;
    }

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
至此,两个bean的代码都已经给出了,可以看出直接连接数据库是非常简单的。不过有时候为了优化效率,我们会用到连接池。

你可能感兴趣的:(javabean)