Results 1 to 7 of 7

Thread: Java JDBC help

  1. #1
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default Java JDBC help

    I am really not getting any further with this, so frustrating. I have a DatabaseUtils class
    Java 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
    Java 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
    Java 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

  2. #2
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Can you print the stack trace relating to the null pointer exception please?

  3. #3
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default

    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

  4. #4
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default stack trace

    manage to find where the exception was, here is the stacktrace
    Java 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

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    the "person" you pass to the method savePerson is null.

  6. #6
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default

    So it has nothing to do with my connection passing?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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
    Java Code:
    at PersonDAO.savePerson(PersonDAO.java:22)

Similar Threads

  1. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  2. Replies: 0
    Last Post: 03-21-2008, 01:13 PM
  3. Replies: 0
    Last Post: 12-18-2007, 06:07 PM
  4. Replies: 0
    Last Post: 09-28-2007, 12:56 PM
  5. Replies: 1
    Last Post: 06-27-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •