Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database.
I encountered a problem in deletion function. E.g. I would like to delete one record in database, it always shows "record not found" in my program, even if the data has been deleted.
I tried to used function for each choices. But the compiler showed that we can't used function in static void. Is there anyway to use function in public static?
One more thing, as a beginner in Java programming, i need a comment about my program. Thank you very much guys.
http://upload2.net/page/download/LVZBO5gb7kMJdY5/Staff.mdb.html
I encountered a problem in deletion function. E.g. I would like to delete one record in database, it always shows "record not found" in my program, even if the data has been deleted.
I tried to used function for each choices. But the compiler showed that we can't used function in static void. Is there anyway to use function in public static?
One more thing, as a beginner in Java programming, i need a comment about my program. Thank you very much guys.
Code:
import java.io.*; import java.sql.*; public class DBMS { public static void main(String args[]) throws Exception { int select; BufferedReader choice1 = new BufferedReader (new InputStreamReader(System.in)); BufferedReader choice2 = new BufferedReader (new InputStreamReader(System.in)); BufferedReader entry1 = new BufferedReader (new InputStreamReader(System.in)); BufferedReader entry2 = new BufferedReader (new InputStreamReader(System.in)); BufferedReader entry3 = new BufferedReader (new InputStreamReader(System.in)); BufferedReader entry4 = new BufferedReader (new InputStreamReader(System.in)); String URL, Query1, Query2; Connection connection; PreparedStatement preparedStatement; URL = "jdbc:odbc:Staff"; Query1 = "INSERT INTO Emp VALUES(?,?,?)"; Query2 = "DELETE FROM Emp WHERE EmpNo = ?"; do { System.out.println("---------------------------------"); System.out.println("| * * * M E N U * * * |"); System.out.println("---------------------------------"); System.out.println("| 1 | New Employee Registration |"); System.out.println("| 2 | Employee Resignation |"); System.out.println("| 3 | Quit Program |"); System.out.println("---------------------------------"); System.out.print("Please Input the choice = "); select = Integer.parseInt(choice1.readLine()); if(select==1) { System.out.println("Please fill up the particular data as the following"); System.out.print("Employee No: "); int entryA = Integer.parseInt(entry1.readLine()); System.out.print("Employee Name: "); String entryB = entry2.readLine(); System.out.print("Employee Salary: "); double entryC = Double.valueOf(entry3.readLine()).doubleValue(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(URL,"",""); preparedStatement = connection.prepareStatement(Query1); preparedStatement.setInt(1,entryA); preparedStatement.setString(2,entryB); preparedStatement.setDouble(3,entryC); preparedStatement.executeUpdate(); System.out.println("RECORD INSERTED SUCCESSFULLY"); preparedStatement.close(); connection.close(); }catch(Exception e) { System.out.println("Error Occured"); } } if(select==2) { int delete=0,loop; System.out.println("Employee Resignation"); System.out.print("Employee No: "); int entryD = Integer.parseInt(entry4.readLine()); loop=1; while(loop==1) { System.out.println("Are you sure want to delete " +entryD+ "? (1) to continue | (2) to cancel"); System.out.print("Choice = "); int choicedel = Integer.parseInt(choice2.readLine()); if(choicedel==1) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(URL,"",""); preparedStatement = connection.prepareStatement(Query2); preparedStatement.setInt(1,entryD); preparedStatement.executeUpdate(); if(delete!=0) // Problem in this line { System.out.println("Employee with EmpNo " +entryD+ " has been deleted"); } else { System.out.println("Record not found"); } preparedStatement.close(); connection.close(); }catch(Exception e) { System.out.println("Error Occured"); } break; } if(choicedel==2) { System.out.println("Resignation has been canceled"); break; } else { System.out.println("Invalid, please refer to the selection"); loop=1; } } } }while(select!=3); } }
Comment