变量id可能尚未初始化(JSP)

时间:2016-05-27 20:36:01

标签: java sql jsp

当我运行文件时丢弃了这个错误...而且我不知道该怎么做......任何帮助,语法似乎都没有错......

      Compiling 3 source files to C:\Users\ioann_000\Documents\NetBeansProjects\Project
 ΒΔ\build\generated\classes
 C:\Users\ioann_000\Documents\NetBeansProjects\Project
 ΒΔ\build\generated\src\org\apache\jsp\assets\jsp\create_002ddest_002dcode_0020_00282_0029_jsp.java:80:
 error: variable id might not have been initialized
               int insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES
 ('"+id+"','"+cat[i]+"')");
                                                                                                                                         ^ 1 error C:\Users\ioann_000\Documents\NetBeansProjects\Project
 ΒΔ\nbproject\build-impl.xml:953: The following error occurred while
 executing this line:
 C:\Users\ioann_000\Documents\NetBeansProjects\Project
 ΒΔ\nbproject\build-impl.xml:296: Compile failed; see the compiler
 error output for details. BUILD FAILED (total time: 1 second)

代码

  <%


   String id8=request.getParameter("id8"); //Country field  
   String id9=request.getParameter("id9"); //City field
   String id10=request.getParameter("id10"); //URL field
   int id;

          Class.forName("com.mysql.jdbc.Driver"); 
          String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
          Connection myConnection = DriverManager.getConnection(myDatabase);
          Statement myStatement = myConnection.createStatement();  

           String sqlInsert = "INSERT INTO dest(Country,City,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
           myStatement.executeUpdate(sqlInsert);
           ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
            while (rs.next()) {
                id=rs.getInt(1);
            }
           String cat[]=request.getParameterValues("dest1");

           for(int i=0;i<cat.length;i++)
           {
              int insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+cat[i]+"')");

           }

4 个答案:

答案 0 :(得分:1)

您声明int id;,但您从未为其设置过值。您需要先使用它来初始化(或设置一个值)。

String id8=request.getParameter("id8"); //Country field  
String id9=request.getParameter("id9"); //City field
String id10=request.getParameter("id10"); //URL field
int id = -1;

Class.forName("com.mysql.jdbc.Driver"); 
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();  

String sqlInsert = "INSERT INTO dest(Country,City,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
myStatement.executeUpdate(sqlInsert);
ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
while (rs.next()) {
    id=rs.getInt(1);
}
String cat[]=request.getParameterValues("dest1");

if(id > -1)
{
    int insert_ch;
    for(int i=0;i<cat.length;i++)
    {
      insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+cat[i]+"')");
    }
}

尝试以上操作,我对其进行了修改,以便它可以正常工作。我还在for循环之外移动了int insert_ch的声明,否则它将在其他地方无法使用,除非你只使用insert_ch在循环中做更多的东西。

答案 1 :(得分:0)

正如你所设定的那样#id;#34;在while循环中,如果resultset为空,则可能无法初始化。这就是您看到此编译器错误的原因。在声明时初始化id,你应该没问题。 -Amit

答案 2 :(得分:0)

int id;
...
ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
while (rs.next()) {
   id=rs.getInt(1);
}
...
for(int i=0;i<cat.length;i++)
{
    int insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+cat[i]+"')");
}

如果查询未返回任何行,则变量id将永远不会获得值。

答案 3 :(得分:0)

Id=rs.getInt(1); //here 

Id字段为空或空白尝试打印或提醒以查看ID中的值。 这就是它给出变量id的原因可能没有被初始化