Join 150,038 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,586 people online right now. Registration is fast and FREE... Join Now!
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 = "nick2price"; public static final String PASSWORD = "hello";
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);
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 = "nick2price"; public static final String PASSWORD = "hello";
public PersonService(PersonInfo p) { Connection c = null; personDao = new PersonDAO();
I am being returned with a NullPointerException so somewhere i am making a mistake with the passing around of the connection. Can anyone see where i am going wrong? Stack trace is looking like this: java.lang.NullPointerException java.lang.NullPointerException at PersonDAO.savePerson(PersonDAO.java:22) at Register.save(Register.java:196) at Register.actionPerformed(Register.java:166)
In my PersonDao class, just before i carry out the prepared statement i have added the line
CODE
System.out.println("con is null? " + (con == null));
I am being returned back true, so this shows that it is to do with this class not recieving the connection object. I have tried your advice but still no change. Do you see any problem with the way i am passing the connection around?
I see what you mean by connection always being null. But if i remove
CODE
Connection connection = null;
from main, then it tells me that non static varible connection cannot be referenced from a static context. I still cant see my error
Two ways of fixing the problem:
If you are going to have only one Connection in all your program You can make
static Connection connection;
then your main() method will be able to access it an create a Connection.
Elewhere is your program if you want to access this connection you can always refer to:
DatabaseUtils.connection
and you don't have to pass it as parameter to your other methods:
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 = "nick2price"; public static final String PASSWORD = "hello";
public static Connection connection;
public static void main(String[] args) { try { if (args.length > 0) { connection = DatabaseUtils.connect(DRIVER, URL, USERNAME, PASSWORD); DatabaseUtils utils = new DatabaseUtils(connection);
If you want to handle mutltiple connections then you will have to use another connection
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 = "nick2price"; public static final String PASSWORD = "hello";
private Connection connection;
public static void main(String[] args) { try { if (args.length > 0) {
Connection con = DatabaseUtils.connect(DRIVER, URL, USERNAME, PASSWORD); DatabaseUtils utils = new DatabaseUtils(connection);
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 = "nick2price"; public static final String PASSWORD = "hello";
This is where i am getting stuck. Now i use the connect method in this class
CODE
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 = "nick2price"; public static final String PASSWORD = "hello";
public PersonService(PersonInfo p) { Connection c = null; personDao = new PersonDAO();
public void setConnection(Connection dbc) { this.con = dbc; }
So now the above method should have given the connection to the variable con in my personDAO class? But then when i try and use this in my savePerson method, there is no connection. Where have i lost my connection. i should be doing this DatabaseUtils class:-creates connection methods PersonService class:-creates connection and sends it to personDAO class. PersonDAO class:- gets connection from PersonService class and uses this connecion to execute a query.
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 = "nick2price"; public static final String PASSWORD = "hello";
This is where i am getting stuck. Now i use the connect method in this class
CODE
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 = "nick2price"; public static final String PASSWORD = "hello";
public PersonService(PersonInfo p) { Connection c = null; personDao = new PersonDAO();
Now that connection should be sent too personDao.setConnectionŠ; which is this method
CODE
public class PersonDAO{
private Connection con;
public void setConnection(Connection dbc) { this.con = dbc; }
So now the above method should have given the connection to the variable con in my personDAO class? But then when i try and use this in my savePerson method, there is no connection. Where have i lost my connection. i should be doing this DatabaseUtils class:-creates connection methods PersonService class:-creates connection and sends it to personDAO class. PersonDAO class:- gets connection from PersonService class and uses this connecion to execute a query.
Can you see where the connection is lost?
Without seeing the method PersonDAO.savePerson(PersonInfo p) I cannot speculate on what wrong you are doing And just to make your code clearer (and avoid the setConnection() method why don't you pass the Connection into the constructor of PersonDAO ?
CODE
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 = "nick2price"; public static final String PASSWORD = "hello";
You can see my savePerson() in my original post. I have put the connection in the personDAO constructor. Now how would pass this constructor the connection from my PersonService class?
CODE
try {
c = DatabaseUtils.connect(DRIVER, URL, USERNAME, PASSWORD); c.setAutoCommit(false); personDao(c); //it wont work like a method e.g. PersonDAO.personDao(c) c.commit(); }
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 = "nick2price"; public static final String PASSWORD = "hello";
public PersonService(PersonInfo p) { Connection c = null; personDao = new PersonDAO();
Are you telling me that... every time you will want to save a PersonInfo in your database you will:
- create a new PersonService object - that will create a new connection to the database - turn autoCommit to false (even if later you will just insert a row) - create a new PersonDAO object - set the cconnection handle to this PersonDAO object - PersonDAO object will created a PrepareStatement - PersonDAO object will perform the SQL Insert - do a commit (just for 1 row but auto commit was at off) - perform a rollBack if the commit doesn't work - close the connection meaning that the just created PersonServices and PersonDAO objects are now useless (including its prepareStatement what is the use of making a PrepareStatement if it is used only one time ?)
Not a good idea at all
You need to seriously review your design
You PersonDAO should look like that:
CODE
class PersonDAO { static Connection con; // connection used by all PersonDAO static PrepareStatement insert; // one prepareStement for all PersonDAO // init these static fields static { try { con = .......; insert = con.prepareStatement("Insert into ....... ?, ?, ?, ?"); } catch(SQLException e) { Display error or throw new IllegalStateException() } }
// make it synchronized if if can be called by more than one thread static synchonized boolean insert(PersonalInfo p) { try { insert.setString(1, p.... insert.setString(2, p.... ps.executeUpdate(); } catch(SQLException e) { System.out.println("PersonDao error: " + e); return false; } return true; } }
Now you don't even need PersonService anymode where you were creating one just do:
Really, what i have shown you is part of a bigger picture. So far i have in total 7 classes. Each time i will need to create a personService object because this is being used for a register/login form. So the connection will depend on the user choosing to register, not me manually inputting information. Also, i was told it is bad coding creating the connection in the PersonDAO class because the DAO can't know if it's part of a larger transaction. That's up to that other class, which is really a service.