Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2008, 01:50 AM
Member
 
Join Date: May 2008
Posts: 20
nick2price is on a distinguished road
Java JDBC help
I am really not getting any further with this, so frustrating. I have a DatabaseUtils class
Code:
public class DatabaseUtils { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String URL = "jdbc:oracle:thin:@localhost:1521:XE"; public static final String USERNAME = "rory"; public static final String PASSWORD = "macflid"; private Connection connection; public static void main(String[] args) { Connection connection = null; try { if (args.length > 0) { connection = DatabaseUtils.connect(DRIVER, URL, USERNAME, PASSWORD); DatabaseUtils utils = new DatabaseUtils(connection); } else { System.out.println("Usage: DatabaseUtils <sql query>"); } } catch (Exception e) { e.printStackTrace(); } finally { close(connection); } } public DatabaseUtils(Connection connection) { this.connection = connection; } public static Connection connect(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException { Class.forName(driver); return DriverManager.getConnection(url, username, password); } public static void close(Connection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement statement) { try { if (statement != null) { statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void rollback(Connection connection) { try { if (connection != null) { connection.rollback(); } } catch (SQLException e) { e.printStackTrace(); } } }
Then i am trying to get another class create the connection to pass it to my DAO clas
Code:
import java.util.*; import java.sql.*; class PersonService { private PersonDAO personDao; public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String URL = "jdbc:oracle:thin:@localhost:1521:XE"; public static final String USERNAME = "rory"; public static final String PASSWORD = "macflid"; public PersonService(PersonInfo p) { Connection c = null; try { c = DatabaseUtils.connect(DRIVER, URL, USERNAME, PASSWORD); c.setAutoCommit(false); personDao.setConnection(c); personDao.savePerson(p); c.commit(); } catch (Exception e) { DatabaseUtils.rollback(c); e.printStackTrace(); } finally { DatabaseUtils.close(c); } } }
Then in my DAO class i am trying to do an isert query
Code:
public class PersonDAO{ private Connection con; public void setConnection(Connection c) { con=c; } public void savePerson(PersonInfo person) { try { String sql = "INSERT INTO Person(name, idNo, " + "userName, passWord) VALUES (?,?,?,?) "; // Create a Preparedstatement PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, person.getName()); ps.setString(2, person.getIdNo()); ps.setString(3, person.getUsername()); ps.setString(4, person.getPassword()); ps.executeUpdate(); } catch(Exception e) { System.out.println(e); } }
Nothing is being inputted into my database. I have just really confused myself. Can anyone see where i am going wrong? i am being return at runtime a NullPointerException.
cheers
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-07-2008, 01:48 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
Can you print the stack trace relating to the null pointer exception please?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-07-2008, 10:12 PM
Member
 
Join Date: May 2008
Posts: 20
nick2price is on a distinguished road
I would if i knew where the exception was coming from. I have lots of different classes and methods that i have no idea where its from. The only thing i do know is that it must be coming from recieving an empty connection object when i setConnection in my personDao class
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-07-2008, 10:25 PM
Member
 
Join Date: May 2008
Posts: 20
nick2price is on a distinguished road
stack trace
manage to find where the exception was, here is the stacktrace
Code:
java.lang.NullPointerException java.lang.NullPointerException at PersonDAO.savePerson(PersonDAO.java:22) at Register.save(Register.java:196) at Register.actionPerformed(Register.java:166) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav a:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel .java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242 ) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL istener.java:236) at java.awt.Component.processMouseEvent(Component.java:6041) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5806) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4413) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322 ) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2440) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) Press any key to continue...
Would you able to tell me how to fix my code from this?
cheers
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-08-2008, 04:09 AM
Senior Member
 
Join Date: Jun 2008
Posts: 484
masijade is on a distinguished road
the "person" you pass to the method savePerson is null.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-08-2008, 04:35 AM
Member
 
Join Date: May 2008
Posts: 20
nick2price is on a distinguished road
So it has nothing to do with my connection passing?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-09-2008, 11:15 AM
Senior Member
 
Join Date: Jun 2008
Posts: 484
masijade is on a distinguished road
Well, it could be that, as well, but it would help to know exactly which line is line 22 in PersonDAO.java, as indicated by the stacktrace
Code:
at PersonDAO.savePerson(PersonDAO.java:22)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use JDBC Template classes to control basic JDBC processing and error handling Java Tip Java Tips 0 04-01-2008 12:17 PM
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver haneeshrawther Advanced Java 0 03-21-2008 03:13 PM
how to connet to MS SQL in java by using net.sourceforge.jtds.jdbc driver cambo Database 0 12-18-2007 08:07 PM
How to use JDBC Template classes to control basic JDBC processing and error handling JavaBean Java Tips 0 09-28-2007 02:56 PM
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Marcus Database 1 06-27-2007 07:57 PM


All times are GMT +3. The time now is 12:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org