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

时间:2016-08-18 07:02:25

标签: java mysql arrays multidimensional-array

我试图将数据库表中的行存储到数组中。我总是得到这个错误'类或接口,枚举预期'在我的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 个答案:

答案 0 :(得分:0)

  1. Netbeans 7.1很老了。考虑升级。
  2. 不要在JSP中编写Java代码。将其写入servlet,通过Bean将其发送到JSP,使用JSTL从JSP读取。
  3. 您阅读ResultSet的方式非常薄弱。尝试类似下面的内容。

    public List findMyRecord(int parameter)          {              PreparedStatement ps = null;              ResultSet rs = null;              连接con = null;              Listlist = new ArrayList();

    if(rs.isBeforeFirst())
                    {
                        while(rs.next())
                        {//read data}
        })
    
  4. 在您的情况下,您应该认真阅读下面的结果集,这可能会解决您的问题......

  5.     SELECT fp.Physician_Key,
           fp.Month,
           pd.DisplayName,
           hd.ProductName,
           SUM(AmtPaid) AS TotalCost
    FROM F_ProgramCost_Fact fp
    INNER JOIN D_HEALTHPLANDim hd ON hd.HealthPlan_Key = fp.HealthPlan_Key
    INNER JOIN D_PHYSICIANDim pd ON fp.Physician_Key = fp.Physician_Key
    INNER JOIN F_MemberPatient_FactLess mpf ON fp.MemberPatientFact_Key = mpf.MemberPatientFact_Key
    GROUP BY fp.Physician_Key
    
    1. 使用连接池确保MySQL不会为1000个请求使用1000个连接。 C3P0很容易就是那个..