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
Hi,
* Prabu Subroto
[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/