繁体   English   中英

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

[英]How to Store rows from a MySQL table into an array in Java

我正在尝试将数据库表中的行存储到数组中。 我在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的方式非常弱。 尝试以下类似的方法。

    公共列表findMyRecord(int参数){PreparedStatement ps = null; ResultSet rs = null; 连接con = null; Listlist =新的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} }) 
  1. 使用连接池确保MySQL不会对1000个请求使用1000个连接。 C3P0是一个简单的库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM