如何将MySQL表中的行存储到Java中的数组中



我试图将数据库表中的行存储到数组中。我总是得到这个错误'类或接口,enum预期'在我的NetBeans IDE 7.1.1

这是我写的代码

<table border="2">
   <tr>
      <th>Coop No</th>
      <th>Salutation</th>
      <th>First Name</th>
      <th>Middle Name</th>
      <th>Last Name</th>
      <th>Form Type</th>
   </tr>
   <%! double[][] g_testdata;
      ResultSet rs = null;
      %>           
   <% 
      //ResultSet rs = null;
      try
      {     
           Class.forName("com.mysql.jdbc.Driver"); //Load the driver– Not required in Java SE 6.0 and later (JDBC 4.0 and later), the driver is loaded automatically.
           String host = "jdbc:mysql://localhost/cooperative";
           String uName = "root";
           String uPass = "Hecares4me";
           //Connection con = DriverManager.getConnection( host, username, password );
           Connection con = DriverManager.getConnection(host, uName, uPass );
           Statement st = con.createStatement( );
           String SQL = "SELECT transfacdept.transfacdept_id, transfacdept.faculty_id, transfacdept.department_id, transfaculties.faculty_id, transfaculties.facultyname FROM transfacdept, transfaculties WHERE transfacdept.transfacdept_id = transfaculties.faculty_id ";
           rs = st.executeQuery( SQL );
           //ResultSet rs=st.executeQuery("SELECT transfacdept.transfacdept_id, transfacdept.faculty_id, transfacdept.department_id, transfaculties.faculty_id, transfaculties.facultyname FROM transfacdept, transfaculties WHERE transfacdept.transfacdept_id = transfaculties.faculty_id"); 
           g_testdata = new double[5][6]; 
           int numRows, numCols;
           if(!rs.next()){
               return;
           }
           rs.last();
           numRows = rs.getRow();
           numCols = rs.getMetaData().getColumnCount();
           out.println(numRows + " " + numCols);
           //rs.first();
           while(rs.next()){
                   for (int i=1; i <= numRows; i++)
                   {
                           for (int j=1; j <= numCols; j++) // populate the test data array
                           {
                                   g_testdata[i][j] = rs.getDouble(j);
                                   out.println(g_testdata[i][j]);
                           }
                   }
           }
       }
       catch(ClassNotFoundException xcp)
       {
           out.println("The SQLException exception has occurred:");
       }
       catch( SQLException err )
       {
           out.println( err.getMessage() );
       }                                                                                                                                                                                      }     
      %>
</table>

你知道我哪里做错了吗?

  1. Netbeans 7.1相当旧。考虑升级。
  2. 不要在JSP中编写Java代码。在servlet中编写,通过Bean发送给JSP,使用JSTL从JSP读取。
  3. 你阅读ResultSet的方式很弱。试试下面的内容:

    public List findMyRecord{PreparedStatement ps = null;ResultSet rs =零;Connection con = null;list = new ArrayList();

             try
             {
                 con = DBMaster.getInstance().getConnection(); //bcs I am using connection pooling. You can use `DriverManager.getConnection`.....
                 String cmd="select * from table where parameter=? ORDER BY columnName DESC";
                 ps=con.prepareStatement(cmd.toLowerCase());
                 ps.setInt(1, parameter);
                 rs=ps.executeQuery();
                 if(rs.isBeforeFirst())
                 {
                     while(rs.next())
                     {
                         YourBean pb = new YourBean ();
                         pb.setMyData(pb.getInt("columnName"));
                         list.add(pb);
                     }
                 }
    
             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }
             finally
             {
                 try
                 {
                     if(rs!=null){rs.close();}
                     if(ps!=null){ps.close();}
                     if(con!=null){con.close();}
                 }
                 catch(Exception e)
                 {
                     e.printStackTrace();
                 }
             }
             return list;
      }
    
  4. 在你的情况下,你应该认真尝试阅读你的结果集如下,这可能会解决你的问题…

if(rs.isBeforeFirst())
                {
                    while(rs.next())
                    {//read data}
    })
使用连接池来确保MySQL不会为1000个请求使用1000个连接。C3P0是一个简单的库…

相关内容

  • 没有找到相关文章

最新更新