Passing a Connection variable between 2 different classes.
Dear my friends... I have 3 classes in my Java program One is Menu class, Konek (Connection) Class and Control Class. I want to put my connection code into one class but I don't know how to make another class can use the same connection, such as under below in my code.... I tried to pass the "conn" from koneksi.konek() like this : conn=koneksi.konek() but the output data type doesn't give me any possibility because the datatype of "conn" is Connection whereas I only may put : String, int, void etc. Should I use ADT in this case? Thanks in advance... ==== import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; class menu{ public static char start() { char pilihmenu; System.out.println("Here is customer menu\n\n"); System.out.println("========================================="); System.out.println("| Main Menu |"); System.out.println("========================================="); System.out.println("| Options : |"); System.out.println("| 1. Customer |"); System.out.println("| 2. Product Field |"); System.out.println("========================================="); pilihmenu = Keyin.inChar("Please choose (1-2) : "); return pilihmenu; } public static void customer() { try { koneksi.konek(); // Do something with the Connection // assume conn is an already created JDBC connection Statement stmt = null; ResultSet rs = null; try { //stmt = conn.createStatement(); //here is the problem. How can I make the "conn" connection variable be usable by this class and this method? In order to make my program can execute the query furthermore. I hate to make so many times Connection String in every time I need it. I want only in one method of one class. System.out.println("Executing query.\n"); rs = stmt.executeQuery("SELECT customerid FROM customer"); .... .... } finally { // it is a good idea to release // resources in a finally{} block // in reverse-order of their creation // if they are no-longer needed System.out.println("Error in Query.\n"); if (rs != null) { try { rs.close(); } catch (SQLException sqlEx) { // ignore } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { // ignore } stmt = null; } } } } }catch (Exception e) { // handle any errors } } } class koneksi{ public static void konek(){ System.out.println("Building connection to database.\n"); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/amt?user=root&password=pandawa5"); System.out.println("Connection established.: "+ conn+"\n"); // Do something with the Connection // assume conn is an already created JDBC connection }catch (Exception e) { // handle any errors System.out.println("Connection failed.\n"); } } } public class comadmin { public static void main(String[] args){ char pilihmenu; pilihmenu = menu.start(); System.out.println("Your choice is : "+pilihmenu+"\n"); if (pilihmenu== '1'){ System.out.println("Opening customer menu\n"); menu.customer(); } } } __________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/
Hi, * Prabu Subroto <prabu_subroto@yahoo.com> [2003-12-10 17:07]:
I want to put my connection code into one class but I don't know how to make another class can use the same connection, such as under below in my code....
Some people say that Singletons are used too often, but this seems to be a classic application. If you don't know what a singleton is, you should read the book Design Patterns. Untested, just entered into the mail editor: public class Resource { protected static Resource myInstance; private Resource() { // Do some serious initialization here } public static Resource getInstance() { if (myInstance == null) { myInstance = new Resource(); } return myInstance; } } Now you can access the resource from anywhere using myResource = Resource.getInstance(); Thorsten -- This sentence no verb.
Dear Thorsten.... Ooo..Oooo... Sorry my friends... I am trapped again into trouble. I made one separated file named "koneksi.java". And did some modification in "comadmin.java". I got this error message: " patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:26: cannot resolve symbol symbol : variable conn location: class menu conn=koneksi.konek(); ^ comadmin.java:33: cannot resolve symbol symbol : variable conn location: class menu stmt = conn.createStatement(); ^ 2 errors patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> " ============== //This is my "koneksi.java". import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; public class koneksi{ protected static koneksi myInstance; private koneksi(){ // Do some serious initialization here try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/amt?user=root&password=pandawa5"); System.out.println("Connection established.: "+ conn+"\n"); // Do something with the Connection // assume conn is an already created JDBC connection }catch (Exception e) { // handle any errors System.out.println("Connection failed.\n"); } } public static koneksi konek(){ System.out.println("Building connection to database.\n"); if (myInstance == null) { myInstance = new koneksi(); } return myInstance; } } ============= //and this is my "comadmin.java". import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; class menu{ public static char start() { char pilihmenu; System.out.println("Here is customer menu\n\n"); System.out.println("========================================="); System.out.println("| Main Menu |"); System.out.println("========================================="); System.out.println("| Options : |"); System.out.println("| 1. Customer |"); System.out.println("| 2. Product Field |"); System.out.println("========================================="); pilihmenu = Keyin.inChar("Please choose (1-2) : "); return pilihmenu; } public static void customer() { try { conn=koneksi.konek(); // Do something with the Connection // assume conn is an already created JDBC connection Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); System.out.println("Executing query.\n"); rs = stmt.executeQuery("SELECT customerid FROM customer"); // or alternatively, if you don't know ahead of time that // the query will be a SELECT... if (stmt.execute("SELECT customerid FROM customer")) { rs = stmt.getResultSet(); int sCustomer; // Now do something with the ResultSet .... while(rs.next()) // for each row of data { sCustomer = rs.getInt("customerid"); // Report each Customer System.out.println("Customer : " + sCustomer + "\n"); } } } finally { // it is a good idea to release // resources in a finally{} block // in reverse-order of their creation // if they are no-longer needed System.out.println("Error in Query.\n"); if (rs != null) { try { rs.close(); } catch (SQLException sqlEx) { // ignore } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { // ignore } stmt = null; } } } } }catch (Exception e) { // handle any errors } } } public class comadmin { public static void main(String[] args){ char pilihmenu; pilihmenu = menu.start(); System.out.println("Your choice is : "+pilihmenu+"\n"); if (pilihmenu== '1'){ System.out.println("Opening customer menu\n"); menu.customer(); } } } =========== Please tell me Thorsten... where is my mistake? --- Thorsten Haude <linux@thorstenhau.de> wrote:
Hi,
* Prabu Subroto <prabu_subroto@yahoo.com> [2003-12-10 17:07]:
I want to put my connection code into one class but I don't know how to make another class can use the same connection, such as under below in my code....
Some people say that Singletons are used too often, but this seems to be a classic application. If you don't know what a singleton is, you should read the book Design Patterns.
Untested, just entered into the mail editor: public class Resource { protected static Resource myInstance;
private Resource() { // Do some serious initialization here }
public static Resource getInstance() { if (myInstance == null) { myInstance = new Resource(); } return myInstance; } }
Now you can access the resource from anywhere using myResource = Resource.getInstance();
Thorsten -- This sentence no verb.
ATTACHMENT part 2 application/pgp-signature
__________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/
Prabu Subroto wrote:
Dear Thorsten....
Ooo..Oooo... Sorry my friends... I am trapped again into trouble.
I made one separated file named "koneksi.java". And did some modification in "comadmin.java".
I got this error message: " patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:26: cannot resolve symbol symbol : variable conn location: class menu conn=koneksi.konek(); ^
<snip>
I don't see where you have declared the variable "conn" anywhere. e.g. Connection conn = null; .... conn=koneksi.konek();
I put this "Connection conn=null;" in the "comadmin.java" as below: ========= Connection conn=null; Statement stmt = null; ResultSet rs = null; conn=koneksi.konek(); // Do something with the Connection // assume conn is an already created JDBC connection try { ... ... ======= And I still got the same error, as below: ======= patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:30: incompatible types found : koneksi required: java.sql.Connection conn=koneksi.konek(); ^ 1 error patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> ======= What is my mistake? Please tell me. TIC. --- expatriate <lbox@nellgc.plus.com> wrote:
Prabu Subroto wrote:
Dear Thorsten....
Ooo..Oooo... Sorry my friends... I am trapped again into trouble.
I made one separated file named "koneksi.java". And did some modification in "comadmin.java".
I got this error message: " patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:26: cannot resolve symbol symbol : variable conn location: class menu conn=koneksi.konek(); ^
<snip>
I don't see where you have declared the variable "conn" anywhere. e.g. Connection conn = null; .... conn=koneksi.konek();
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
__________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/
Dear my friend, expatriate.... I still have the same error: ===== patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:30: incompatible types found : koneksi required: java.sql.Connection conn=koneksi.konek(); ^ 1 error patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> ===== Here is my current "koneksi.java": ===== import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; public class koneksi{ protected static koneksi myInstance; private koneksi(){ // Do some serious initialization here try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/amt?user=root&password=pandawa5"); System.out.println("Connection established.: "+ conn+"\n"); // Do something with the Connection // assume conn is an already created JDBC connection }catch (Exception e) { // handle any errors System.out.println("Connection failed.\n"); } } public static koneksi konek(){ System.out.println("Building connection to database.\n"); if (myInstance == null) { myInstance = new koneksi(); } return myInstance; } } ===== Here is my "comadmin.java" : ===== import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; class menu{ public static char start() { char pilihmenu; System.out.println("Here is customer menu\n\n"); System.out.println("========================================="); System.out.println("| Main Menu |"); System.out.println("========================================="); System.out.println("| Options : |"); System.out.println("| 1. Customer |"); System.out.println("| 2. Product Field |"); System.out.println("========================================="); pilihmenu = Keyin.inChar("Please choose (1-2) : "); return pilihmenu; } public static void customer() { try { Connection conn=null; Statement stmt = null; ResultSet rs = null; conn=koneksi.konek(); // Do something with the Connection // assume conn is an already created JDBC connection try { stmt = conn.createStatement(); System.out.println("Executing query.\n"); rs = stmt.executeQuery("SELECT customerid FROM customer"); // or alternatively, if you don't know ahead of time that // the query will be a SELECT... if (stmt.execute("SELECT customerid FROM customer")) { rs = stmt.getResultSet(); int sCustomer; // Now do something with the ResultSet .... while(rs.next()) // for each row of data { sCustomer = rs.getInt("customerid"); // Report each Customer System.out.println("Customer : " + sCustomer + "\n"); } } } finally { // it is a good idea to release // resources in a finally{} block // in reverse-order of their creation // if they are no-longer needed System.out.println("Error in Query.\n"); if (rs != null) { try { rs.close(); } catch (SQLException sqlEx) { // ignore } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { // ignore } stmt = null; } } } } }catch (Exception e) { // handle any errors } } } public class comadmin { public static void main(String[] args){ char pilihmenu; pilihmenu = menu.start(); System.out.println("Your choice is : "+pilihmenu+"\n"); if (pilihmenu== '1'){ System.out.println("Opening customer menu\n"); menu.customer(); } } } ===== Please, keep teaching me.... TIC --- expatriate <lbox@nellgc.plus.com> wrote:
Prabu Subroto wrote:
Dear Thorsten....
Ooo..Oooo... Sorry my friends... I am trapped again into trouble.
I made one separated file named "koneksi.java". And did some modification in "comadmin.java".
I got this error message: " patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:26: cannot resolve symbol symbol : variable conn location: class menu conn=koneksi.konek(); ^
<snip>
I don't see where you have declared the variable "conn" anywhere. e.g. Connection conn = null; .... conn=koneksi.konek();
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
__________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/
Prabu Subroto wrote:
Dear my friend, expatriate....
I still have the same error: ===== patrixlinux@patrix:~/arsip/My/comadmin/dimodulkan> javac comadmin.java comadmin.java:30: incompatible types found : koneksi required: java.sql.Connection conn=koneksi.konek();
<snip>
OK. It seems that you should declare conn to be a variable of type koneksi since the konek() method returns a reference to a koneksi object.
On Thu, Dec 11, 2003 at 06:03:11AM -0800, Prabu Subroto wrote:
Dear Thorsten....
Ooo..Oooo... Sorry my friends... I am trapped again into trouble.
I made one separated file named "koneksi.java". And did some modification in "comadmin.java".
I got this error message: " You are declaring conn as a local variable in the constructor of the koneksi class. Conn is garbage collected as soon as the constructor finishes executing, because you no longer have any references to it. You need to save a reference to conn somewhere where the Menu class can access it. Others have already given you possible solutions so I won't repeat them here.
Victor
Hi, Please avoid tofu mails. http://www.vranx.de/mail/tofu.html * Prabu Subroto <prabu_subroto@yahoo.com> [2003-12-11 15:03]:
Ooo..Oooo... Sorry my friends... I am trapped again into trouble.
I made one separated file named "koneksi.java". And did some modification in "comadmin.java".
Sorry, but I have a hard time reading your source. Could you reformat to tabwidth 4 and lines < 80 chars? Thorsten -- If I have seen further, it is by standing on the shoulders of giants. - Sir Isaac Newton
participants (4)
-
expatriate
-
Prabu Subroto
-
Thorsten Haude
-
Victor R. Cardona