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

📄 dbcontrol.java

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 JAVA
字号:
package zsw_mmorpg.server;

import java.sql.*;
import java.util.Vector;
import java.util.Collection;
import java.util.Iterator;
import java.nio.channels.SocketChannel;

import zsw_mmorpg.server.ConnectionPool;
import zsw_mmorpg.common.Player;

public class DBControl
{
  private static Connection getConnection()
  {
    ConnectionPool myPool=ConnectionPool.getInstance();
    Connection con=null;
    con=myPool.getConnection();
    return con;
  }

  private static void closeConnection(ResultSet rs,Statement st,Connection con)
  {
    if(rs!=null)
    {
      try
      {
        rs.close();
      }
      catch(SQLException e)
      {
        System.out.println(e);
      }
    }
    if(st!=null)
    {
      try
      {
        st.close();
      }
      catch(SQLException e)
      {
        System.out.println(e);
      }
    }
    if(con!=null)
    {
      ConnectionPool myPool=ConnectionPool.getInstance();
      myPool.releaseConnection(con);
    }
  }

  public static String select(String stm)
  {
    String result=null;
    Connection con=getConnection();
    if(con!=null)
    {
      Statement st = null;
      ResultSet rs = null;
      try {
        st = con.createStatement();
        rs = st.executeQuery(stm);
        if(rs.next())
        {
          result=rs.getString(1).trim();
        }
      }
      catch (SQLException e) {
        result = null;
        System.out.println(e);
      }
      finally
      {
          closeConnection(rs,st,con);
      }
    }
    return result;
  }

  public static Vector selectLine(String stm)
  {
    Connection con=getConnection();
    Vector result = new Vector();
    if(con!=null)
    {
      Statement st = null;
      ResultSet rs = null;
      try {
        st = con.createStatement();
        rs = st.executeQuery(stm);
        System.out.println("select line sql :"+stm);
          if (!rs.wasNull()) {
              ResultSetMetaData rsmd = rs.getMetaData();
              int end=rsmd.getColumnCount();
              if(rs.next())
              {
                for(int i=1;i<=end;i++)
                {
//                  System.out.println(""+i);
                  //result.addElement(rs.getString(i).trim());
                    String string = rs.getString(i);
                    if (string != null) {
                        string = string.trim();
                    }
                    result.addElement(string);
                }
              }
          }
          else result =null;
      }
      catch (SQLException e) {
          System.out.println("select line error");
        System.out.println(e);
      }
      finally {
        closeConnection(rs, st, con);
      }
    }
    return result;
  }

  public static Vector selectRow(String stm)
  {
    Connection con=getConnection();
    Vector result=new Vector();
    if(con!=null)
    {
      Statement st = null;
      ResultSet rs = null;
      try {
        st = con.createStatement();
        rs = st.executeQuery(stm);
        while(rs.next())
        {
          result.addElement(rs.getString(1).trim());
        }
      }
      catch (SQLException e) {
        System.out.println(e);
      }
      finally {
        closeConnection(rs, st, con);
      }
    }
    return result;
  }


  public static Vector selectTable(String stm)
  {
    int i;
    Vector result=new Vector();
    Connection con=getConnection();
    if(con!=null)
    {
      Statement st = null;
      ResultSet rs = null;
      try {
        st = con.createStatement();
        rs = st.executeQuery(stm);
        ResultSetMetaData rsmd=rs.getMetaData();
        while(rs.next())
        {
          Vector resultLine=new Vector();
          for(i=1;i<=rsmd.getColumnCount();i++)
            resultLine.addElement(rs.getString(i).trim());
          result.addElement(resultLine);
        }
      }
      catch (SQLException e) {
        System.out.println(e);
      }
      finally
      {
          closeConnection(rs,st,con);
      }
    }
    return result;
  }

  public static boolean insert(String stm)
  {
    Connection con=getConnection();
    if(con!=null)
    {
      Statement st = null;
      try {
        st = con.createStatement();
        st.executeUpdate(stm);
        return true;
      }
      catch (SQLException e) {
          System.out.println("insert error");
        System.out.println(e);
        return false;
      }
      finally
      {
          closeConnection(null,st,con);
      }
    }
    return false;
  }

