• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Accessing variable from another method

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code. On line 38 in method MakeConnection() I am trying to access some variable from method ReadIni()above but it tells me each one "is not accessible or is not a field". What does it want from me?
Rebecca


import java.io.*;
import java.sql.*;
import java.util.Properties;

class ConnectionMaker {
String database;
String user;
String password;

public void ReadIni() throws IOException{
File iniFile = new File("C:\\Documents and Settings\\rwitmer.NA\\Desktop\\eclipse\\workspace2\\Tree\\config.ini");
FileInputStream fis = new FileInputStream(iniFile);
Properties properties = new Properties();
properties.load(fis);
database = properties.getProperty("database");
user = properties.getProperty("user");
password = properties.getProperty("password");
System.out.println(database);
}

public ResultSet MakeConnection(String query) {
try {
ReadIni();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
}

try {
Statement stmt = null;
rs = null;
Connection conn = DriverManager.getConnection("jdbc:mysql:" + ReadIni().database + "?user=" + ReadIni().user + "&password" + ReadIni().password);

stmt = conn.createStatement();
rs = stmt.executeQuery(query);

} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return rs;
}

}
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
instead of using ReadIni().database, just use database and you should be okay,
remmember you cannot access a method with out a object unless method is static

Ashish
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic