• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiling with packages

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class (in a package) to open and close database connections:

package com.rexam.sem.db.semDB;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.lang.ClassNotFoundException;

public class SEMConnector {

public Connection getConnection() {
Connection conn = null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String connUrl = "jdbc dbc:sem_1.0";
conn = DriverManager.getConnection(connUrl, "", "");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Class Not Found Exception:");
System.out.println(cnfe);
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}

return conn;
}

public void closeConnection(Connection c) {
try
{
c.close();
System.out.println("Connection closed successfully");
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}
}

}



I have complied this class from the command line without any problem:
javac SEMConnector.java

I also have a second class (in the same package) where I have a simple query:

package com.rexam.sem.db.semDB;

import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

public class SEMQuery {

public ResultSet execute () {
ResultSet results = null;

SEMConnector semCon = new SEMConnector();
Connection conn = semCon.getConnection();

try
{
Statement dbStatement = conn.createStatement();
results = dbStatement.executeQuery("select * from semServer");

while (results.next())
{
System.out.println ("uID : " + results.getInt("uID"));
}

semCon.closeConnection(conn);
}
catch(SQLException sqle)
{
System.out.println("SQL Exception:");
System.out.println(sqle);
}

return results;
}
}



I have tried to compile this class from the command line:
javac SEMQuery.java

...but the compiler complains that it cannot resolve symbol: SEMConnector

I have noticed that if I comment out the package statement from both of my classes, I can get them both to compile just fine.

I really need to compile these classes in the packaage though so I'd be very grateful if someone could point out the mistake I'm making.

Thanks!
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,

When compiling the second class, you need to make sure that the classpath of SEMConnector.class is visible. If the SEMConnector.class is in the same directory as the source, try this at the command line:

set classpath=%classpath%;.
javac *.java

the "." tells the compiler that everything in the current directory is in the classpath. If the SEMConnector.class is in a different directory, put that directory in the classpath, something like so:

set classpath=%classpath%;C:\ConnectorCode\classes
javac *.java

Julia
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, make sure that you put SEMConnector.class in a directory that matches your package 'com.rexam.sem.db.semDB', ie... \com\rexam\sem\db\semDB. That's basically what a package does, it specifies the directory where the class can be found.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The CLASSPATH needs to contain the directory that contains the com directory. For information on CLASSPATH, you should visit this page in our FAQ.

HTH

Layne
 
Dan Mortimer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Thanks very much for your help. Based on your advice, I managed to get both classes to compile using set classpath=%classpath%;. at the command line.

Before compiling the classes though, I checked my classpath and found that it was set to the following:

.;C:\SQLServerDriver\lib\msbase.jar;C:\SQLServerDriver\lib\mssqlserver.jar;C:\SQLServerDriver\lib\msutil.jar

Notice the ".;" at the beginning...since both my classes are in the same directory (and correct package structure) I shouldn't have had a problem with compilation in the first place, should I...?

If you have any thoughts on this I'd be glad to hear them, becuase although both my classes compile now, I'm still scratching my head about why the ".;" entry in my classpath seemed not to have any effect.

Thanks again for your help,



Dan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic