Principal
Pesquisa na Web
Google
Data/Hora
Entrar
Siga-me no Tweeter
RSS Feed

Dicas sobre java, pacotes e afins
Java : Singleton
em 17/9/2008 12:40:00 (68 leituras)





 public class SingletonObject

{

  private SingletonObject()

  {

    // no code req'd

  }



  public static SingletonObject getSingletonObject()

  {

    if (ref == null)

        // it's ok, we can call this constructor

        ref = new SingletonObject();        

    return ref;

  }



  public Object clone()

    throws CloneNotSupportedException

  {

    throw new CloneNotSupportedException();

    // that'll teach 'em

  }



  private static SingletonObject ref;

}

Rating: 0.00 (0 votes) - Rate this News - Comments?
Java : Conexão / Connection MySql
em 23/3/2008 8:50:41 (60 leituras)
Java

//Source file: D:\\PROJETOS\\CF\\ControleFrequencia\\Conexao.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class Conexao {

//Faz a ponte entre o Jdbc e Odbc
private String fDBLoc = "jdbc:mysql://localhost:3306/mysql";
private String fDBDriver = "org.gjt.mm.mysql.Driver";
private ResultSet fRS = null;
public Connection fconn = null;
private PreparedStatement pstmt;

// Método dBConnect -- deve ser executado para fazer a conexao com o BD
public String conectar() {
String retVal = "";
try {
Class.forName(fDBDriver);
fconn = DriverManager.getConnection(fDBLoc, "", "");
} catch (ClassNotFoundException e) {
retVal = e.toString();
} catch (SQLException e) {
retVal = e.toString();
}
return (retVal);
}

public Connection getConn(){
return fconn;
}

public String desconectar() {
String retVal = "";
try {
fconn.close();
} catch (SQLException e) {
retVal = e.toString();
}
return retVal;
}

// Método executeQuery -- Executa consultas e retorna os dados
public ResultSet consulta(String stmt) {
if (fconn == null)
fRS = null;
else {
try {
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
} catch (SQLException e) {
}
}
return (fRS);
}

public int codigoDoRegistroEsp(String campo, String table) {
if (fconn == null) {
fRS = null;
return 0;
} else {
try {
pstmt = fconn.prepareStatement("Select max(" + campo
+ ") from " + table);
pstmt.execute();
fRS = pstmt.getResultSet();
fRS.next();
int codigo = fRS.getInt(1);
codigo++;
return codigo;// retorno do valor do codigo
} catch (Exception e) {
System.err.println("Erro no método codigoDoRegistro");
System.err.println(e);
}
}
return 0;
}

// select exclusivo
public ResultSet selecionarRegistroEsp(String stmt) {
if (fconn == null)
fRS = null;
else {
try {
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
System.out.println(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
return (fRS);
}

public ResultSet selecionarRegistro(String tabela) {
String stmt;
if (fconn == null)
fRS = null;
else {
try {
stmt = "Select * from " + tabela;
System.out.println(stmt);
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
return (fRS);
}

public ResultSet selecionarRegistro(String tabela, String condicao) {
String stmt;
if (fconn == null)
fRS = null;
else {
try {
stmt = "Select * from " + tabela + " Where " + condicao;
System.out.println(stmt);
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
System.out.println(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
return (fRS);
}

public ResultSet alterarRegistroEsp(String stmt) {
if (fconn == null)
fRS = null;
else {
try {
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
System.out.println(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
return (fRS);
}

public ResultSet excluirRegistroEsp(String stmt) {
if (fconn == null)
fRS = null;
else {
try {
Statement s = fconn.createStatement();
fRS = s.executeQuery(stmt);
System.out.println(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
return (fRS);
}

public int excluirRegistro(String tabela, String condicao) {
String stmt;
if (fconn == null)
return 0;
else {
try {
stmt = "Delete from " + tabela + " where " + condicao;
System.out.println(stmt);
Statement s = fconn.createStatement();
return s.executeUpdate(stmt);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
}

public void inserirRegistroEsp(String stmt) {
try {
Statement s = fconn.createStatement();
s.executeUpdate(stmt);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Rating: 0.00 (0 votes) - Rate this News - Comments?
Java : Busca Binária
em 23/3/2008 8:50:00 (114 leituras)
Java

Algoritimo BuscaBinaria {
Var tam : Integer // Tamanho pré-determinado do conjunto:
find : Integer // Valor a ser buscado:
inc : Integer; // Variável contadora de saltos;
tempo : long; // Variável contadora de tempo de execução do método:
tam : array[] de Inteiros; // Criação da lista de 'tam' números:
int i;


função getResult(valor : inteiro): inteiro
Var index : integer;
x : integer;
loc : integer;
laco1 : boolean;
laco2 : boolean;

iniciar
index = 0;
x = 2;
loc = tam/x;
laco1 = true;
laco2 = true;

Enquanto (true) Faça

Se (valor <= array[loc]) Então

Se (valor == array[loc]) {
Escreva ("Achou.");
index = loc;
Se_não

Se (x > 0) Então
x = x * 2;
Fim_se

Se (tam/x == 0) Então
loc = loc - 1;
Se_não
loc = loc - tam/x;
Fim_se

Fim_se

Se_não

Se (x > 0) então
x = x * 2;
Fim_se

Se (tam/x == 0) Então
loc = loc + 1;
Se_não
loc = loc + tam/x;
Fim_Se

Fim_Se

Fim_Enquanto

retorna index;

Fim

inicio
tam = 10
find = 6
inc = 0
tempo = 0
i = 0;

Para (int i=0; i tam[i] = i;
Fim_para
Fim

}

Rating: 0.00 (0 votes) - Rate this News - Comments?
Java : Inserção Direta - Exemplo
em 1/1/1970 7:10:00 (120 leituras)
Java



algoritmo revisao2
var ex : conjunto[100] de real
i, j, m : inteiro
achou : logico
n : real
inicio
m = -1
para i de 0 ate 99 faca
achou = .F.
leia n
m = m + 1
se m = 0 entao
ex[m] = n
senao
j = m
enquanto (j >= 0) .E. (.NAO.achou) faca
se ex[j - 1] > n entao
ex[j] = ex[j-1]
achou = .V.
fim_se
j = j - 1
fim_enquanto
ex[j] = n
fim_se
fim_para
fim

Rating: 0.00 (0 votes) - Rate this News - Comments?
Java : Liquidificador - Virtual
em 1/1/1970 6:50:00 (76 leituras)
Java

public class Liquidificador{


private int velocidade, velocidadeMaxima;


public Liquidificador(int vm){

velocidadeMaxima = vm;
velocidade = 1;
}

public Liquidificador(){

velocidadeMaxima = 2;
velocidade = 1;
}


public void setVelocidadeMaxima(int vm){

if (velocidadeMaxima>0)
{
velocidadeMaxima = vm;
}
}


public int getVelocidadeMaxima(){

return velocidadeMaxima;
}


public void setVelocidade(int vl){

if ((velocidade>=0)&(vl<=velocidadeMaxima))
{
velocidade = vl;
System.out.println(vl);
}
}

public int getVelocidade(){

return velocidade;
}


}

Rating: 0.00 (0 votes) - Rate this News - Comments?
Clima Portal