User
{
   private int userID;
   
private double accountBalance;
   
private boolean isAdmin;
   
private String userName;

   
public int getUserID()
   {
      
return this.userID;
   }

   
public void setUserID(int userID)
   {
      
this.userID = userID;
   }

   
public double getAccountBalance()
   {
      
return this.accountBalance;
   }

   
public void setAccountBalance(double accountBalance)
   {
      
this.accountBalance = accountBalance;
   }

   
public boolean getIsAdmin()
   {
      
return this.isAdmin;
   }

   
public void setIsAdmin(boolean isAdmin)
   {
      
this.isAdmin = isAdmin;
   }

   
public String getUserName()
   {
      
return this.userName;
   }

   
public void setUserName(String userName)
   {
      
this.userName = userName;
   }
}

 UserDataBase
{
   public static String CONNECTION_STRING = AppVariable.getDataBaseConnectionString();
   
public static String USER_ID = AppVariable.getDataBaseUserID();
   
public static String PASSWORD = AppVariable.getDataBasePassword();

   
public static void insert(User instance) throws Exception
   {
      Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
      Connection conn 
= DriverManager.getConnection(CONNECTION_STRING, USER_ID, PASSWORD);
      CallableStatement cs 
= conn.prepareCall("{? = call User_Insert(?,?,?,?)}");
      cs.setDouble(
"AccountBalance", instance.getAccountBalance());
      cs.setBoolean(
"IsAdmin", instance.getIsAdmin());
      cs.setString(
"UserName", instance.getUserName());
      cs.registerOutParameter(
"UserID",Types.INTEGER);
      cs.registerOutParameter(
1,Types.INTEGER);
      cs.executeUpdate();
      
if(cs.getInt(1)!=0)
      {
         
throw new Exception("Errors happened when executing the stored procedure");
      }
      instance.setUserID(cs.getInt(
"UserID"));
      cs.close();
      conn.close();
   }

   
public static void select(User instance) throws Exception
   {
      Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
      Connection conn 
= DriverManager.getConnection(CONNECTION_STRING, USER_ID, PASSWORD);
      CallableStatement cs 
= conn.prepareCall("{? = call User_Select(?)}");
      cs.setInt(
"UserID", instance.getUserID());
      cs.registerOutParameter(
1,Types.INTEGER);
      ResultSet rs 
= cs.executeQuery();
      
if(rs.next())
      {
         instance.setAccountBalance(rs.getDouble(
"AccountBalance"));
         instance.setIsAdmin(rs.getBoolean(
"IsAdmin"));
         instance.setUserName(rs.getString(
"UserName"));
      }
      rs.close();
      
if(cs.getInt(1)!=0)
      {
         
throw new Exception("Errors happened when executing the stored procedure");
      }
      cs.close();
      conn.close();
   }

    
public static void update(User instance) throws Exception
    {
      Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
      Connection conn 
= DriverManager.getConnection(CONNECTION_STRING, USER_ID, PASSWORD);
      CallableStatement cs 
= conn.prepareCall("{? = call User_Update(?,?,?,?)}");
      cs.setInt(
"UserID", instance.getUserID());
      cs.setDouble(
"AccountBalance", instance.getAccountBalance());
      cs.setBoolean(
"IsAdmin", instance.getIsAdmin());
      cs.setString(
"UserName", instance.getUserName());
      cs.registerOutParameter(
1,Types.INTEGER);
      cs.executeUpdate();
      
if(cs.getInt(1)!=0)
      {
         
throw new Exception("Errors happened when executing the stored procedure");
      }
      cs.close();
      conn.close();
   }

    
public static void delete(User instance) throws Exception
    {
      Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
      Connection conn 
= DriverManager.getConnection(CONNECTION_STRING, USER_ID, PASSWORD);
      CallableStatement cs 
= conn.prepareCall("{? = call User_Delete(?)}");
      cs.setInt(
"UserID", instance.getUserID());
      cs.registerOutParameter(
1,Types.INTEGER);
      cs.executeUpdate();
      
if(cs.getInt(1)!=0)
      {
         
throw new Exception("Errors happened when executing the stored procedure");
      }
      cs.close();
      conn.close();
   }
}

 AppVariable
{
   public static String getDataBaseConnectionString()
   {
      
return "";//add connection string here
   }

   
public static String getDataBaseUserID()
   {
      
return "";//add user name here
   }

   
public static String getDataBasePassword()
   {
      
return "";//add password here
   }
}

 UserBroker
{
   public static void insert(User instance)
   {
      
// add business logic here
      try
      {
         UserDataBase.insert(instance);
      }
      
catch(Exception e)
      {
      }
   }

   
public static void select(User instance)
   {
      
// add business logic here
      try
      {
         UserDataBase.select(instance);
      }
      
catch(Exception e)
      {
      }
   }

   
public static void update(User instance)
   {
      
// add business logic here
      try
      {
         UserDataBase.update(instance);
      }
      
catch(Exception e)
      {
      }
   }

   
public static void delete(User instance)
   {
      
// add business logic here
      try
      {
         UserDataBase.delete(instance);
      }
      
catch(Exception e)
      {
      }
   }
}

相关文章: