Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 20th, 2007, 03:48 AM
gbuhagia gbuhagia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 2 gbuhagia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 51 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old October 21st, 2007, 03:03 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 633 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 5 h 36 m 35 sec
Reputation Power: 3
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

Reply With Quote
  #3  
Old October 22nd, 2007, 12:23 PM
gbuhagia gbuhagia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 2 gbuhagia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 51 sec
Reputation Power: 0
Hi Icon,

Thanks for your help. Your advice solved my problem.

Cheers
Gordon

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Calling a method from Main


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway