|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Calling a method from Main
Hi
I have a class which consits of the main method and another method called makeDBUpdate. In the main method I am creating a shell and then positioning a button on that shell. When the user creates a button the it is then supposed to call the makeDBUpdate method which in turn inserts a row in a MYSQL databse. However when I am clicking the button the following error is coming up: Unhandled exception type ClassNotFoundException Unhandled exception type SQLException Below you can see the code that I have written. import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.events.*; import java.sql.*; import java.sql.SQLException; public class TimeK { final static String UNAME = System.getProperty("user.name"); public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(270,200); // Create the "Break In" Button Button buttonBreakIn = new Button(shell,SWT.PUSH); buttonBreakIn.setSize (100,50); buttonBreakIn.setLocation(140,90); buttonBreakIn.setText("Break In"); buttonBreakIn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ String actionDo = "BIN"; makeDBUpdate(UNAME, actionDo); } }); shell.open(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } display.dispose(); } public static void makeDBUpdate (String UNAME, String actionDo) throws ClassNotFoundException, SQLException{ Class.forName ("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/timekeeper"; Connection con = null; con = DriverManager.getConnection(url,"root","Malta123"); Statement stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO main(Username,Action) VALUES (UNAME,actionDo)"); con.close(); } } The error is coming up in the above bold line which is: makeDBUpdate(UNAME, actionDo); Can you please help as I don't know what could the problem be? Thanks Gordon |
|
#2
|
||||
|
||||
|
It has been a while since I did some Java, but, I am actually not sure why your program compiled at all. MakeDBUpdate declares that it throws those 2 exceptions, but the caller of makeDBUpdate (= main) does not handle them. You should put a try catch around the call to makedbupdate. Hope this helps.
__________________
Current project: roborally |
|
#3
|
|||
|
|||
|
Hi Icon,
Thanks for your help. Your advice solved my problem. Cheers Gordon |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Calling a method from Main |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|