運行index.jsp出現這樣的報錯java.sql.SQLException: No value specified for parameter 1
運行index.jsp出現這樣的報錯java.sql.SQLException: No value specified for parameter 1
然后只出現商品展示四個字 什么內容也木有了?
我的代碼是
<h1>
<B>商品展示<B>
</h1>
<hr>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0"
border="0">
<tr>
<td>
<!-- 商品循環開始 -->
<%
? ? ? ? ?ItemsDAO itemsDao = new ItemsDAO();
? ? ? ? ?ArrayList<Items> list = itemsDao.getAllItems();
? ? ? ? ?if (list != null && list.size() > 0) {
? ? ? ? for (int i = 0; i < list.size(); i++) {
? ? ? ? ? ?Items item = list.get(i);
? ? ? ? ?%>?
?
<div>
<dl>
<dt>
<a href="details.jsp?id=<%=item.getId()%>"><img
src="images/<%=item.getPicture()%>" width="120" height="90"
border="1" /></a>
</dt>
<dd class="dd_name"><%=item.getName()%></dd>
<dd class="dd_press">出版社:<%=item.getPress()%></dd>
<dd class="dd_price">價格:¥<%=item.getPrice()%></dd>
</dl>
</div> <!-- 商品循環結束 -->
<%
}
? } ? ? ? ? ? ? ??
? %>
求教求教
2015-12-10
sql語句掛掉了。把你的Statement對象toString打印一下,在控制臺里看看sql語句,看哪錯了。
2015-12-10
public class ItemsDAO {
//獲得所有的商品信息
public ArrayList<Items> getAllItems(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Items>list = new ArrayList<Items>();//商品集合
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/items","root","123456");
String sql = "select * from items where id =?;";//SOL語句
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){//商品初始化
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setPress(rs.getString("press"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
item.setNumber(rs.getInt("number"));
list.add(item);//把一個商品加入集合
}
return list;//返回集合
}
catch(Exception ex){
ex.printStackTrace();
return null;
}finally{
//釋放數據集對象
if(rs != null){
try{
rs.close();
rs = null;
}
catch(Exception ex){
ex.printStackTrace();
}
//釋放語句對象
if(stmt != null){
try{
stmt.close();
stmt = null;
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
}
}
我的DAO類是這樣的 但是我不知道哪里不對呢 求指教
2015-12-10
你的preparestatement沒有傳參數1的值,已經報出來了
看一下你后臺DAO類寫的語句對不對