2013-04-11 117 views
1

我在从数据库检索数据时遇到问题。 我有一个函数从Java中的mysql数据库表中检索数据

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class DataAccess { 
private Connection conn; 
private User user = new User(); 

public DataAccess(){ 
    connect(); 
} 

public boolean connect() { 
    boolean success = true; 

    String driverName ="com.mysql.jdbc.Driver"; 
    String conURL = "jdbc:mysql://localhost:3306/final_project"; 
    String user = "root"; 
    String pass = "1009"; 

    try { 
     Class.forName(driverName).newInstance(); 
    } catch (InstantiationException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     System.err.println(e.getMessage() + "------Cannot Load Driver"); 
     success = false; 
    } 

    try { 
     conn = DriverManager.getConnection(conURL, user, pass); 
    } catch (SQLException e) { 
     System.err.println(e.getMessage() + "--SQL States: " + e.getSQLState() + "---- ErrorCode: " + e.getErrorCode()); 
     success = false; 
    } 
    return success; 
} 

public void insertBooks (Books books) 
{ 
    try {   
     PreparedStatement stmt = conn.prepareStatement("insert into Books (Title, Author, ISBN, Total) VALUES (?, ?, ?, ?);"); 
     stmt.setString(1, books.getTitle()); 
     stmt.setString(2, books.getAuthor()); 
     stmt.setInt(3, books.getISBN()); 
     stmt.setDouble(4, books.getPrice()); 

     stmt.executeUpdate(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void selectBooks() { 
    try { 
     Statement stm = conn.createStatement(); 
     ResultSet rs = stm.executeQuery("SELECT * FROM books"); 
     while (rs.next()) { 
      Books books = new Books(); 

      books.setTitle(rs.getString(1)); 
      books.setAuthor(rs.getString(2)); 
      books.setISBN(rs.getInt(3)); 
      books.setPrice(rs.getDouble(4)); 

      user.add(books); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 

}

从表中的书籍选择和获取和设置各个领域如标题,作者,ISBN和价格,然后它加入到书目对象的用户类。 当我使用功能

public ArrayList<Books> getBookList() { 
    return bookList; 
} 

没有任何返回。

用户等级:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext; 
import java.util.ArrayList; 

@ManagedBean(name = "user") 
@SessionScoped 
public class User { 
private String firstName; 
private String lastName; 
private ArrayList<Books> bookList = new ArrayList<Books>(); 
private ArrayList<Books> shopBookList = new ArrayList<Books>(); 
private double total; 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public ArrayList<Books> getBookList() { 
    return bookList; 
} 

public void setBookList(ArrayList<Books> bookList) { 
    this.bookList = bookList; 
} 

public double getTotal() { 
    for (Books bk : bookList) { 
     total += bk.getPrice(); 
    } 
    return total; 
} 

public void setTotal(double total) { 
    this.total = total; 
} 

public ArrayList<Books> getShopBookList() { 
    return shopBookList; 
} 

public void setShopBookList(ArrayList<Books> shopBookList) { 
    this.shopBookList = shopBookList; 
} 

public String add(Books books) { 
    bookList.add(books); 
    DataAccess da = new DataAccess(); 
    da.insertBooks(books); 
    return "Added"; 
} 

public String searchBooks() { 
    DataAccess da = new DataAccess(); 
    da.selectBooks(); 
    return "books"; 
} 

public String shop(Books books) { 
    shopBookList.add(books); 
    return null; 
} 

public String start() { 
    bookList = new ArrayList<Books>(); 
    firstName = ""; 
    lastName = ""; 
    total = 0; 
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
    return "index"; 
} 

}

书籍类:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean(name = "books") 
@SessionScoped 

public class Books { 
private String title; 
private String author; 
private int ISBN; 
private double price; 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getAuthor() { 
    return author; 
} 

public void setAuthor(String author) { 
    this.author = author; 
} 

public int getISBN() { 
    return ISBN; 
} 

public void setISBN(int iSBN) { 
    ISBN = iSBN; 
} 

public double getPrice() { 
    return price; 
} 

public void setPrice(double total) { 
    this.price = total; 
} 

}

是应该从user.booklist获取数据的页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core"> 

<h:body> 
<h:form> 
    <h1>Books Page</h1> 
    <hr /> 
    <label>The following books are available:</label><br /> 
    <h:dataTable value="#{user.bookList}" var="bk" border="1"> 
     <h:column> 
      <f:facet name="header">ISBN</f:facet> 
      <h:outputText value="#{bk.ISBN}" /> 
     </h:column> 
     <h:column> 
      <f:facet name="header">Title</f:facet> 
      <h:outputText value="#{bk.title}" /> 
     </h:column> 
     <h:column> 
      <f:facet name="header">Author</f:facet> 
      <h:outputText value="#{bk.author}" /> 
     </h:column> 
     <h:column> 
      <f:facet name="header">Price</f:facet> 
      <h:outputText value="#{bk.price}" /> 
     </h:column> 
     <h:column> 
      <f:facet name="header">Add</f:facet> 
      <h:commandLink value="Add" action="#{user.shop(bk)}" /> 
     </h:column> 
    </h:dataTable> 
    <h:commandButton value="Home" action="index" /> 
    <h:commandButton value="Checkout" action="checkout" /> 
</h:form> 
</h:body> 
</html> 
+0

你能打印user.add()方法吗? – 2013-04-11 08:45:39

+0

您正在将书对象添加到用户ArrayList,而不是bookList – Abi 2013-04-11 08:45:47

+0

您是否调试过,看看您是否创建过书籍?没有足够的代码来帮助! – John3136 2013-04-11 08:45:52

回答

0

正如Abi所述,您将添加到错误列表中。 以更一般的方式,您应该使用OpenJPA,这更容易管理持久性。

+0

user.add函数允许我将对象添加到数组列表中 – 2013-04-11 08:59:56

+0

好的,您应该更加具体地将其重命名为:addBook(...) – Julien 2013-04-11 10:27:03

+0

为什么所有用户都拥有所有书籍?这个“Select * From Books”令我深感讨厌;) – Julien 2013-04-11 10:31:56

0
public User selectBooks() { 
    try { 

     User userObject=new User(); 

     //ArrayList<Books> bookList=new ArrayList<Books>(); 

     Statement stm = conn.createStatement(); 
     ResultSet rs = stm.executeQuery("SELECT * FROM books"); 
     while (rs.next()) { 
      Books books = new Books(); 

      books.setTitle(rs.getString(1)); 
      books.setAuthor(rs.getString(2)); 
      books.setISBN(rs.getInt(3)); 
      books.setPrice(rs.getDouble(4)); 

      user.getBookList().add(books); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return userObject; 
} 
+0

我已经改变user.add(books)到user.getBookList().add(books),但仍然没有显示出来 – 2013-04-11 09:04:58

+0

User mainUser = new User(); user = selectBooks(); System.out.println(user.getBookList()。(0)); – ashwinsakthi 2013-04-11 09:08:15

+0

尝试上面的代码,看看是否有东西打印在控制台中。 – ashwinsakthi 2013-04-11 09:09:01

1

我已经发现了问题所在。我忘了提及我正在使用jsf技术,并且还使用会话来存储对象和值。每次我尝试使用selectbooks方法时,它都会创建一个新用户,而不是使用当前会话用户。因此,它始终会生成一个新用户并为当前会话用户提供空值。