Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes Null data issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Null data issue" Watch "Null data issue" New topic
Author

Null data issue

Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Hello All!

I am new to the JDBC forum, but I did post a JDBC question in the Java Beginner forum. I am still stuck so thought I would try this one out. Any ideas??

If this post is a little messed up, look at the post labeled Class.forName error in the beginner thread. I just copied and pasted from there. I realized that I should have probably posted to this one first, and couldn't just 'move' the post.

Thanks everyone!
Amy

OK, I tried using java.lang.forName(...) and it worked. So thanks! I also tried the Class.class.forName() and there it was! All ready to work. Man oh man...

Now, I have one more issue and then I will stop bugging everyone for a bit; I promise. I am trying to connect to a db and insert records into it. The insert works, but I also get an exception, saying that "No ResultSet was produced SQL State: null". I have my code in try / catch blocks so it catches the errors. It inserts the record fine, but still gives me the error BEFORE it reports the insert. So, I commented out the code to write the record to the database and I get strange results: every field comes up as null in the console (there are only 6 fields right now). Here is my code:


code:
--------------------------------------------------------------------------------

package com.lexx.ecommerce; import java.sql.*; /** * @author default * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class CustomerDataBaseProcessor { /** * */public CustomerDataBaseProcessor() {super();// TODO Auto-generated constructor stub} public static void main(String[] args) {CustomerDataBaseProcessor cdbp = new CustomerDataBaseProcessor();Customer cust = new Customer();cust.setFirstName("Clark");cust.setLastName("Kent");cust.setAddress("211 North");cust.setCity("Metropolis");cust.setState("IL");cust.setZip("60651");cdbp.setCustomerInfoToDB(cust);cust = cdbp.getCustomerInfoFromDB("Kent");System.out.println("Customer from DB:\n" + cust.getFirstName() + " " + cust.getLastName() + " " + cust.getAddress() + " " + cust.getCity() + " " + cust.getState() + " " + cust.getZip());} // end main methodpublic Customer getCustomerInfoFromDB(String nameId) {Customer cust = new Customer();String url = "jdbc dbc:CustomersDB";String sqlQuery = "SELECT FirstName, LastName, Address, City, State, Zip "+ "FROM Customer WHERE LastName ="+ "\'" + nameId + "\'";Connection conn = null;Statement stmt = null;ResultSet rs = null;try {java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");conn = DriverManager.getConnection(url, "lexx", "java");stmt = conn.createStatement();rs = stmt.executeQuery(sqlQuery);boolean isMoreData = rs.next();while (isMoreData) {cust.setFirstName(rs.getString(1));cust.setLastName(rs.getString(2));cust.setAddress(rs.getString(3));cust.setCity(rs.getString(4));cust.setState(rs.getString(5));cust.setZip(rs.getString(6));isMoreData = rs.next();} // end while loop} // end try blockcatch (ClassNotFoundException e) {System.out.println("Class not found: " + e.getMessage());} // end catch blockcatch (SQLException e) {while (e != null) {System.out.println("Exception: " + e.getMessage() + " SQL State: " + e.getSQLState());e = e.getNextException();} // end while loop} // end catch blockcatch (Exception e) {System.out.println("Exception: " + e.getMessage());} // end catch blockfinally {try {rs.close();stmt.close();conn.close();} // end try blockcatch (SQLException e) {System.out.println("Closing DB Objects Exception: " + e.getMessage() + " SQL State: " + e.getSQLState());e = e.getNextException();} // end catch block} // end finally blockreturn cust;} // end getCustomerInfoFromDB method public void setCustomerInfoToDB(Customer cust) { String url = "jdbc dbc:CustomersDB";String sql = "INSERT INTO Customer ( "+ "FirstName, LastName, Address, City, State, Zip"+ ") VALUES ('"+ cust.getFirstName()+ "','"+ cust.getLastName()+ "','"+ cust.getAddress()+ "','"+ cust.getCity()+ "','"+ cust.getState()+ "','"+ cust.getZip()+ "')";Connection conn = null;Statement stmt = null;try {java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");conn = DriverManager.getConnection(url, "lexx", "java");stmt = conn.createStatement();stmt.executeQuery(sql);} // end try blockcatch (ClassNotFoundException e) {System.out.println("Class not found: " + e.getMessage());} // end catch blockcatch (SQLException e) {while (e != null) {System.out.println("Exception: " + e.getMessage() + " SQL State: " + e.getSQLState());e = e.getNextException();} // end while loop} // end catch blockcatch (Exception e) {System.out.println("Exception: " + e.getMessage());} // end catch blockfinally {try {stmt.close();conn.close();} // end try blockcatch (SQLException e) {System.out.println("Closing DB Objects Exception: " + e.getMessage() + " SQL State: " + e.getSQLState());e = e.getNextException();} // end catch block} // end finally block} // end setCustomerInfoToDB method}

--------------------------------------------------------------------------------



There is also a Customer class in the same package that has the setFirstName(), setLastName(), setAddress(), etc. methods that it is using. That is also how I am creating a new customer object.

The results, when run WITH the setCustomerInfoToDB, are these:

Exception: No ResultSet was produced SQL State: null
Customer from DB:
Clark Kent 211 North Metropolis IL 60651

When run WITHOUT the setCustomerInfoToDB (I just commented it out), these are the results:

Customer From DB:
null null null null null null

Why is it giving the nulls? Any ideas??

Thanks!
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Holy man this is really messed up. Here is the code:



Sorry about that!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Amy,
You need to call executeUpdate() rather than executeQuery() on insert, delete or update statements.

For the future, you can edit your post by clicking the icon that looks like a pencil and paper. It's the third icon from the left near the top of your post.

In this case, it was fine to post a new thread as it was a different question based on the same code. (Class.forName vs resultsets.) If you accidently post in the wrong forum, you can ask a forum bartender or any sheriff to move your thread to the correct forum for you. This will also preserve any discussion on the topic.
[ April 09, 2005: Message edited by: Jeanne Boyarsky ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null data issue
 
Similar Threads
unable to forward using jsp:forward
Multiple Drivers
Exception:[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch
Class.forName error
Specific SQLException