【发布时间】: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