Skip to content

Instantly share code, notes, and snippets.

@arturotena
arturotena / UpdateBlob
Created February 10, 2014 17:11
To update a blob field, either byte or text / Para actualizar un campo tipo blob, ya sea de bytes o de texto
// Para actualizar un campo tipo blob, ya sea de bytes o de texto / To update a blob field, either byte or text
Class.forName(driver);
Connection con = DriverManager.getConnection(...)
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT info FROM tabla WHERE id = 1";
ResultSet rs = stmt.executeQuery(query);
rs.next();
if (esBinario) {
rs.updateBytes(campo, contenidoBytes);
@arturotena
arturotena / cifrar-decifrar.java
Last active April 18, 2023 16:48
Cifrar y descifrar en Java (encriptar y desencriptar)
/*
* Código fácil para cifrar y descifrar en Java ("encriptar" y "desencriptar").
* No ha sido auditado, ni garantizo su seguridad.
*/
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public byte[] cifra(String sinCifrar) throws Exception {
@arturotena
arturotena / equalsNullSafe.java
Last active August 29, 2015 14:02
Returns true if both objects are equal, even if any of them is null
/*
Copyright 2014 Arturo Tena
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@arturotena
arturotena / equals_hashcode.java
Last active August 29, 2015 14:02
How to overwrite equals() and hashcode()
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof MyClass)) return false;
final MyClass that = (MyClass) other;
return this.intField == that.intField &&
this.booleanField == that.booleanField &&
(this.stringField == null ? this.stringField == null : this.stringField.equals(that.stringField)) &&
(this.objectField == null ? this.objectField == null : this.objectField.equals(that.objectField));
}
class Fechas implements Iterable<Timestamp> {
FechasIter iter;
Fechas(int field, int amount) {
this.iter = new FechasIter(field, amount);
}
public Iterator<Timestamp> iterator() {
return iter;
}
@arturotena
arturotena / CountLines.java
Created September 3, 2014 21:24
Quick-and-dirty way to count the lines of Java source code of a project
// Quick-and-dirty way to count the lines of Java source code of a project
System.out.println(Files.walk(Paths.get("."))
.map(e -> e.toFile())
.filter(f -> f.isFile() && f.canRead())
.filter(f -> f.getName().endsWith(".java"))
.mapToLong(f -> {
try { return new BufferedReader(new FileReader( f )).lines().count();
} catch (Exception ex) { throw new RuntimeException(ex); }
})
.reduce(0, (acc, current) -> acc + current));
@arturotena
arturotena / root-shell.sh
Created September 7, 2014 21:54
Abre un shell de root en OS X
# Abre un shell de root en OS X
# El usuario actual debe ser administrador
sudo -s
# La contraseña que solicita es del usuario actual.
@arturotena
arturotena / find-file.sh
Created September 7, 2014 21:59
Encuentra archivos con cierto nombre en OS X
# Encuentra archivos con cierto nombre en OS X
find / -name "java"
@arturotena
arturotena / compareTo.java
Created September 23, 2014 23:32
One option to implement Comparable.compareTo()
// I haven't tested it
public int compareTo(MyClass that) {
if (this == that) return 0;
int comp = Integer.valueOf(this.intField).compareTo(that.intField);
if (comp != 0) return comp;
comp = this.stringField == null ? -1 : this.claveRed.compareTo(that.stringField);
if (comp != 0) return comp;
comp = Boolean.valueOf(this.booleanField).compareTo(that.booleanField);
if (comp != 0) return comp;
comp = this.objectField == null ? -1 : this.claveRed.compareTo(that.objectField);
@arturotena
arturotena / removeAllExcept.java
Created October 9, 2014 22:41
Remove all entries except the given keys
/** Remove all entries except the given keys */
private <K,V>void removeAllExcept(Map<K,V> map, K... keys) {
Map<K,V> objs = new HashMap<K,V>(keys.length);
for (K key : keys)
objs.put(key, map.get(key));
map.clear();
map.putAll(objs);
}
/** Remove all entries except the given keys */