【问题标题】:How to Store rows from a MySQL table into an array in Java如何将 MySQL 表中的行存储到 Java 中的数组中
【发布时间】:2016-12-25 00:12:26
【问题描述】:

我正在尝试将数据库表中的行存储到数组中。我总是在我的 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>

知道我哪里出错了吗?

【问题讨论】:

  • 不要将代码放入您的 JSP 中。使用其他一些 web 服务的 servlet。然后使用 JSTL 打印出你的数据
  • @ScaryWombat:说得好。要添加更多内容,您可以像我们大多数人一样在自己的应用程序中编写 servlet...

标签: java mysql arrays multidimensional-array


【解决方案1】:
  1. Netbeans 7.1 已经很老了。考虑升级。
  2. 不要在 JSP 中编写 Java 代码。用 servlet 编写,通过 Bean 发送到 JSP,使用 JSTL 从 JSP 中读取。
  3. 您阅读 ResultSet 的方式很弱。试试下面的方法。

    public List findMyRecord(int 参数) { PreparedStatement ps = null; 结果集 rs=null; 连接 con = null; Listlist = 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}
    })
  1. 使用连接池确保 MySQL 不会为 1000 个请求使用 1000 个连接。 C3P0 是一个简单的库。

【讨论】:

  • YourBean pb = new PatientBean();
  • @dapson:对不起,这是一个错误,我已修复。应该是YourBean pb = new YourBean ()
  • YourBean 是什么意思?
  • 或者我怎样才能为这个特定的示例代码编写一个 bean?
猜你喜欢
  • 2014-03-18
  • 2013-04-01
  • 2011-03-25
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 2011-06-09
相关资源
最近更新 更多