合并2个类

时间:2012-07-25 02:32:29

标签: java mysql

我想将我的Vote4Cash类与我的命令类合并。

我的vote4cash课程会在玩家输入命令检查时奖励玩家投票。因此,我需要在我的命令类中构建vote4cash过程。我一直试图这样做,这是我的vote4cash系统中最重要的部分。如果你知道如何将它们合并在一起或让它们一起工作,请告诉我如何。我尝试使用导入,但没有成功。我没有对其他人做过的命令类进行编码,这使得我更难添加vote4gold进程或让它们协同工作。

我的Vote4Cash课程:

import java.sql.*;
import java.net.*;

public class Vote4Cash {

  public static void main(String args[]) {

  Connection con = null;
  Statement st = null;
  Statement stmt = null;
  ResultSet auth = null;
  ResultSet given = null;

    String url = "jdbc:mysql://localhost:3306/";
    String db = "vote4gold";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);
  st = con.createStatement();
  stmt = con.createStatement();

  //String give = "SELECT `given` FROM `has_voted` WHERE `ip` LIKE '+thisIp.getHostAddress()'";
        InetAddress thisIp =InetAddress.getLocalHost();
String give = "SELECT `given` FROM `has_voted` " + 
              "WHERE `ip` = '" + thisIp.getHostAddress() + "'";
  given = st.executeQuery(give);

while (given.next()) {
    if (given.getInt("given") > 0) {
        System.out.println("You've already recieved a reward for the last time you voted, but thanks again for voting.");
    } else {
        System.out.println("Thanks for voting! You've been rewarded 25m gold! Vote again tomorrow!");
        String sql = "SELECT has_voted (given) Replace('0', '0', '1')";
        int rows = stmt.executeUpdate("UPDATE has_voted SET given = 1 WHERE given = 0");
        System.out.println("The given reward column has been set to 1 for the ip address:");
       System.out.println("IP:"+thisIp.getHostAddress());
    }
  }
  } catch (Exception e) {
  System.out.println(e);
  }
  }
}

我的命令类(不适合这里): http://pastebin.com/GXFLfMX4

谢谢!如果您需要更多详细信息或不明白,请告诉我,以便我帮助您。 :)

1 个答案:

答案 0 :(得分:1)

首先,我不会尝试“合并”代码。当您从其他人那里收到代码时,这永远不应该是解决方案。您应该首先尝试了解它的用途。

为此......

对于初学者,Commands类声明为:  package server.model.players.packets;

因此您需要导入该包。此外,Commands类中的方法将Client作为参数。因此,为了使用此类,您需要能够实例化Client的实例。

是哪个Client?好吧,查看Commands中的import语句,它似乎是server.model.players.Client。您需要访问该类并能够创建实例。

您是给了Commands的源代码还是编译类或jar的源代码?如果是前者,则需要访问Commands导入的所有其他类才能编译它。

我怀疑,看着Commands课程的详细程度,以及你想把它当成黑盒子的愿望(但仍然把它与你的班级合并?!?(这实际上只是一个包装纸) main方法)),您真的只是在寻找快速解决方案。我会开始变小。抵制合并的冲动。尝试编写自己的Commands类,用几种简单的方法来学习你的设计课程。您最终会得到一个您理解的流程并获得更好的体验。

相关问题