problems with connection to mysql database
When i try to execute the ListProdQty method it jumps from that method to the catch in the connecttodatabase method and gives me a java.lang.NullPointerException
public void ConnectToDatabase()
String user, pass, host;
user = readEntry("Please enter User ID : ");
pass = readEntry("Please enter Password : ");
host = readEntry("Please enter Hostname : ");
// database = readEntry("Please enter database name: ");
// userid, password and hostname are obtained from the console
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/oncs", user, pass);
/* JDBC default is to commit each SQL statement as it is sent to the database. Setting autocommmit=false changes the default
behaviour so that transactions have to be committed explicity.
private void ListProdQty()
// Creates a statement and a result set to execute the sql query.
s = conn.createStatement();
resultSet = s.executeQuery("SELECT pname FROM product"+
"WHERE pqtyinstock >5"+
" AND pretailprice > 150"+
"ORDER BY pname");
//Creates a loop to loop through the result set
//Prints the results to the user
//Commits the connection to the database
//Prints an error message to the user
Re: problems with connection to mysql database
First of all, welcome to the forum :)
And then youre code; I see in ListProdQty() that you call conn.createStatement() but! I dont see what conn is actually holding (and think this is part of youre problem).
In ConnectToDatabase() I do see you defining conn as a Connection with a connection provided by the Drivermanager. But! I dont see you putting conn into a attribute of the class or a way that conn in ListprodQty() knows that it was defined in ConnectToDatabase(). Try fixing this first.