• Threads in java.
    5 replies, posted
Hey, so I am learning this thing Threads in java and well I am completely pointless in it, I don't get it.... I am using databases to get info, when I click get info button program freezes for about 5 seconds so I was suggested to use Threads. Anyways, I have a method which makes a connection to database called conn(); [code]public void conn(){ mySQL = new SimpleMySQL(); mySQL.connect(host, user, pass4, "arleitiss"); }[/code] It's located in a class called SQL.java From my main class I call it right now when the button is clicked, I want it to connect to database on program start up, how would I do this? I tried this: [code] public static void main(String args[]) { Thread c1; c1 = new Thread(); final SQL mys; mys = new SQL(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new main().setVisible(true); mys.conn(); } }); } private void loginActionPerformed(java.awt.event.ActionEvent evt) { SQL mys; mys = new SQL(); String email = ema.getText(); String password = Password.getText(); String getPass = "SELECT * FROM users WHERE `email`='"+email+"'"; status.setText(mys.qGet(getPass, "name")); } [/code] But I feel I did something very stupid :v: When I click run the app opens and it's loading for like 5 seconds, it looks like it's connecting, yet when I click submit it throws an exception so I am sure there is something wrong in that. Can someone help? I am still only learning this java thing.
That's not how you use threads. And if you're only just learning Java now, you shouldn't worry too much about it as it can be some complicated stuff. Also, post the exception you get. I expect it's because in the loginActionPerformed method, you are creating a new SQL object without running its conn() method and thus the mys.qGet() method returns null.
Yeah man, threads is complicated stuff. Haven't quite learned it that well myself.
A few minor pointers. I see you do this when declaring new objects: [code] Thread c1; c1 = new Thread(); [/code] ...you can shorten this to: [code] Thread c1 = new Thread(); [/code] In reply to your question, however, it would appear that your SQL syntax [I]may[/I] be invalid. Try this: [code] SELECT * FROM users WHERE email = '" + email + "' [/code] Make sure you also sanitise the user's input before processing it though.
You can use [url=http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html]Prepared Statements[/url] to avoid having to sanitize everything(which can get a bit repetitive).
Your mistake is not in the code you posted, zip your project and upload it and i am sure at least 1 person will look at it for you. EDIT: Also for futher posts, while errors don't always make sense to you, they might make sense to people with more experience and help them identity your problem.
Sorry, you need to Log In to post a reply to this thread.