Closing Database Connection
I have an all purpose Database Class. There are two methods in this
class: DBConnect() and DBClose().
(DBConnect)----------------------------------------------------------
package Program;
import java.sql.*;
public class Database{
public void DBConnect(){
Connection conn = null;
try
{
String userName = "root";
String password = "passwor";
String url = "jdbc:mysql://localhost/program";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName,
password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database
server");
}
}
public void DBClose(String conn){
try
{
conn.close ();
System.out.println ("Database connection
terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
--------------------------------------------------------------------------------------------------------
I can connect to the database just fine. My question, is that when
calling the DBClose() method, how do I pass along the connection I
created when I called DBConnect()? I have a sample program setup that
opens the database connection and then tries to close it.
(DBClose)---------------------------------------------------------------
package Program;
public class Processing {
public void Login(String Username, String Password){
Database dbconnection = new Database();
dbconnection.DBConnect();
//Database operations go here
dbconnection.DBClose(conn);
}
}
---------------------------------------------------------------------------------------