java - 当请求检索所有记录时,数据库返回相同的记录
问题描述
昨天这段代码运行良好,现在却不行。我创建了一个图书数据库并将其连接到一个 java 前端(对不起,如果这些术语都是错误的!),最初它将为用户提供选择他们想要做什么的选项(控制器中的代码),然后使用switch 子句,输出用户的愿望。
这是我的图书数据库Access。这将建立与 sqlite 数据库的连接。
/** Class
*
* allocates variables and set to null
*/
static Connection c = null;
Statement s = null;
ResultSet r = null;
/**
* / Method to establish the Database connection
* @return connection or NotFoundException when unsuccessful
*/
private static Connection getDBConnection() {
try {
Class.forName("org.sqlite.JDBC"); // driver
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
String dbURL = "jdbc:sqlite:books.sqlite"; // connection to the database
c = DriverManager.getConnection(dbURL); // driver manager imported above
return c;
}
catch (SQLException e) { // imported exception above
System.out.println(e.getMessage());
}
return c;
}
public void closeConnection() { // method to close the connection
try {
if(s!=null) { // if s != to null, statement close
s.close ();
}
if (c!=null) { // if connection !=null,
}
} catch (SQLException e) {
e.printStackTrace();
}
}
检索所有书籍的方法。我创建了一个数组列表,然后连接到数据库,然后执行查询,昨天一切正常:S 今天早上....不,它现在重复生成同一本书。
/** Method to retrieve all books
* @return bookList
* @throws SQLException
*/
public static ArrayList<Books> getAllBooks() throws SQLException {
System.out.println ("Retrieving all books ...");
Connection dbconnection = null;
Statement statement = null;
ResultSet result = null;
String query = "Select * FROM books;";
Books temp = null; // books object - set to null
ArrayList <Books> bookList = new ArrayList <> ();
try {
dbconnection = getDBConnection (); // Establish connection
statement = dbconnection.createStatement();
System.out.println("DBQuery = " + query); // Print out what the query to the database was
result = statement.executeQuery(query);
while (result.next()) {
String book_id = result.getString("ID"); // Retrieves the String named "ID" from the database
String title = result.getString("Title");
String author = result.getString ("Author");
String year = result.getString("Year");
String edition = result.getString("Edition");
String publisher = result.getString("Publisher");
String isbn = result.getString("ISBN");
String cover = result.getString("Cover");
String condition = result.getString ("Condition");
String price = result.getString("Price");
String notes = result.getString ("Notes");
bookList.add(new Books(book_id, title, author, year, edition,
publisher, isbn,cover, condition, price, notes));
}
}
finally {
/** If result is null,
will not return anything and close the console.
*/
if (result != null) { result.close(); }
if (statement != null) { statement.close(); }
if (dbconnection != null) { dbconnection.close(); }
} return bookList;
}
对不起,我没有很好地表达自己。如果您也需要控制器代码,请告诉我。
图书
package sqlBooks;
import java.sql.SQLException;
/** Book class - all (11) variables set to string
* constructors
* stringToString
* getters and setters
* @author Jennifer Walsh
*/
public class Books {
private static String book_id;
private static String title;
private static String author;
private static String year;
private static String edition;
private static String publisher;
private static String isbn;
private static String cover;
private static String condition;
private static String price;
private static String notes;
// constructors
public Books (String book_id, String title, String author, String year, String edition,
String publisher, String isbn,String cover, String condition, String price, String notes) {
this.book_id=book_id;
this.title=title;
this.author=author;
this.year=year;
this.edition=edition;
this.publisher=publisher;
this.isbn=isbn;
this.cover=cover;
this.condition=condition;
this.price=price;
this.notes=notes;
}
// Outputs sensibly on request
public String toString() {
String s = "Book_id: "+ this.book_id+ '\n' +
"Title: "+ this.title+ '\n' +
"Author: " + this.author+ '\n' +
"Year (released): " + this.year + '\n' +
"Edition: " + this.edition + '\n' +
"Publisher: " + this.publisher + '\n' +
"ISBN: " + this.isbn + '\n' +
"Cover: " + this.condition + '\n' +
"Condition: " + this.cover + '\n' +
"Price: " + this.price + '\n' +
"Notes: " + this.notes + '\n';
return s;
}
// getters and setters
public static String getBook_id() {
return book_id;
}
public void setBook_id(String book_id) {
this.book_id = book_id;
}
public static String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public static String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public static String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public static String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public static String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public static String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public static String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public static String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public static String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
解决方案
推荐阅读
- php - Active Directory 仅返回 1000 条记录(PHP)
- javascript - chrome xml下载警告
- php - Lumen 5.7 JSON 响应限制
- javascript - 在 Openlayers 3 中加载 mapbox GL JS 底图样式
- c# - 何时在 C# 中使用“in”修饰符参数
- python - 我如何获得 TOCO tf_convert 的冻结 Tensorflow 模型的 input_shape
- node.js - 访问循环时,在循环外声明的变量确实会发生变化 - 节点
- html - 纯 css 手风琴菜单溢出
- docker - docker run 将参数传递给入口点
- javascript - 在 Postman 中,如何从 Get 请求中获取响应主体,并将其放入 PUT 请求中并进行细微更改