aspose file tools*
The moose likes JDBC and the fly likes How to use the connection object defined in another class from the Main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to use the connection object defined in another class from the Main class " Watch "How to use the connection object defined in another class from the Main class " New topic
Author

How to use the connection object defined in another class from the Main class

Saptarshi Chaudhuri
Greenhorn

Joined: Apr 12, 2009
Posts: 6
I am trying to execute the query from my Main class by calling the connection object from another class named "Conection".But I have been getting the following error.I have included the source code and also the errors.Please advise:


//This is the main class:
package esperimentbase;
import java.sql.*;

/**
*
* @author Saptarshi
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args)throws ClassNotFoundException,SQLException
{
Conection conn1=new Conection();



Statement stmt=conn1.createStatement();

ResultSet rset=stmt.executeQuery("select 'Hello thin driver tester'||USER||'!' result from dual")

while(rset.next())
{
System.out.println(rset.getString(1));
}
}

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Tjis is the conection class from which I am connecting JDBC to Oracle
package esperimentbase;
import java.sql.*;

/**
*
* @author Saptarshi
*/
public class Conection
{
public Connection conn;
public Conection()throws ClassNotFoundException,SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");

System.out.println("OK :/");

Connection conn=DriverManager.getConnection("jdbcracle:thin:@127.0.0.1:1521:XE","System","orion");
}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The error that I am getting is


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
found : esperimentbase.Conection.createStatement
required: java.sql.Statement
at esperimentbase.Main.main(Main.java:24)


PLEASE ADVISE




Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Saptarishi,

Please use code tags as more code posted , it would be unreadable and ugly.


Whats this ? conn1 is object of Conection ( confusing name - change it pleaase) and has no method createStatement() ( which is actually a method in java.sql.Connection).

Since you are creating the connection in Conection(your class) constructor itself , i think its not neccessary to call createStatement().

Please correct those errors and come back.thanks.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
hi,
Saptarshi

I have modified your code a little bit and its working. If it is that what you needed then its ok. As Balu said please avoid confusing names.

code for main class



code for Conection class

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


@Adeeb

I have modified your code a little bit and its working.


Is it compiling at first hand ? I wonder how this is working without getting Statement object initialized from Connection. No code where the Main class calls the other class.
Saptarshi Chaudhuri
Greenhorn

Joined: Apr 12, 2009
Posts: 6
First of all as per your advice I have changed the confusing names.So here is my earlier program with the connection made in "Myclass" and I want to execute the query from the "Main" class



@Balu,you said that since I am defining the Connection in my class constructor so I need not require to call the "createStatement()" method(I have put that line under comments-line22) then how will the statement object be created??(All I know we need the createStatement() method to create a statement object).And dont I need it to get it initialised from Myclass as you said adeeb before??Please tell me.

And @Adeeb,the edited code doesnt ctually execute the Query that I want it to execute.The only output I get is ,where as the model output that I want is

The reason may be because the Main class is not calling Myclass.
In short what I want is to execute queries from the main class while defining the connection in another class.If not tell me just give me the hints atleat as to how will the Main class be able to know about the connection and execute the queries,because I seem to be doing it all wrong!!

I dont know if it is worthwhile to menton it or not,but for the last three years I(CSE second year student,BTech) have actually been a simple Assembly Language and C guy with little knowledge of C++.Recently I was assigned a database project and that is why I am making this painful transition from C to JAVA.Normally I would have taken my time and learned JAVA nicley going with my own pace,but this project wont let me do that.I dont know why I am saying this to you guys bt maybe sharing my problem will light me up a bit.Thanks.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Saptarishi,

Follow these steps

1) Create a class ( for serving connection) and declare static Connection variable.
2) Declare a static method getConnection() , here in this do the stuff (get Connection from Driver manager)
3) Now in the main class , call MyClass.getConnection() to get the connection and store it in local Connection object (say conn). Use conn.createStatement to Obain Statement object and executeQuery as you did the before.

Saptarshi Chaudhuri
Greenhorn

Joined: Apr 12, 2009
Posts: 6
Thank you very much Balu..I thank you from the bottom of my heart.Anyways,I am including the code to ensure that this is what you asked me to do...also any advise for improvements is welcomed.





Thank you again..this small step has actually solved a number of bigger problems regarding the project.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



You are welcome.

Important thing to note is to close the resources that were opened, otherwise it would be produce unexpected results.

1) close the connection , statement and resultsets. This is usually done at the finally clause to ensure its done even during exceptions.


2)
Ok declare a connectionClose() method in the Connectionbase class. which simply checks



next thing may be to use existing connection rather than create one if its already



Can be added a condition to see if conn is null , otherwise return conn ( note when you close the connection , you assign conn= null)



3) The names used are still no to the standards. check out this Coding standards

4)


Throws ? if this the main class it's not good to throw exception. handle the it in catch() , report the exception and do cleannups.
Saptarshi Chaudhuri
Greenhorn

Joined: Apr 12, 2009
Posts: 6
Fantastic!!that was super informative.I have already implemented the technique for checking a pre-existing connection and also closing the connection.
And regarding the coding standards, Java programmers are something!! arent they??...not in my life had I thought about taking care of what names I use to write my programs and all......but now you have inspired me to do so....it calls for a change....I'll surely incorporate these things..just lemme finish this project for once ...bye
 
jQuery in Action, 2nd edition
 
subject: How to use the connection object defined in another class from the Main class