how to import own package in the java file?

Discussions

General J2EE: how to import own package in the java file?

  1. how to import own package in the java file? (5 messages)

    I created a folder priya. Under that I created 2 folders connections and procedures.

    Both folders have one java file.

    The connection folder has a Connectionfactory.java file that gets the database connection and the procedure folder has a CreateSetPrice.java file that creates a stored procedure from database. The procedure sets a price.

    The connection for database has to be taken from the ConnectionFactory.java in the other folder.

    I set the classpath as set classpath = c:\priya\connections;c:\priya\procedures;

    The ConnectionFactory file compiles.

    The CreateSetPrice.java file has:

    package procedures;
    import java.sql.*;
    import java.util.*;
    import connections.*;

    I copied both file codes from a book and the code is okay and compiles. The CreateSetPrice.java file however does not recognise the ConnectionFactory at all??

    Error is: "package connections does not exist" and other error are related to it like "cannot resolve symbile Connectionfactory".

    I think I set the classpath but I dont know what else to do ? Please help.

    ---------------------

    Pasting the two files below for reference:
    package procedures;

    import java.sql.*;
    import java.util.*;
    import connections.*;

    public class CreateSetPrice
    {
    public static void main(String args[])
    {
    Connection conn = null;
    Statement stmt = null;

    try
    {
    conn = ConnectionFactory.getConnection();
    stmt = conn.createStatement();

    String sql = "create or replace procedure set_price ("+
    "+id in number,price in number)" + "as begin "+
    "update recordings set listprice = price "+
    "where recordingid = id; end;";

    int result = stmt.executeUpdate(sql);
    // result should return 0

    System.out.println("Procedure created sucessfully");
    }//try
    catch(Exception e)
    {
    System.out.println("Procedure not created");
    }
    finally
    {
    ConnectionFactory.close(stmt);
    ConnectionFactory.close(conn);
    }
    }//main
    }

    package connections;

    import java.sql.*;


    public class ConnectionFactory
    {
    private static ConnectionFactory ref = new ConnectionFactory();

    //Constructor
    private ConnectionFactory()
    {
    try
    {
    Class.forName("oracle.jdbc.odbc.OracleDriver");
    }
    catch(ClassNotFoundException e)
    {
    System.out.println("Error: exception loading drivers");
    }
    }


    public static Connection getConnection() throws SQLException
    {
    String url = "jdbc:oracle:thin:@wcsuds1.ctstateu.edu:1521:wprac";
    return DriverManager.getConnection(url,"sys","featherstone");
    }

    public static void close(ResultSet rs)
    {
    try
    {
    rs.close();
    }
    catch(Exception e)
    {
    System.out.println("Exception ignored");
    }
    }


    public static void close(Statement stmt)
    {
    try
    {
    stmt.close();
    }
    catch(Exception e)
    {
    System.out.println("Exception ignored");
    }
    }


    public static void close(Connection conn)
    {
    try
    {
    conn.close();
    }
    catch(Exception e)
    {
    System.out.println("Exception ignored");
    }
    }

    }
  2. Try

    import priya.connections.*;
  3. try taking the package names out of the classpath...

    classpath = c:\priya

  4. try taking the package names out of the classpath...

    classpath = c:\priya

  5. just put classpath to c:\priya
    understand the basic here that u your class path has to point til the package and not include any of the folder names in ur package.

       for a class myclass in package x.y.z with x, y,z being folders under C drive like c:\x\y\z\myclass.class. Then ur class path shd just have c and not c:\x\y\z.
       I hope i didnt confuse u :)

  6. also dont put ur sensitive code on a public site, specially ur database id and password ..