日期:2014-05-17  浏览次数:20740 次

求java大神 指导页面乱码
本菜鸟刚学J2EE, 响应老师的要求在做一个在线购书系统,自己也就做在大约前台顾客的位置,就遇到一个自己不能解决的问题,自己新手,还玩,各位高手,抽出点宝贵的时间,挽救我于水深火热之中;由于不会截图我就把情况连接到QQ相册...http://user.qzone.qq.com/503237006/photo/V13JIShO46UoXi/ 代码的大体是:jsp页面<%
ArrayList list = (ArrayList)application.getAttribute("allbooks");
if(list==null){
String sql = "select * from book";
DbDao dbDao = new DbDao();
dbDao.connectDb();
list = dbDao.executeQuery(sql);
dbDao.closeDb();
application.setAttribute("allbooks",list);
}
 
  for(int i = 0 ; i < list.size(); i++){
  BookBean book = (BookBean)list.get(i);
  String bookname = book.getBookName().toString();
  %> 
<form name="f1" method="post" action="AddOrderLineServlet">
<input type="hidden" name="bookid" value="<%=book.getId()%>"/>
<tr>
<td align="center"><%=book.getId()%></td>
  <td><%=bookname%></td>
  <td><%=book.getPrice()%></td>
  <td><input type="text" name="num1" value="0" size="4" maxlength="4" ></td>  
  <td><input type="submit" value="购买"/></td>
</tr>
</form>
<%}%>
</table>


数据库的连接:package cn.db;

import java.sql.*;
import java.util.ArrayList;

import cn.bean.BookBean;

public class DbDao {
Connection con=null;//连接 
Statement stm=null;//执行SQL语句
ResultSet rs=null;//保存查询结果

public void connectDb(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","123");
stm = con.createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public ArrayList executeQuery(String sql){
ArrayList list = new ArrayList();

try {
rs = stm.executeQuery(sql);
while(rs.next()){
BookBean book = new BookBean();
book.setId(rs.getInt(1));
book.setBookName(rs.getString(2));
book.setPrice(rs.getDouble(3));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

public void executeUpdate(String sql){
try {
stm.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void closeDb(){
try {
if(rs!=null) rs.close();
if(stm!=null) stm.close();
if(con!=null) con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
bookbean的代码如下面:package cn.bean;

public class BookBean {
private int id;
private String bookName;
private double price;

public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;


}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
希望哥哥们能帮小弟解决这燃眉之急.....