测试时,得到所需的输出。代码中也没有错误。
但是,当我启动程序时,当我单击“创建”按钮(触发检查主机方法的位置)时,出现此异常:
public String[] hostGame() throws SQLException {
FS = "FinancialSector_".trim() + gameName.trim();
System.out.println("Insert recode to main table");
st = con.createStatement();
String queryq = "insert into StorkMgame (GameName,PlayerName,GameStatus) VALUE ('" + gameName + "','" + name + "',FALSE);";//game name
st.executeUpdate(queryq);
System.out.println("Creating table for game");
st = con.createStatement();
String query2 = "CREATE TABLE " + gameName + " (PlayerNumber INTEGER NOT NULL AUTO_INCREMENT , PlayerName VARCHAR(20),GameName VARCHAR(20),GameStatus INTEGER,PRIMARY KEY (PlayerNumber))";//Game name insert
st.executeUpdate(query2);
st = con.createStatement();
String query = "insert into " + gameName + " (PlayerName,GameStatus) VALUE ('" + name + "',FALSE);";
st.executeUpdate(query);
st = con.createStatement();
String query4 = "CREATE TABLE " + FS + "(number INTEGER NOT NULL AUTO_INCREMENT ,GameName VARCHAR(20),Sector VARCHAR(20),comapanyName VARCHAR(20),newnominalValue float(20),oldnominalValue float(20),numberOfShares INTEGER(20),PLock INTEGER(5), PRIMARY KEY (Number))";
st.executeUpdate(query4);
int[] list = {1, 2, 3,4,5,6,7,8,9,10};
Random random = new Random();
int arrayElement = random.nextInt(list.length);
System.out.println(arrayElement);
if (arrayElement == 0) {
findIndex = 1;
list = removeTheElement(list, arrayElement);
} else {
findIndex = arrayElement;
list = removeTheElement(list, arrayElement - 1);
}
System.out.println(arrayElement);
System.out.println("Resultant Array: " + Arrays.toString(list));
for (int i = 0; i < 4; i++) //need to run 4 times
{
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM financialSector_main WHERE Number= '" + findIndex + "';");//selecte data to each sector
while (rs.next()) {
String CM = rs.getString("comapanyName");
String CV = rs.getString("nominalValue");
String NS = rs.getString("numberOfShares");
System.out.print(CM);
st = con.createStatement();
String query0 = "insert into " + FS + " (GameName,Sector,comapanyName,newnominalValue,oldnominalValue,numberOfShares,PLock) VALUE ('" + gameName + "','Financial','" + CM + "','" + CV + "', 0 ,'" + NS + "',0)";
st.executeUpdate(query0);
}
}
}
//Generate Random Market
Integer[] Values = {-2, -1, 0, 1, 2};
Random rand = new Random();
int n;
int nominalvalue;
int newnominalvalue;
int randNum;
int T;
for (int i = 0; i < 4; i++) {//need to run 4 times
for (int c = 1; c <= 12; c++) {
//return T;
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM financialSector_main WHERE Number= '" + findIndex + "';");//select data for each sector
while (rs.next()) {
String CMF = rs.getString("comapanyName");
String CVF = rs.getString("nominalValue");
// String NS = rs.getString("numberOfShares");
System.out.print(CMF);
nominalvalue = Integer.parseInt(CVF);
System.out.println(nominalvalue);
n = rand.nextInt(5);
randNum = Values[n];
//System.out.println(Values[n]);
//System.out.println(randNum);
//System.out.println(c);
//int nv = 1;
T = randNum * nominalvalue;
newnominalvalue = nominalvalue - T;
System.out.println(newnominalvalue);
st = con.createStatement();
String query0 = "insert into " + FS + " (comapanyName,newnominalValue,PLock) VALUE ('" + gameName + "','Financial','" + CMF + "','" + CVF + "', 0 )";
st.executeUpdate(query0);
}
}
}
//}