public class RockPosDBManager
{
private boolean debugEnable;
private int engine;
private int typeOfConn;
private String Embeddeddriver
= "org.apache.derby.jdbc.EmbeddedDriver"; private String Clientdriver
= "org.apache.derby.jdbc.ClientDriver"; private String mySQLClientDriver
= "com.mysql.jdbc.Driver";
private String DerbyServer
= "jdbc:derby://localhost:1527/"; private String DerbyNoServer
= "jdbc:derby:/"; private String MySQLServer
= "jdbc:mysql://localhost:3306/";
private String finalConnectionString
; private boolean Authentication;
private boolean create;
private boolean isConnected;
/**
* @function Print
* @brief this function Print Information on terminal console if the Debug Flag is enable.
* @param String - this String is the value that will be print onto the screen
* */
private void Print
(String templ
){ if( debugEnable == false) return;
if( templ != null )
{
//System.out.printf("DEBUG : %s\n", templ );
}
}
/**
* @function Print
* @brief this constructor will set the current Engine, kind of connection, we have avaliable Derby engine and Mysql .
* @param int sqlEngine =type of Engine rockPosClient = server, embedded or Client
* @see RockPosDBEngine & RockPosTypeConn
* */
public RockPosDBManager(int sqlEngine , int rockposClient, boolean created ){
this.engine = sqlEngine;
this.typeOfConn = rockposClient;
this.debugEnable = false;
this.isConnected = false;
this.create = created;
}
/**
* @function setDebugVerbose
* @brief this function will turn on the Debug Flag for Terminal.
* @param none
* @see
* */
public void setDebugVerbose(boolean e){
debugEnable=e;
}
/**
* @function isConnectedIntoDatabase
* @brief this Function Verify if the databe is connected;
* @param none
* @see
* */
public boolean isConnectedIntoDatabase(){
return this.isConnected;
}
/**
* @function SetDBNameConn
* @brief
* @param
* @see
* */
public void SetDBNameConn
(String DBname
){ try{
if(DBname == null)
this.DBName = DBname;
Print("DB name is : "+ DBName);
switch(engine){
case RockPosDBEngine.SQL_DERBY:
Print("ENGINE SQL_DERBY");
switch(typeOfConn)
{
case RockPosTypeConn.ROCKPOS_CLIENT:
Print("Type Of Connection ROCKPOS_CLIENT");
finalConnectionString = DerbyServer + this.DBName;
finalDriver = Clientdriver;
break;
case RockPosTypeConn.ROCKPOS_EMBEDDED:
Print("Type Of Connection ROCKPOS_EMBEDDED");
finalConnectionString = DerbyNoServer + this.DBName;
finalDriver = Embeddeddriver ;
break;
case RockPosTypeConn.ROCKPOS_SERVER:
Print("Type Of Connection ROCKPOS_SERVER");
finalConnectionString = DerbyServer + this.DBName;
finalDriver = Clientdriver;
break;
}
break;
case RockPosDBEngine.SQL_MYSQL:
Print("ENGINE SQL_MYSQL");
switch(typeOfConn)
{
case RockPosTypeConn.ROCKPOS_CLIENT:
/// Print("Type Of Connection ROCKPOS_CLIENT");
finalConnectionString = MySQLServer + this.DBName;
finalDriver = mySQLClientDriver;
break;
case RockPosTypeConn.ROCKPOS_EMBEDDED:
//// Print("Type Of Connection ROCKPOS_EMBEDDED");
Print("MySQL does not work in Embedded Mode yet.");
finalDriver = mySQLClientDriver;
break;
case RockPosTypeConn.ROCKPOS_SERVER:
//// Print("Type Of Connection ROCKPOS_SERVER");
finalConnectionString = MySQLServer + this.DBName;
finalDriver = mySQLClientDriver;
break;
}
break;
}
Print("Connection Driver is : "+ finalDriver);
Print("Connection String is ===> "+ finalConnectionString);
if(this.UserName != null && this.password != null)
{
Print("Authentication is On.");
Print("User name is =[ "+this.UserName+ " ] password = [ "+ this.password +"]");
Authentication = true;
}
else
{
Print("Authentication is On.");
Print("No user name was provide.");
Authentication = false;
}
{
e.printStackTrace();
}
}
/**
* @function SetCredential
* @brief
* @param
* @see
* */
if(user != null)
this.UserName = user;
if(pass != null)
this.password = pass;
}
public void RequireAuth(boolean auth){
this.Authentication = auth;
}
/**
* @function Connect
* @brief
* @param
* @see
* */
public boolean Connect(){
this.connection = null;
try{
Class.forName(finalDriver);
}
{
e.printStackTrace();
this.isConnected = false;
return this.isConnected ;
}
try{
if(create == false)
{
param = finalConnectionString +";user="+ this.UserName+";password="+ this.password;
}
else
{
param = finalConnectionString +";create=true;user="+this.UserName+";password="+ this.password;
}
System.
out.
printf("Param to Server : %s", param
); if(this.connection.isValid(30)){
this.isConnected = true;
System.
out.
print("\nEsta Conectado."); }
else{
this.isConnected = false;
System.
out.
print("\nno Esta Conectado.");
}
}
{
int code = e.getErrorCode();
e.printStackTrace();
this.isConnected = false;
return this.isConnected;
}
return this.isConnected;
}
public boolean Disconnect(){
try {
this.connection.close();
e.printStackTrace();
return isConnected;
}
this.isConnected = false;
return isConnected;
}
public boolean Execute
(String sql
){ ///Print("Execute");
boolean res = true;
if(this.isConnected == false )
{
Print("theres is not Connection");
return false;
}
try
{
stmt = this.connection.prepareStatement(sql);
stmt.executeUpdate(sql);
e.printStackTrace();
Print("Error Executing that Query");
return false;
}
finally{
///Print("Execute Finally");
try {
stmt.close();
Print("Error closing Statement.");
}
}
return res;
}
if(this.isConnected == false )
{
Print("theres is not Connection");
return null;
}
try
{
this.connection.setAutoCommit(true);
stmt = this.connection.createStatement();
result = stmt.executeQuery(sql);
if(result == null)
Print("Result is null");
Print("Error Executing that Query");
}
try {
stmt.close();
Print("Error closing Statement.");
}
return result;
}
public void CloseConn(){
try {
this.connection.close();
Print("Closing Connection");
Print("Error Closing Conection.");
}
this.isConnected= false;
}
}