[英]Error while using database in Java using SQLite
Bank class calling the Atm class.The machine class contains the start machine method.银行类调用 Atm 类。机器类包含启动机器方法。 Here onwards loop will run forever calling the required methods from another classes not relevant.这里向前循环将永远运行,从另一个不相关的类调用所需的方法。 The create table method should be called only once.Once the Pin is validated the program runs infinitely create table 方法应该只调用一次。一旦 Pin 被验证,程序就会无限运行
import java.util.Scanner;
public class Bank {
final static int PIN = 5423;//pin to start the ATM operations
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the PIN to start the machine");
for(int i = 3; i >= 1;--i) {
int n = scanner.nextInt();
if(n == PIN) {//if pin was correct , start the ATM
Atm atm = new Atm();
atm.startMachine();
break;
}
else if(i > 1)
System.out.println("Incorrect PIN. You have " + (i-1) + " tries remaining");
else {
System.out.println("Tries exhausted. Contact the main office.");
break;
}
}
scanner.close();
}
}
Atm class which has start machine method具有启动机器方法的 Atm 类
class Atm {//class for functioning of ATM
Scanner scan;
protected String load;//loading time
protected int cashAvailable = 2000000;//initial cash available
static Database database;
Atm(){//constructor to initialize fields
scan = new Scanner(System.in);
load = "00:00";//the loading time at midnight
database = new Database();
database.createTable();//call the database class
}
public void startMachine(){//start the machine
Machine machine = new Machine();
machine.start();//call the start method
}
}
And the machine class和机器类
public void start() {//method to start the machine
Withdraw withdraw = new Withdraw();//withdraw object
Deposit deposit = new Deposit();//deposit object
while(true) {//infinite loop for functioning of machine
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());//get the current time
boolean isLoadingTime = currentTime.equals(load);//if is loading time
if(isLoadingTime) {//if yes, the call load method
load();//method to load the cash
}
//show the welcome screen at every iteration
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println();
System.out.println("Welcome to National Bank");
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
System.out.print(formatter.format(date) + " ");
System.out.println(LocalDateTime.now().getDayOfWeek());
System.out.println("Press 1 to view your current balance");
System.out.println("Press 2 to withdraw cash");
System.out.println("Press 3 to deposit funds");
System.out.println();
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
while(true) {
int enter_num = scan.nextInt();//get the preferred choice
if(enter_num == 1) {
currentBalance();//method to show current balance
break;
}
else if(enter_num == 2) {
withdraw.debit();//method to withdraw cash
break;
}
else if(enter_num == 3) {
deposit.giveCash();//method to deposit cash
break;
}
else
break;//if not valid option
}
}
}
The obvious errors in your code in update()
method are the comma after SET Balance = ?
update()
方法中代码中的明显错误是SET Balance = ?
之后的逗号SET Balance = ?
which you must remove and also change the indices of stmt.setInt()
to 1
for balance
and 2
for accountNo
because you are passing 2 paremeters with that order in your UPDATE
statement:您必须删除它,并将stmt.setInt()
的索引更改为1
表示balance
和2
表示accountNo
因为您在UPDATE
语句中以该顺序传递了 2 个参数:
String change = "UPDATE accounts SET Balance = ? WHERE AccountNumber = ?";
try {
PreparedStatement stmt = conn.prepareStatement(change);
stmt.setInt(1,balance);
stmt.setInt(2,accountNo);
.............................
Also, the error message:此外,错误消息:
Abort due to constraint violation (UNIQUE constraint failed: accounts.AccountNumber)由于违反约束而中止(唯一约束失败:accounts.AccountNumber)
means that you are trying to insert in the table an account number that already exists in the table and this is not allowed because the column AccountNumber
is the primary key of the table so it is unique.意味着您试图在表中插入一个已经存在于表中的帐号,这是不允许的,因为AccountNumber
列是表的主键,因此它是唯一的。
Also, inside createTable()
what is sc
?另外,在createTable()
里面什么是sc
? Is it a resultset?是结果集吗?
If so, what are you trying to do?如果是这样,你想做什么? Are you trying to loop through the rows of the table?您是否试图遍历表的行?
The table is just created and it is empty.表刚刚创建,它是空的。
From the code that you posted, I can't see why you get 4 times the message "A new database has been created" , because I don't know how you call the method createTable()
.从您发布的代码中,我不明白为什么您会收到 4 次消息"A new database has been created" ,因为我不知道您如何调用createTable()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.