File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Error in creating Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Error in creating Instance" Watch "Error in creating Instance" New topic
Author

Error in creating Instance

Nilesh Joshi
Greenhorn

Joined: Nov 10, 2004
Posts: 4
hi i have following program Which connects to database

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

public class Database
{
public Connection Conn;
Statement st;

public Database()
{
try
{
String url = "jdbc ostgresql://192.168.0.124:5432/test";
DriverManager.registerDriver(new org.postgresql.Driver());
Conn = DriverManager.getConnection(url,"postgres","");
}
catch (SQLException ex )
{
System.out.println("Exception in Database.Database moblog " + ex );
}
catch (Exception genex)
{
System.out.println("Exception in Database " + genex );
}
}

public Connection getConn()
{
return Conn;
}

public ResultSet Select(String SQLStatement)
{
ResultSet result=null;
try {
st=(Statement) getConn().createStatement();
result=(ResultSet) st.executeQuery(SQLStatement);
if (result.isBeforeFirst()==false)
return null;
} catch (SQLException ex) {
return null;
}
return result;
}

public boolean Execute(String SQLStatement) {
try {
st=(Statement) getConn().createStatement();
if(st.executeUpdate(SQLStatement) >=0)
return true;
st.close();
} catch (SQLException ex) {
return false;
}
return true;
}

public void CloseConn()
{
try
{
Conn.close();
}
catch (SQLException ex ){ }
}

public static void main(String args[])
{
try
{
Database db=new Database();
Statement st=(db.getConn()).createStatement();
ResultSet result=st.executeQuery("select * from account limit 10");
while(result.next()) {
System.out.println("values "+result.getString(1)+"="+result.getString(2));
}
if(result!=null)
result.close();
if(db!=null)
db.CloseConn();
}
catch(SQLException e)
{
System.out.println(e);
}
}
}

but when i create instance of Database in following program i get compilation error saying cannot resolve symbol

import java.sql.*;

import java.lang.*;



public class TestInstance

{

public String retData(int count)

{

String password = null;

try

{

System.out.println("Before Instantiation ");
Database db = new Database();

Passwd psw = new Passwd();

password = psw.generatePasswd(count);

System.out.println("After Instantiation ");

}catch(Exception ex){

System.out.println("Exception in Database");

}

return password;

}



public static void main(String[] args)

{

Passwd psw = new Passwd();

String pass = psw.generatePasswd(8);

System.out.println("Hello ur passwd is "+pass);

TestInstance ti = new TestInstance();

String s = ti.retData(8);

System.out.println("Hello ur passwd is "+s);

}

}


and when i compile this code on other machine it works fine.

what could be the solution for this please help me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

In a word: CLASSPATH. You don't tell us which symbol the compiler can't resolve, but I'd suspect it was this: org.postgresql.Driver. You need to make sure that your Driver class is in the CLASSPATH.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nilesh Joshi
Greenhorn

Joined: Nov 10, 2004
Posts: 4
hi n thanx for instant reply, i m not getting any problem in Database file where i actually make a connection with database, the problem is in other program when i try to make an instance of Database file.

i get error at

Database db = new Database();
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sorry, are you saying that the code which complains it "cannot resolve symbol" is not the code you posted!?

Can you show use the code and the exception stack trace?
Shekar Atmakur
Ranch Hand

Joined: Oct 24, 2003
Posts: 36
That may be because, The DataBase class is not visible in the calling class. Try including the DataBase class in the imports section of the calling class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in creating Instance