Trying to first make sure this part of my project is working before implementing it into a GUI.
Trying to first create a new table in the data base called n012345_Accounts with 4 columns:
- Account Number
- Name
- Balance
- Lock
Then I want to populate the data of this table by reading the lines of the file I have created with in Accounts.txt which includes the following
Number Name Balance Locked
1001 Isabel_Newton 2000 yes
1002 Blake_Wool 1500 yes
1003 Martha_Curie 3000 no
1004 Nortom_Eef 1500 no
1009 Dan_Heckler 2000 yes
1010 Timothy_Wicket 4000 no
1011 Jane_Doe 5000 no
The purpose of this is to practice my understanding of using PreparedStatements and transactions. If anyone can see what the error is that is not allowing the creation of the table I would appreciate the input.
Currently when running my project the console returns
unable to create new table for accounts
//Create a GUI application for a bank
//it should manage fund transfers from one account to another
//1
//Start
//@ the start up it should create a table name YourStudentNumber_Accounts ( n012345)
//it should also populate this table with the information stored in the file provided ("Accounts.txt")
//2
//Then the application will ask for
//account number the funds are to be transferred from
//amount to be transferred
//account number funds are to be transferred to
//3
//Upon exit the application will present the contents of the Accounts table in standard output
//USE PREPARED STATEMENTS and TRANSACTIONS wherever appropriate
//All exceptions must be handled
import oracle.jdbc.pool.OracleDataSource;
import java.io.*;
import java.sql.*;
public class Main {
public static void main(String[] args) throws SQLException{
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:n012345/luckyone@calvin.humber.ca:1521:grok");
//try to connect to the database connection we have declared
try(Connection con = ods.getConnection()) {
//create a statement object
try (Statement stmt = con.createStatement()) {
try (ResultSet rs = stmt.executeQuery("CREATE TABLE n012345_Accounts (AccountNumber float(4) , Name varchar(25), Balance float(9), Lock varchar(25))")) {
try (BufferedReader reader = new BufferedReader(new FileReader("Accounts.txt"));) {
String line;
//do not automatically commit statements
con.setAutoCommit(false);
while ((line = reader.readLine()) != null) {
//inputting data into a String array splitting data by the space between the items in the file
String[] fields = line.split(" ");
String queryString = "INSERT INTO n012345_Accounts (AccountNumber, Name, Balance, Lock) VALUES(?,?,?,?)";
try (PreparedStatement statement = con.prepareStatement(queryString);) {
statement.setFloat(1, Float.parseFloat(fields[0]));
statement.setString(2, fields[1]);
statement.setFloat(3, Float.parseFloat(fields[2]));
statement.setString(4, fields[3]);
statement.executeUpdate();
} catch (Exception e) {
System.out.println("There was an error inserting into the database.");
}
System.out.println("Accounts.txt data was populated into the table n01494108_Accounts");
}
} catch (Exception e) {
System.out.println("unable to read the file.");
}
con.commit();
} catch (SQLException ex) {
System.out.println("unable to create new table for accounts");
}
//closes the statement
} catch (Exception e) {
//using rollback() to ensure no statements in a transaction are committed if an exception error occurs
con.rollback();
}
}catch (SQLException ex){
//closes connection
}
} //closes main method
} // closes main class