Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Prepared Statements

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i'm trying to create a prepared statement that updates the database when values are input from a text field and then the submit button is clicked. I'll explain what i've done so far

Take the input from a text field



and then validated the data by using the following



and the part i'm stuck on is the prepared statement to insert the value into the database, this is what I have so far



But firstly I keep getting a null pointer exception on line 130 which is this line



I can't seem to figure it out even though I know that the following things cause a null pointer exception


- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.




 
Bartender
Posts: 1164
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three mistakes in your code.  The most severe is that you are catching any sqlexception at line 15 without handling it.This way, you are not able to verify if something went wrong with execution of sql code....and this leads  to the question you posted here
Try to print out the stack trace in the catch block: you will see that your prepared statement is not prepared at all :you missed an enclosing parentheses at line 3....
About the third error...well, run your code  
 
Ranch Hand
Posts: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code should be



And as pointed out by Claude Moore, its good practice to always print stack trace while catching the exception.

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The connection is null is my JDBC class



but when I run the code it still points me back to this line here

 
Rancher
Posts: 4183
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is that connection created?
Since it's null then it's clearly that part that is incorrect.
 
M Khalid
Ranch Hand
Posts: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mitchell bat wrote:The connection is null is my JDBC class



connection object has null reference here, while in JDBC Class conn is referring to a Valid Connection object.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I have to call the object from the JDBC class into the secondframe class?
 
M Khalid
Ranch Hand
Posts: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mitchell bat wrote:Do I have to call the object from the JDBC class into the secondframe class?


Yes, you can have a member function returning the Connection object in JDBC class and in current class you can make an instance of JDBC class and calling the function to get a valid Connection object.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mitchell bat wrote:Do I have to call the object from the JDBC class into the secondframe class?


Definitely! In this topic you'll find a possible example of how you can achieve this (using a ConnectionFactory class).
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between ConnectionFactory and my JDBC class?

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've completely butchered everything and i'm so lost. I returned the connection object in the JDBC class and then in the secondframe class I created an instance of the JDBC class and then called the function to get a valid connection object.



and then in the SecondFrame class I did this

 
Dave Tolls
Rancher
Posts: 4183
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method is static, so there's no need at all to create an instance of JDBC.

Also I would recommend giving it a proper name, with proper parameters (none in this case).

ETA: Also, throw that SQLException, don't catch and log it.
If it can't connect then you want to know about it ASAP as you won't be able to process any statements.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also I would recommend giving it a proper name, with proper parameters (none in this case)



Giving what a proper name?


Also, throw that SQLException, don't catch and log it.



 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mitchell bat wrote:What is the difference between ConnectionFactory and my JDBC class?


Your JDBC class is useless and not appropriate for this use case. Creating a new Connection should not happen in a static method called main(). If you prefer a JDBC class, that's ok. But you should definitely rename the method (e.g. createConnection()) and use appropriate parameters (because it doesn't need a String array in my opinion). Also it's not required to have a static method. In fact, too much static methods is a (very) strong indication for non-OO code.

After you have made these changes, you can use this class in the SecondFrame class. You create a (private final) instance member jdbc, which is initialized with a new JDBC instance. Then in your code to save the value, you can do something like thisPreferably you want to use a try-with-resources statement to make sure your resources (like the connection and the prepared statement) are closed automatically, so you don't have resource leaks. So the code might look like

Hope it helps!
Kind regards,
Roel
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Creating a new Connection should not happen in a static method called main()



I created it that way originally so I could test the connection to make sure that the class connects to the database.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I changed afew things around and the null pointer exception has been sorted, now my only problem is when I run the program and click on the login button it takes me to my secondFrame class for a second and then moves onto the next window. It happened ever since I corrected my previous mistakes. Could it be an issue with the connection that the class is trying to make to the database?

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!