Hai all,

AM trying to connect java with database .I created class called data manager with several methods like setdburl(), getdnurl() ,etc..
i called the set functions inside another class and assign values. in set method i assigned LOCAL VARIABLE. when i call get url method it shows null value.. this my code

public class DataManager 
{
		
		Connection con = null;
		private String dbURL = "";
		private String dbUserName = "";
		private String dbPassword = "";
		private String jdbcDriver = "";
		
		/*
		 * setting the values for connection elements  
		 */

		public void setDbURL(String dbURL)
		{
			this.dbURL = dbURL;
			
		}
		
		
	    
	    /*
	     * retrieve the values  of  connection elements   
	     */
	    
	    public String getDbURL()
		{
			
			return dbURL;
		}
public openConnection{
      con = DriverManager.getConnection(getDbURL(),root,"");
	    
	    
	    }

Thanks in advance

when i call get url method it shows null value.

I assume you are talking about the dbURL variable.
Your code shows that the value of the varible: dbURL is initialized to an empty String.
Does the setDbURL method set it to null? Add a println in that method to show what value it is setting the variable to.

when i call get url method it shows null value.
Actually i called the method with dburl value
i.e

setDbURL("jdbc:mysql://localhost:3306/invoice")

now the method getDbURL() should return url value but still it return null value only ...


pls help me

could you paste the complete method in which you call this one?

public void iniMethod{
            DataManager dataManager = new DataManager();
	    dataManager.setDbURL("jdbc:mysql://localhost:3306/invoice");
	    dataManager.setDbUserName("root");
	    dataManager.setDbPassword("");
	    dataManager.setjdbcDriver("com.mysql.jdbc.Driver");
	    System.out.println(config.getInitParameter("jdbcDriver"));  
}

After that I need to call connection method

public void calMethod{             
		String str_val=null;
		DataManager dataManager=new DataManager(); 
		String str_userid="Anand";
		String str_pass="123";
		PrintWriter out = response.getWriter();
		dataManager.getConnection();
		dataManager.getUserData(str_userid,str_pass);
		}

You are not using the same instance of the DataManager class in the two method.
Each one has created its own version. There are two instances. One with data, the other without.

Move the definition of the DataManager object to a level common to both methods.

isn't DataManager methods that comings only from sqljdbc4.jar by Microsoft

thanks NormR1 now its working i used two instances that is my mistake ...

thanks mKorbel and stultuske

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.