Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC connection problem

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i'm working on making a connection from my login page to the next frame using Netbeans using a login and password system. Everything works fine except for the login page, I think i've confused myself with the try and catch statements. Can you guys take a look at let me know what you think. Cheers in advance! I will only post the 2 classes of login and mysqlconnect to make things easier.





 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the login file this is the error message that I get

run:
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at app.Login$MySQLConnect.ConnectDb(Login.java:271)
at app.Login$MySQLConnect.access$300(Login.java:268)
at app.Login.jButton1MouseClicked(Login.java:187)
at app.Login.access$200(Login.java:13)
at app.Login$3.mouseClicked(Login.java:113)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 8 seconds)
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is still an awful lot of code. 
Most of the Swing stuff is probably irrelevant to the question at hand.

Perhaps line 277 in your code as posted here might be relevant to your exception ?

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following errors are at these lines

200 - unreported exception SQLException; must be caught or declared to be thrown
201 - unreported exception SQLException; must be caught or declared to be thrown
208 - illegal start of type, cannot find symbol, symbol: variable next, location: variable rs of type resultset, illegal start of expression
215 - else without if
219 - catch without try
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not wrong this is the code where you try to connect to database.
Why do you have this static class in Login class on line 268?
I think this code is calling to the static method of static class MySQLConnect instead of calling method of public class MySQLConnect. That is why it is throwing UnsupportedOperationException which is created in ConnectDb static method of static class MySQLConnect.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • You created extra block using curly braces, opening brace on line no 193 and closing brace on line 224. It doesn't cause any error but no need of it

  • mitchell bat wrote:200 - unreported exception SQLException; must be caught or declared to be thrown
    201 - unreported exception SQLException; must be caught or declared to be thrown

  • On line no 200 and 201 in below code. Method setString(int parameterIndex,String x) of PreparedStatement throws SQLException so you must catch it so alway write that code in try block

  • mitchell bat wrote:208 - illegal start of type, cannot find symbol, symbol: variable next, location: variable rs of type resultset, illegal start of expression

  • Below is the code on line no 208. Here .next() is the method ResultSet i.e. boolean next() throws SQLException. I think you forgot to add circular brackets i.e. ()

  • 215 - else without if
    219 - catch without try
  • Here is that code where you missed closing curly bracket of if statement on line no 215

  • On line 219 you have catch without try block so write try{ before

  •  
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • On line 190, your JButton name is
  • Always use expressive name for components like jbtnLogin or whichever you feel more expressive, so at first glance you understand what the code does when this button is clicked.
    Please read this Java Programming Style

  • If you are using JDBC API 4.0 or later version and JDK 6 or later, then you don't have to write following code to load the drivers. Because DriverManager class does it when it is loaded. This class has static initializer which loads and registers drivers using ServiceLoader which was introduced in JDK 6.
  • Hope you understood. If still has doubts, you're always welcome
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Always use expressive name for components like jbtnLogin or whichever you feel more expressive, so at first glance you understand what the code does when this button is clicked.
    Please read this Java Programming Style


    Thanks for that, makes alot more sense and easier to understand

    If you are using JDBC API 4.0 or later version and JDK 6 or later, then you don't have to write following code to load the drivers. Because DriverManager class does it when it is loaded. This class has static initializer which loads and registers drivers using ServiceLoader which was introduced in JDK 6


    At the moment i'm using version 5.1.39 so i'll take that out

    As for the code now it works fine with no errors but the only issue now is when I type in my username and password I get the following message

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at app.Login.jButton1MouseClicked(Login.java:194)
    at app.Login.access$200(Login.java:13)
    at app.Login$3.mouseClicked(Login.java:113)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6528)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    BUILD SUCCESSFUL (total time: 5 seconds)


    so.....



    takes me to this line

              



    takes me to this line



    and



    takes me to this line



     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • Make sure your calling method of MySQLConnect class but not inner static class's method because I don't know what is your new updated code looks like.
  • Make sure your connection is established in public static Connection ConnectDB() method of public class MySQLConnect by writing below code in that method before return conn;
  • Check what it prints when you click on that JButton.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Check what it prints when you click on that JButton.


    Nothing is printed

    Here is the new code



     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where does pst get initialised?
    You leap from declaring the sql String to trying to set values on your PreparedStatement.  There is no attempt to create a PreparedStatement in between.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:Nothing is printed

    Because you are not calling public static Connection ConnectDB() method of MySQLConnect and that is the reason your connection is not established.

  • Remove this code or comment that, why do you need that?

  • Write MySQLConnect class's method like below.



  • Before below code on line no 198, write code to call method of MySQLConnect  to establish connection. To make sure it is calling correct method.
    Just press ctrl and click on ConnectDb(), see where it leads you
  •  
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Like I've said I've confused myself and stuck
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:Like I've said I've confused myself and stuck
    That means you are trying to solve it, Isn't that best thing 
     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:
  • Before below code on line no 198, write code to call method of MySQLConnect  to establish connection. To make sure it is calling correct method.
    Just press ctrl and click on ConnectDb(), see where it leads you


  • That won't cure the null pointer exception.
    Still need to actually set pst to something.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:That won't cure the null pointer exception.
    Still need to actually set pst to something.
    I thought better first check whether connection is established or not. Because he/she is using conn which is initialied as null and not calling the method of MySQLConnect which returns an object of Connection to assign to that conn. Do you think we have left setting pst to sth else more?
     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Null Pointer shown earlier is entirely down to 'pst' never being set to anything (let alone a PS using that SQL string).

    From the latest code posted that rogue connection class has been removed from the Login file, so I would hope that the proper one, which does make a connection, will be used.
    Though that one should not be eating exceptions like that, and should be throwing them and letting the app crash, at least for now.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:is entirely down to 'pst' never being set to anything (let alone a PS using that SQL string)
    Apologize, hassling you more. Sometime it is very difficult for non-native english speakers to understand what some statements in english means. I really have no notion what precisely this statement means.
     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No problem.

    The code posted above (the second lot, with the changes) has this:


    The NullPointerException that mitchell reported is thrown from this like:


    At this point, 'pst' is null.  It doesn't have anything assigned to it until 2 lines later on, with the call to conn.prepareStatement.
    That line should be before any of the calls to setString on 'pst'.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:That line should be before any of the calls to setString on 'pst'.
    Opps   I didn't even notice that. Thank you so much
     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:
    Dave Tolls wrote:That line should be before any of the calls to setString on 'pst'.
    Opps   I didn't even notice that. Thank you so much


    I should own up that I only noticed when I wrote that reply to you!


    I did notice that 'pst' had to be null, but completely missed the prepare call two lines later...
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guys, i've spent a fair bit of time trying to understand where I went wrong but can't figure it out. Should I scrap the connection and start again?
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:Guys, i've spent a fair bit of time trying to understand where I went wrong but can't figure it out. Should I scrap the connection and start again?
    have you made changes mentioned above in your code.? Did it print connection established successfully?  Also make below change in your code on line no 193 in new updated code.please show what error it gives again.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried it but still had the same error message, tomorrow when I get a chance i'm going to re do it all and go through my notes again and post my results. I sincerely apologize and hope I won't make the same mistake again, I think I confused myself and jumped the gun when I was writing this up. I did however take a note out of your book and changed the variables names to more meaningful names so it is alot easier to follow.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:I tried it but still had the same error message, tomorrow when I get a chance i'm going to re do it all and go through my notes again and post my results. I sincerely apologize and hope I won't make the same mistake again, I think I confused myself and jumped the gun when I was writing this up. I did however take a note out of your book and changed the variables names to more meaningful names so it is alot easier to follow.
    Yes you are always welcome. Keep trying
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So i'm still confused on writing this so bare with me guys. So lets start from step 1 of this and i'll try to explain this so we're all on the same page

    So i've got a login class called 'login' with a username (jtextfield) and password (jpasswordfield) field and what I want to happen is when the user enters a string in the username field and the password I want it to check up against the MySQL database (or I could even hard code a username and password) and when the login button is clicked I want it to either login to the program or if the credentials are wrong I want a dialog box to pop up and say try again
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First create simple form which has JTextField for user name and JPasswordField for user password. When user clicks on login (JButton) check if it matches hard coded user name and password according to that display message using  JOptionaPane dialog. Like below I created which just checks hard coded user name and password and displays message according to that.
    loginForm.png
    [Thumbnail for loginForm.png]
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you post your source code?
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:Can you post your source code?
    Since you are struggling,
    On Login button click:
    but remember Not A Code Mill Please post only method which do login check and databse connection etc. , not whole code generated by NetBeans IDE.
     
    mitchell bat
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Something to do with netbeans isn't letting me use the action listener properly so I'll have to do the login from scratch. Thanks so much for your help

    This won't be the last you hear from me
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:This won't be the last you hear from me
      keep trying until you solve it. We are always there to help as much as we can
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mitchell bat wrote:Something to do with netbeans isn't letting me use the action listener properly so I'll have to do the login from scratch.
    Use IDE for design else it will take much time to code it manually.
     
    Dave Tolls
    Ranch Hand
    Posts: 2102
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do not use the Netbeans GUI designer.
    You'll get completely lost.
    It's not a simple tool by any means, and even worse if you don't understand how Swing fits together.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:It's not a simple tool by any means, and even worse if you don't understand how Swing fits together.
    yes you need to understand how to use the multiple layouts as appearance varies on different resolutions. I did couple of desktop applications using swing, was very tough to maintain same appearance on all systems.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic