我正在尝试连接到数据库以检索一些数据并将其放在primefaces表中。这是我的代码:
public Connection initConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
if (connection == null) {
connection = DriverManager.getConnection(URL, USER, PASS);
System.out.println("Connected.");
} else if (connection.isClosed()) {
connection = null;
connection = DriverManager.getConnection(URL, USER, PASS);
System.out.println("Connected.");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} finally {
return connection;
}
} // End getConnection.
public void setIngredients() throws SQLException {
getIngredients = connection.prepareStatement("SELECT * FROM ingredient");
rset = getIngredients.executeQuery();
while(rset.next()) {
ingredients.add(new Ingredients(rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getDouble(6)));
}
dbc.closeConnection();
}
public List<Ingredients> getIngredients() throws SQLException {
return ingredients;
}
<div id="content">
<p:dataTable var="ingredient" value="#{newIngredientBean.ingredients}">
<p:column headerText="Ingredient Name">
<h:outputText value="#{ingredient.ingredientName}" />
</p:column>
<p:column headerText="Ingredient Short Name">
<h:outputText value="#{ingredient.ingredientShortName}" />
</p:column>
<p:column headerText="Batch Code">
<h:outputText value="#{ingredient.supplierBatchCode}" />
</p:column>
<p:column headerText="Expiry Date">
<h:outputText value="#{ingredient.ingredientExpiryDate}" />
</p:column>
<p:column headerText="Quantity">
<h:outputText value="#{ingredient.ingredientQuantity}" />
</p:column>
</p:dataTable>
</div>
现在,当我启动项目时,我看到的是&#34;等待localhost&#34;如果我删除这一行&#34; Class.forName(&#34; com.mysql.jdbc.Driver&#34;);&#34;网页加载但抛出MySQL没有驱动程序错误。我能够在Java中的main方法中检索数据,但后来我尝试填充它从未加载的表格。感谢您的帮助!