Skip to content

Instantly share code, notes, and snippets.

PreparedStatement ps = connection.prepareStatement("INSERT INTO users (first_name, email, last_name) VALUES (?, ?, ?)", new String[]{"id"});
ps.setObject(1, "John");
ps.setObject(2, "Doe");
ps.setObject(3, "john@doe.com");
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
rs.next();
Object id = rs.getObject(1);
public void close() {
try {
Connection connection = ConnectionsAccess.getConnection(dbName);
if(connection == null){
throw new DBException("cannot close connection '" + dbName + "' because it is not available");
}
StatementCache.instance().cleanStatementCache(connection);
connection.close();
LogFilter.log(logger, "Closed connection: " + connection);
} catch (Exception e) {
@ipolevoy
ipolevoy / gist:8226928
Created January 2, 2014 21:07
Suggestion for design top open ActiveJDBC connections in desktop applications
package test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class ConnectionDemo {
@ipolevoy
ipolevoy / gist:8401235
Created January 13, 2014 14:31
caching
List users = User.where("id not in (select user_id from restricted_users)");
Person p = new Person();
p.set("first_name", "Marilyn","last_name", "Monroe");
p.set("dob", "1926/6/1"); //wrong format
p.validate();
System.out.println(p.errors().get("dob")); //prints out: attribute dob does not conform to format: yyyy-MM-dd
p.set("dob", "1926-06-01"); //right format
p.validate();
System.out.println(p.get("dob").getClass()); //prints out: class java.sql.Date
System.out.println(p.get("dob")); //prints out: 1926-06-01
<target name="-post-compile">
<java classname="org.javalite.instrumentation.Main" failonerror="true">
<sysproperty key="outputDirectory" value="${build.classes.dir}"/>
<classpath>
<pathelement path="${build.classes.dir}" />
<pathelement path="${javac.classpath}" />
<pathelement path="${javac.processorpath}" />
</classpath>
</java>
</target>
List<Tickets> tickets = Person.where("name = ?", "Bill");
for(Ticket t: tickets)
System.out.println(t);
public class City extends Model{
public void beforeSave(){
set("province_id" Convert.toInteger(get("province_id")));
}
}
PlayerItemDefinition record = new PlayerItemDefinition();
record.set("id", "this is id string");
record.set("player_id", "t123");
record.saveIt();
PlayerItemDefinition.findAll().dump();
@Test
public void test(){
PlayerItemDefinition record = new PlayerItemDefinition();
record.set("id", "this is id string");
record.set("player_id", "t123");
record.saveIt();
PlayerItemDefinition definition = PlayerItemDefinition.findFirst("id = ? and player_id = ?", "this is id string", "t123");