为什么表没有在mysql中更新

时间:2017-02-10 08:16:21

标签: java mysql servlets jdbc

我已经创建了这个算法,其中两个团队burnleymanchester_united正在进行匹配,他们根据表中给出的玩家overall来评分目标。

我有这两个表:

  • manchester_united:列名称=数字,名称,整体,目标
  • burnley:列名称=数字,名称,整体,目标

当我执行我的代码时,它在浏览器中显示更新的值,但在goals列的数据库表中没有更新这些值。

下面是我的servlet代码:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Random;


public class gg extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();  
        int a[]=new int[11];
        int b[]=new int[11];
        int count1=0 ,count2=0;
        String up;
        int goals1[]= new int[11];
int goals2[]=new int[11];

        Random rand = new Random();
        int x=0,y=0;
        int player1=0,player2=0;

Statement stmt=null;
String sql3[] =new String[21];
String sql4[] =new String[21];

String sql1="select * from manchester_united";          
try{  
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/log","root","root");
stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery(sql1);
    while (rs.next())
    {
        a[count1]=rs.getInt("overall");
        goals1[count1]=rs.getInt("goals");
        out.println("<br>");
        out.println("a["+(count1+1)+"] = "+a[count1]+"   ");
        out.println("<br>");
        out.println("goals1["+(count1+1)+"] = "+goals1[count1]+"   ");
        count1++;
    }
//count=0;     

out.println("<br>");
out.println("<br>");

String sql2="select * from burnley";
//stmt=con.createStatement();
    rs=stmt.executeQuery(sql2);
    while(rs.next())
    {
        b[count2]=rs.getInt("overall");
        goals2[count2]=rs.getInt("goals");
        out.println("<br>");
        out.println("b["+(count2+1)+"] = "+b[count2]+"   ");
        out.println("<br>");
        out.println("goals2["+(count2+1)+"] = "+goals2[count2]+"   ");
        count2++;
    }


    for (int i=0;i<11;i++)
        {
            player1=rand.nextInt(11);
            player2=rand.nextInt(11);

        if (a[player1]>b[player2])
        {
            int goal=rand.nextInt(10)+1;

            if (a[player1]>=90)
            {
                if (goal==1 || goal==2 ||goal==4 || goal==5 ||goal==7 || goal==8 ||goal==9 || goal==10)
                {
                    x=x+1;

                        goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);

                }
            }
            else if (a[player1]>=80 && a[player1]<90)
            {
                if (goal==1 || goal==2 || goal==4 || goal==5 || goal==7 || goal==9 || goal==10)
                {
                    x=x+1;

                         goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);
                }
            }
            else if (a[player1]>=70 && a[player1]<80)
            {
                if (goal==1 || goal==2 || goal==4 || goal==7 || goal==8 || goal==10)
                {
                    x=x+1;

                    goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);
                }
            }
            else if (a[player1]>=60 && a[player1]<70)
            {
                if (goal==1 || goal==2 ||goal==5 || goal==8 ||goal==9)
                {
                    x=x+1;

                   goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);
                }
            }
            else if (a[player1]>=50 && a[player1]<60)
            {
                if (goal==1 ||goal==4 || goal==5 || goal==8)
                {
                    x=x+1;

                    goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);

                }
            }
            else
            {
                if (goal==1 || goal==5 || goal==7)
                {
                    x=x+1;

                  goals1[player1]++;
                        sql3[i]="UPDATE `manchester_united` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql3[i]);
                        ps.setInt(1,goals1[player1]);
                        ps.setInt(2,player1);
                }
            }

        }


        else
        {
            int goal=rand.nextInt(10)+1;

            if (b[player2]>=90)
            {
                if (goal==1 || goal==2 ||goal==4 || goal==5 ||goal==7 || goal==8 ||goal==9 || goal==10)
                {
                    y=y+1;

                    goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }
            else if (b[player2]>=80 && b[player2]<90)
            {
                if (goal==1 || goal==2 || goal==4 || goal==5 || goal==7 || goal==9 || goal==10)
                {
                    y=y+1;

                  goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }
            else if (b[player2]>=70 && b[player2]<80)
            {
                if (goal==1 || goal==2 || goal==4 || goal==7 || goal==8 || goal==10)
                {
                    y=y+1;

                  goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }
            else if (b[player2]>=60 && b[player2]<70)
            {
                if (goal==1 || goal==2 ||goal==5 || goal==8 ||goal==9)
                {
                    y=y+1;

                   goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }
            else if (b[player2]>=50 && b[player2]<60)
            {
                if (goal==1 ||goal==4 || goal==5 || goal==8)
                {
                    y=y+1;

                    goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }
           else
            {
                if (goal==1 || goal==5 || goal==7)
                {
                    y=y+1;

                  goals2[player2]++;
                        sql4[i]="UPDATE `burnley` SET `goals`=? WHERE `number`=?";
                        PreparedStatement ps = con.prepareStatement(sql4[i]);
                        ps.setInt(1,goals2[player2]);
                        ps.setInt(2,player2);
                }
            }

        }

        }




}
catch (Exception e)
        {
            e.printStackTrace();
        }

out.println("<br>");

        out.println("Team1= "+x+" and Team2= "+y+"<br>");

        for(int j=0;j<11;j++)
        {
        out.println("<br>goals1= "+goals1[j]+" and goals2= "+goals2[j]);

        }
    }





    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }



}

1 个答案:

答案 0 :(得分:0)

您需要添加executeUpdate()才能实际更新数据。