  public static String update(Player p,String stm)
  {
   String result="failed";
   Connection con=getConnection();

          if (con != null) {
              Statement st = null;
              try {
                  st = con.createStatement();
//                  System.out.println(stm);
                      if (st.executeUpdate(stm) == 0) {
                          result = "noDATA";
//                      zsw_out.outdebug("保存信息出错!" + p.getPlayerId() + "出现问题,查查!");
                      } else {
                          result = "succeed";
//                      zsw_out.out("成功保存所有玩家信息");
                      }
              }
              catch (SQLException e) {
                  System.out.println(e);
                  result = "failed";
              }
              finally {
               closeConnection(null,st,con);
          }
          }
          return result;

  }
  public static String update_all(Collection players)
  {
   String stm;
   String result="failed";
   Connection con=getConnection();
       SocketChannel P_channel;

      if (!players.isEmpty()) {
          if (con != null) {
              Statement st = null;
              try {
                  st = con.createStatement();
                  Iterator i = players.iterator();
                  while (i.hasNext()) {
                      Player p = (Player) i.next();
//                      P_channel =p.getChannel();
//                if(P_channel.isConnected())
//                {
                      stm = "UPDATE character_1 SET" +
                              " life="+p.getPlayer_Max_life()+
                              ", mp="+p.getPlayer_Max_mp()+
                              ", str="+p.getPlayer_strength()+
                              ", cel="+p.getPlayer_celerity()+
                              ", con="+p.getPlayer_constitution()+
                              ", wis="+p.getPlayer_wisdom()+
                              ", sav="+p.getPlayer_att()+
                              ", charm="+p.getPlayer_def()+
                              ", lev="+p.getPlayer_level()+
                              ", map="+p.getPlayer_map()+
                              ", x="+p.getPlayer_x()+
                              ", y="+p.getPlayer_y()+
                              ", money="+p.getPlayer_money()+
//                          ", item="+p.getPlayer_Max_life()+
                              ", exp="+p.getPlayer_exp()     +
                              " where playerid ='"+p.getPlayerId()+
                              "' and career ="  + p.getPlayer_CareerId()     ;
//                  System.out.println(stm);
                      if (st.executeUpdate(stm) == 0) {
                          result = "noDATA";
//                      zsw_out.outdebug("保存信息出错!" + p.getPlayerId() + "出现问题,查查!");
                          break;
                      } else {
                          result = "succeed";
//                      zsw_out.out("成功保存所有玩家信息");
                      }
//                }  else
//                {
//                   System.out.println("数据库,t玩家");
//                    try {
//                        players.remove(p);         //踢掉该玩家
//                    } catch (Exception e) {
//                        System.out.println("t player 有错误");
//                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                    }
//                }

                  }
              }
              catch (SQLException e) {
                  System.out.println(e);
                  result = "failed";
              }
              finally {
               closeConnection(null,st,con);
          }
          }
      }
      else System.out.println("没有玩家在线");
          return result;

  }

  public static String doTransacion(String tranName,Vector stm)
  {
    String stmBeginTran="begin tran "+tranName;
    String stmCommitTran="commit tran";
    String stmRollbackTran="rollback tran "+tranName;

    String tranResult="failed";
    String result="succeed";
    int tmpResult;

    Connection con=getConnection();
    if(con!=null)
    {
      Statement st = null;
      try {
        st = con.createStatement();
        st.execute(stmBeginTran);
        for(int i=0;i<stm.size();i++)
        {
          tmpResult=st.executeUpdate(stm.get(i).toString());
          if(tmpResult==0)
            result="failed";
        }
        if(result=="succeed")
        {
          st.execute(stmCommitTran);
          tranResult="succeed";
        }
        else
          st.execute(stmRollbackTran);
      }
      catch (SQLException e) {
        System.out.println(e);
        result="failed";
      }
      finally
      {
        closeConnection(null,st,con);
      }
    }
    return tranResult;
  }

  public static int getCount(String stm)
  {
    int count=0;
    Connection con=getConnection();
    Statement st = null;
    ResultSet rs=null;
    try {
      st = con.createStatement();
      rs=st.executeQuery(stm);
      rs.next();
      count=rs.getInt(1);
    }
    catch(SQLException e){
      System.out.println(e);
    }
    return count;
  }
}

⌨️ 快捷键说明

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