Añado los fuentes de java
This commit is contained in:
+243
@@ -0,0 +1,243 @@
|
||||
package com.tarisan.chipcard.tpvvs.webservicetrayicon.accesodatos;
|
||||
|
||||
import java.sql.*;
|
||||
import es.chipcard.util.*;
|
||||
|
||||
public class LocalDatabaseControl {
|
||||
private Connection objCon=null;
|
||||
private static LocalDatabaseControl I=null;
|
||||
|
||||
public static LocalDatabaseControl getInstance(){
|
||||
if(I==null){
|
||||
I=new LocalDatabaseControl();
|
||||
I.init();
|
||||
}
|
||||
return I;
|
||||
}
|
||||
|
||||
private void init(){
|
||||
try{
|
||||
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
|
||||
}catch(Exception err){
|
||||
SerClsDebug.aLog(err);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection(){
|
||||
if(objCon==null){
|
||||
try{
|
||||
String protocol = "jdbc:derby:";
|
||||
java.util.Properties props = new java.util.Properties();
|
||||
props.put("user","admin");
|
||||
props.put("password","admin");
|
||||
objCon = DriverManager.getConnection(protocol + getDBName()+";create=true",props);
|
||||
}catch(Exception err){
|
||||
SerClsDebug.aLog(err.getMessage());
|
||||
}
|
||||
}
|
||||
return objCon;
|
||||
}
|
||||
|
||||
private static String getDBName(){
|
||||
String sRetorno="localDB";
|
||||
return sRetorno;
|
||||
}
|
||||
|
||||
public static void shutdown(){
|
||||
try{
|
||||
forceClose(getInstance().objCon);
|
||||
//DriverManager.getConnection("jdbc:derby:"+getDBName()+";shutdown=true");
|
||||
}catch(Throwable th){
|
||||
SerClsDebug.aLog("Error shutting down Local Database:"+th.getMessage());
|
||||
//th.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void forceClose(Statement stmt){
|
||||
try{
|
||||
stmt.close();
|
||||
}catch(Exception e){}
|
||||
}
|
||||
public static void forceClose(ResultSet rs){
|
||||
try{
|
||||
rs.close();
|
||||
}catch(Exception e){}
|
||||
}
|
||||
|
||||
private static void forceClose(Connection con){
|
||||
try{
|
||||
if(con!=null)
|
||||
con.close();
|
||||
}catch(Exception e){}
|
||||
}
|
||||
/**
|
||||
* Metodo para INSERT, UPDATE y DELETE
|
||||
* Devuelve el numero de filas afectadas o -1 si hubo error
|
||||
* @param strUpdate
|
||||
* @return
|
||||
*/
|
||||
public int executeUpdate(String strUpdate){
|
||||
Connection con=getConnection();
|
||||
int intRetorno=executeUpdate(strUpdate, con);
|
||||
//forceClose(con);
|
||||
return intRetorno;
|
||||
}
|
||||
|
||||
public int executeUpdate(String strUpdate,Connection con){
|
||||
int intRetorno=executeUpdate(strUpdate,null,con);
|
||||
return intRetorno;
|
||||
}
|
||||
|
||||
public int executeUpdate(String strUpdate,java.io.StringWriter s){
|
||||
Connection con=null;
|
||||
return executeUpdate(strUpdate,s, con);
|
||||
}
|
||||
|
||||
public int executeUpdate(String strUpdate,java.io.StringWriter s,Connection con){
|
||||
int intRetorno=-1;
|
||||
//Connection con=null;
|
||||
Statement stmt=null;
|
||||
try{
|
||||
if(con==null)
|
||||
con=getConnection();
|
||||
if(con!=null){
|
||||
stmt=con.createStatement();
|
||||
SerClsDebug.aLog("LocalDatabaseUpdate:"+strUpdate);
|
||||
intRetorno=stmt.executeUpdate(strUpdate);
|
||||
}
|
||||
}catch(Exception e){
|
||||
if(s==null){
|
||||
e.printStackTrace();
|
||||
}else{
|
||||
java.io.PrintWriter p=new java.io.PrintWriter(s);
|
||||
p.println("Error al ejecutar: "+strUpdate);
|
||||
e.printStackTrace(p);
|
||||
}
|
||||
}finally{
|
||||
forceClose(stmt);
|
||||
}
|
||||
return intRetorno;
|
||||
}
|
||||
|
||||
|
||||
public boolean executeProcedure(String strUpdate){
|
||||
boolean blnRetorno=executeProcedure(strUpdate, null);
|
||||
return blnRetorno;
|
||||
}
|
||||
|
||||
public boolean executeProcedure(String strCall,java.io.StringWriter s){
|
||||
boolean blnRetorno=false;
|
||||
CallableStatement cs=null;
|
||||
ResultSet rs=null;
|
||||
Connection con=getConnection();
|
||||
boolean blnKeepConnectionOpened=false;
|
||||
try{
|
||||
cs = con.prepareCall(strCall);
|
||||
cs.execute();
|
||||
cs.close();
|
||||
blnRetorno = true;
|
||||
}catch(Exception e){
|
||||
if(s==null){
|
||||
e.printStackTrace();
|
||||
}else{
|
||||
java.io.PrintWriter p=new java.io.PrintWriter(s);
|
||||
p.println("Error al ejecutar: " + strCall);
|
||||
e.printStackTrace(p);
|
||||
}
|
||||
}finally{
|
||||
forceClose(rs);
|
||||
forceClose(cs);
|
||||
}
|
||||
return blnRetorno;
|
||||
}
|
||||
|
||||
/**
|
||||
* Realiza una select y devuelve el resultado como un arraylist
|
||||
* de contenido un String[] por fila
|
||||
* */
|
||||
public ResultSet getQuery(String strQuery){
|
||||
Statement stmt=null;
|
||||
ResultSet rs=null;
|
||||
try{
|
||||
SerClsDebug.aLog("LocalDatabaseQuery:"+strQuery);
|
||||
stmt=getConnection().createStatement();
|
||||
rs=stmt.executeQuery(strQuery);
|
||||
}catch(Exception e){
|
||||
SerClsDebug.aLog(e);
|
||||
}finally{
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
|
||||
public String[] getFieldsOfTable(String strTablename,Connection con){
|
||||
String arrFields[]=null;
|
||||
Statement stmt=null;
|
||||
ResultSet rs=null;
|
||||
String strQuery=null;
|
||||
if(strTablename.toUpperCase().startsWith("SELECT ")){
|
||||
strQuery=strTablename;
|
||||
SerClsDebug.aLog("LocaDatabaseQuery:"+strQuery);
|
||||
}else{
|
||||
strQuery="SELECT * FROM "+strTablename+" WHERE 1<>0";
|
||||
}
|
||||
try{
|
||||
if(con==null)
|
||||
con=getConnection();
|
||||
stmt=con.createStatement();
|
||||
rs=stmt.executeQuery(strQuery);
|
||||
arrFields=getColumnNames(rs);
|
||||
}catch(Exception e){
|
||||
SerClsDebug.aLog(e);
|
||||
}finally{
|
||||
forceClose(rs);
|
||||
forceClose(stmt);
|
||||
}
|
||||
return arrFields;
|
||||
}
|
||||
|
||||
public String[] getColumnNames(ResultSet rs){
|
||||
String arrNombresColumnas[]=null;
|
||||
try {
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int nColumns = rsmd.getColumnCount();
|
||||
System.out.println("Hay "+nColumns+" columnas");
|
||||
//HashMap h = null;
|
||||
String arrFila[]=null;
|
||||
arrNombresColumnas=new String[nColumns];
|
||||
try{
|
||||
//Se rellena la primera fila con los nombres de columnas
|
||||
for(int column = 1; column<(nColumns+1); column++) {
|
||||
int intColumnType = rsmd.getColumnType(column);
|
||||
String strColumnName = rsmd.getColumnName(column);
|
||||
arrNombresColumnas[column-1]=strColumnName;
|
||||
System.out.println("Columna["+column+"]="+arrNombresColumnas[column-1]);
|
||||
}
|
||||
}catch(Exception e){}
|
||||
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return arrNombresColumnas;
|
||||
}
|
||||
|
||||
public String getMetaData(Connection con){
|
||||
StringBuffer buff=new StringBuffer();
|
||||
try{
|
||||
buff.append("<ProductName>"+con.getMetaData().getDatabaseProductName()+"</ProductName>");
|
||||
buff.append("<getMaxColumnsInSelect>"+con.getMetaData().getMaxColumnsInSelect()+"</getMaxColumnsInSelect>");
|
||||
buff.append("<getMaxConnections>"+con.getMetaData().getMaxConnections()+"</getMaxConnections>");
|
||||
buff.append("<getURL>"+con.getMetaData().getURL()+"</getURL>");
|
||||
ResultSet rs=con.createStatement().executeQuery("select TNAME from \"DB2eSYSTABLES\"");
|
||||
while(rs.next()){
|
||||
buff.append("\n<TableName>"+rs.getString("TNAME")+"</TableName>");
|
||||
}
|
||||
//buff.append("<allTablesAreSelectable>"+con.getMetaData().allTablesAreSelectable()+"</allTablesAreSelectable>");
|
||||
}catch(Exception e){
|
||||
buff.append(e.toString());
|
||||
}
|
||||
return buff.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user