我只是在学习sql,这是我的第一个任务,将它整合到java应用程序中。我有问题,由用户输入多个字,它打破了。如果我只在word上输入没有空格的单词,一切都会很好。我真的很感激你的帮助!
下面是一些错误的截图(可能与扫描仪有关,但就我的生命而言,我无法修复它!)代码如下。
如果我键入2个单词,则会同时显示以下2个.next()。
但如果我只打一个字,它就不会断。
按标题“搜索”也一样,如果键入多个单词,则会出现以下错误。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
// This is a simple bookstore application that allows the user to add, update,
// delete and search for books in the MySQL database.
// Book class
class Books {
public int id;
public String title;
public String author;
public int qty;
public String search;
public Books() {
}
public Books(int id, String title, String author, int qty) {
this.id = id;
this.title = title;
this.author = author;
this.qty = qty;
}
}
// BookStore Class
public class BookStore {
public int choice;
static int id = 0;
static String title = "";
static String author = "";
static int qty = 0;
public static void main(String[] args) throws SQLException {
boolean quit = false;
Scanner input = new Scanner(System.in);
int menu = 0;
System.out.println("Betty's Book Store\n-----------------------------");
// SQL details
String url = "jdbc:mysql://localhost:3306/ebookstore_db?useSSL=false";
String user = "otheruser";
String password = "swordfish";
PreparedStatement myStmt = null;
Connection myConn = null;
do {
// Menu
System.out.println();
System.out.println("1. Enter book");
System.out.println("2. Update book");
System.out.println("3. Delete book");
System.out.println("4. Search books");
System.out.println("5. Exit");
System.out.print("\nPlease enter your choice: ");
menu = input.nextInt();
System.out.println();
switch (menu) {
case 1:
System.out.println("1. Enter book\n-----------------------------");
try {
// Add a new book
System.out.print("Book ID: ");
int id = input.nextInt();
System.out.print("Book Title: ");
String title = input.next();
System.out.print("Book Author: ");
String author = input.next();
System.out.print("Quantity: ");
int qty = input.nextInt();
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "INSERT INTO books " + " (id, title, author, qty)" + " values (?, ?, ?, ?)";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setInt(1, id);
myStmt.setString(2, title);
myStmt.setString(3, author);
myStmt.setInt(4, qty);
// 3. Execute SQL query
myStmt.executeUpdate();
System.out.println("Insert complete.");
printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 2:
// Update a book's data
System.out.println("2. Update book\n-----------------------------");
try {
// Search for book by ID
System.out.print("Enter Book ID: ");
int searchId = input.nextInt();
int id = searchId;
ResultSet results;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "SELECT * FROM books WHERE id = ?";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setInt(1, id);
// Set parameter values
myStmt.setInt(1, id);
// 3. Execute SQL query
results = myStmt.executeQuery();
// Loop over the results, printing them all.
while (results.next()) {
System.out.println(results.getInt("id") + ", " + results.getString("title") + ", "
+ results.getString("author") + ", " + results.getInt("qty") + "\n");
}
// Close up our connections
results.close();
// Sub menu for updating book data after selecting ID
System.out.println("1. Update ID");
System.out.println("2. Update Title");
System.out.println("3. Update Author");
System.out.println("4. Update Quantity");
System.out.println("5. Main menu");
do {
System.out.print("\nPlease enter your choice: ");
menu = input.nextInt();
System.out.println();
switch (menu) {
case 1:
try {
// Update Book ID
System.out.print("Enter New ID: ");
int updateId = input.nextInt();
int id2 = updateId;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql1 = "UPDATE books SET id = ? WHERE id = ?";
myStmt = myConn.prepareStatement(sql1);
// Set parameter values
myStmt.setInt(1, id2);
myStmt.setInt(2, id);
// 3. Execute SQL query
myStmt.execute();
System.out.println("ID updated.");
printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 2:
try {
// Update Book title
System.out.print("Enter New Title: ");
String title = input.next();
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql1 = "UPDATE books SET title = ? WHERE id = ?";
myStmt = myConn.prepareStatement(sql1);
// Set parameter values
myStmt.setString(1, title);
myStmt.setInt(2, id);
// 3. Execute SQL query
myStmt.execute();
System.out.println("Title updated.");
printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 3:
try {
// Update Book Author
System.out.print("Enter New Author: ");
String author = input.next();
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql1 = "UPDATE books SET author = ? WHERE id = ?";
myStmt = myConn.prepareStatement(sql1);
// Set parameter values
myStmt.setString(1, author);
myStmt.setInt(2, id);
// 3. Execute SQL query
myStmt.execute();
System.out.println("Autor updated.");
printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 4:
try {
// Update Book Quantity
System.out.print("Enter New Quantity: ");
int qty = input.nextInt();
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql1 = "UPDATE books SET qty = ? WHERE id = ?";
myStmt = myConn.prepareStatement(sql1);
// Set parameter values
myStmt.setInt(1, qty);
myStmt.setInt(2, id);
// 3. Execute SQL query
myStmt.execute();
System.out.println("Quantity updated.");
printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 5:
break;
default:
System.out.println("Invalid Entry");
}
} while (menu != 5);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 3:
// Delete a book from the database
System.out.println("3. Delete book\n-----------------------------");
try {
// Search
System.out.print("Enter Book ID to delete: ");
int searchId = input.nextInt();
int id = searchId;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "DELETE FROM books WHERE id = ?";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setInt(1, id);
// 3. Execute SQL query
myStmt.executeUpdate();
System.out.println("Delete complete.\n");
// printAllFromTable(myStmt);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 4:
// Search for a book in the database
System.out.println("4. Search books\n-----------------------------");
System.out.println();
do {
// Sub menu for searching by ID, title or author
System.out.println("1. Enter ID");
System.out.println("2. Enter Title");
System.out.println("3. Enter Author");
System.out.println("4. Main menu");
System.out.print("\nPlease enter your choice: ");
menu = input.nextInt();
System.out.println();
switch (menu) {
case 1:
try {
// Search by book id
System.out.print("Enter Book ID: ");
int searchId = input.nextInt();
int id = searchId;
ResultSet results;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "SELECT * FROM books WHERE id = ?";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setInt(1, id);
// 3. Execute SQL query
results = myStmt.executeQuery();
// Loop over the results, printing them all.
while (results.next()) {
System.out.println(results.getInt("id") + ", " + results.getString("title") + ", "
+ results.getString("author") + ", " + results.getInt("qty"));
}
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 2:
try {
// Search by book title
System.out.print("Enter Book Title: ");
String searchTitle = input.next();
String title = searchTitle;
ResultSet results;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "SELECT * FROM books WHERE title = ?";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setString(1, title);
// 3. Execute SQL query
results = myStmt.executeQuery();
// Loop over the results, printing them all.
while (results.next()) {
System.out.println(results.getInt("id") + ", " + results.getString("title") + ", "
+ results.getString("author") + ", " + results.getInt("qty"));
}
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 3:
try {
// Search by book Author
System.out.print("Enter Author: ");
String searchAuthor = input.next();
String title = searchAuthor;
ResultSet results;
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
String sql = "SELECT * FROM books WHERE author = ?";
myStmt = myConn.prepareStatement(sql);
// Set parameter values
myStmt.setString(1, title);
// 3. Execute SQL query
results = myStmt.executeQuery();
// Loop over the results, printing them all.
while (results.next()) {
System.out.println(results.getInt("id") + ", " + results.getString("title") + ", "
+ results.getString("author") + ", " + results.getInt("qty"));
}
} catch (Exception exc) {
exc.printStackTrace();
} finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
break;
case 4:
break;
default:
System.out.println("Invalid Entry");
}
} while (menu != 4);
break;
case 5:
// Exits the program
System.out.println("Exiting...");
quit = true;
break;
default:
System.out.println("Invalid Entry");
}
} while (!quit);
if (input != null) {
input.close();
}
}
/**
* Method printing all values in all rows. Takes a statement to try to avoid
* spreading DB access too far.
*
* @param a statement on an existing connection
* @throws SQLException
*/
public static void printAllFromTable(Statement statement) throws SQLException {
ResultSet results = statement.executeQuery("SELECT id, title, author, qty FROM books");
while (results.next()) {
System.out.println(results.getInt("id") + ", " + results.getString("title") + ", "
+ results.getString("author") + ", " + results.getInt("qty"));
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!