Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problems with connection to mysql database

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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()
    {
        try
        {
        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.
         */
        conn.setAutoCommit(false);
        MenuMain();
        }
        catch(Exception ex)
        {
        	System.out.println(ex);
        }
      } 
     
     
     
     
     
    private void ListProdQty()
        {
        try
        	{
        	// 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
    			while(resultSet.next()) 
          {
          	//Prints the results to the user
    	 		  System.out.println(resultSet.getString(1));
          }
          //Commits the connection to the database
          conn.commit();
     
        	}
        	catch(SQLException excep)
        	{
        		//Prints an error message to the user
        		System.out.println(excep);
        	}
        }
    Last edited by thepower; April 18th, 2010 at 02:05 PM.


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: problems with connection to mysql database

    Hey thepower,

    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.

Similar Threads

  1. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM
  2. Unable to create a new connection!
    By fh84 in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2009, 05:58 PM
  3. How database connection pooling works in a application
    By JayVirk in forum JDBC & Databases
    Replies: 0
    Last Post: October 10th, 2009, 07:14 AM
  4. Replies: 4
    Last Post: September 19th, 2009, 11:56 PM
  5. cant get rid of http connection
    By kartik in forum Java Networking
    Replies: 1
    Last Post: July 21st, 2009, 03:09 AM