use of java.sql.ResultSet in project hibernate-orm by hibernate.
the class JdbcTimestampCustomSessionLevelTimeZoneTest method testTimeZone.
@Test
public void testTimeZone() {
connectionProvider.clear();
doInHibernateSessionBuilder(() -> {
return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
}, s -> {
Person person = new Person();
person.id = 1L;
s.persist(person);
});
assertEquals(1, connectionProvider.getPreparedStatements().size());
PreparedStatement ps = connectionProvider.getPreparedStatements().get(0);
try {
ArgumentCaptor<Calendar> calendarArgumentCaptor = ArgumentCaptor.forClass(Calendar.class);
verify(ps, times(1)).setTimestamp(anyInt(), any(Timestamp.class), calendarArgumentCaptor.capture());
assertEquals(TIME_ZONE, calendarArgumentCaptor.getValue().getTimeZone());
} catch (SQLException e) {
fail(e.getMessage());
}
connectionProvider.clear();
doInHibernateSessionBuilder(() -> {
return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
}, s -> {
s.doWork(connection -> {
try (Statement st = connection.createStatement()) {
try (ResultSet rs = st.executeQuery("select createdOn from Person")) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
int offsetDiff = TimeZone.getDefault().getOffset(0) - TIME_ZONE.getOffset(0);
assertEquals(Math.abs(Long.valueOf(offsetDiff).longValue()), Math.abs(timestamp.getTime()));
}
}
}
});
Person person = s.find(Person.class, 1L);
assertEquals(0, person.createdOn.getTime());
});
}
use of java.sql.ResultSet in project hibernate-orm by hibernate.
the class JdbcTimestampWithoutUTCTimeZoneTest method testTimeZone.
@Test
public void testTimeZone() {
doInHibernate(this::sessionFactory, session -> {
Person person = new Person();
person.id = 1L;
long y2kMillis = LocalDateTime.of(2000, 1, 1, 0, 0, 0).atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
assertEquals(946684800000L, y2kMillis);
person.createdOn = new Timestamp(y2kMillis);
session.persist(person);
});
doInHibernate(this::sessionFactory, s -> {
s.doWork(connection -> {
try (Statement st = connection.createStatement()) {
try (ResultSet rs = st.executeQuery("SELECT to_char(createdon, 'YYYY-MM-DD HH24:MI:SS.US') " + "FROM person")) {
while (rs.next()) {
String timestamp = rs.getString(1);
assertEquals(expectedTimestampValue(), timestamp);
}
}
}
});
});
}
use of java.sql.ResultSet in project Openfire by igniterealtime.
the class RrdSqlBackend method importRRD.
public static void importRRD(String id, File rrdFile) throws IOException {
// Read content from file
FileInputStream stream = null;
byte[] bytes = null;
try {
stream = new FileInputStream(rrdFile);
// Create the byte array to hold the data
bytes = new byte[(int) rrdFile.length()];
// Read in the bytes
int offset = 0;
int numRead;
while (offset < bytes.length && (numRead = stream.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
} finally {
if (stream != null) {
stream.close();
}
}
// Save file content to the DB
Connection con = null;
PreparedStatement pstmt = null;
PreparedStatement insertStmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(JDBC_SELECT);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
// Do not import since there is already an RRD in the DB
} else {
// RRD with the given id does not exist
// we'll insert a new row in the table using the supplied id
// but with no RRD bytes (null)
insertStmt = con.prepareStatement(JDBC_INSERT);
insertStmt.setString(1, id);
insertStmt.setLong(2, System.currentTimeMillis());
insertStmt.setBytes(3, bytes);
insertStmt.executeUpdate();
}
} catch (Exception e) {
Log.error("Error while accessing information in database: " + e);
} finally {
DbConnectionManager.closeStatement(insertStmt);
DbConnectionManager.closeConnection(rs, pstmt, con);
}
}
use of java.sql.ResultSet in project Openfire by igniterealtime.
the class DbRuleManager method getRuleByOrderId.
public Rule getRuleByOrderId(int order) {
Rule rule = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_RULE_BY_ORDER_ID);
pstmt.setInt(1, order);
rs = pstmt.executeQuery();
while (rs.next()) {
rule.setOrder(rs.getInt(1));
String ruleType = rs.getString(2);
if (ruleType.equals(Reject.class.getName())) {
rule = new Reject();
} else if (ruleType.equals(Pass.class.getName())) {
rule = new Pass();
} else if (ruleType.equals(Drop.class.getName())) {
rule = new Drop();
}
rule.setRuleId(rs.getString(3));
rule.setPacketType(Rule.PacketType.valueOf(rs.getString(4)));
rule.setDestination(rs.getString(5));
rule.setSource(rs.getString(6));
rule.isDisabled(rs.getBoolean(7));
rule.doLog(rs.getBoolean(8));
rule.setDescription(rs.getString(9));
rule.setSourceType(Rule.SourceDestType.valueOf(rs.getString(10)));
rule.setDestType(Rule.SourceDestType.valueOf(rs.getString(11)));
}
} catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle);
} finally {
DbConnectionManager.closeConnection(pstmt, con);
}
return rule;
}
use of java.sql.ResultSet in project Core by iConomy.
the class iConomy method onConversion.
public boolean onConversion() {
if (!Constants.Nodes.Convert.getBoolean())
return false;
Thrun.init(new Runnable() {
public void run() {
String from = Constants.Nodes.ConvertFrom.toString();
String table = Constants.Nodes.ConvertTable.toString();
String username = Constants.Nodes.ConvertUsername.toString();
String password = Constants.Nodes.ConvertPassword.toString();
String url = Constants.Nodes.ConvertURL.toString();
if (!Common.matches(from, "h2", "h2db", "h2sql", "mysql", "mysqldb"))
return;
String driver = "", dsn = "";
if (Common.matches(from, "sqlite", "h2", "h2sql", "h2db")) {
driver = "org.h2.Driver";
dsn = "jdbc:h2:" + directory + File.separator + table + ";AUTO_RECONNECT=TRUE";
username = "sa";
password = "sa";
} else if (Common.matches(from, "mysql", "mysqldb")) {
driver = "com.mysql.jdbc.Driver";
dsn = url + "/" + table;
}
if (!DbUtils.loadDriver(driver)) {
System.out.println("Please make sure the " + from + " driver library jar exists.");
return;
}
Connection old = null;
try {
old = (username.isEmpty() && password.isEmpty()) ? DriverManager.getConnection(url) : DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
System.out.println(ex);
return;
}
QueryRunner run = new QueryRunner();
try {
try {
run.query(old, "SELECT * FROM " + table, new ResultSetHandler() {
public Object handle(ResultSet rs) throws SQLException {
Account current = null;
Boolean next = rs.next();
if (next)
if (iConomy.Accounts.exists(rs.getString("username")))
current = iConomy.Accounts.get(rs.getString("username"));
else
iConomy.Accounts.create(rs.getString("username"), rs.getDouble("balance"));
if (current != null)
current.getHoldings().setBalance(rs.getDouble("balance"));
if (next)
if (iConomy.Accounts.exists(rs.getString("username")))
if (rs.getBoolean("hidden"))
iConomy.Accounts.get(rs.getString("username")).setStatus(1);
return true;
}
});
} catch (SQLException ex) {
System.out.println("[iConomy] Error issueing SQL query: " + ex);
} finally {
DbUtils.close(old);
}
} catch (SQLException ex) {
System.out.println("[iConomy] Database Error: " + ex);
}
System.out.println("[iConomy] Conversion complete. Please update your configuration, change convert to false!");
}
});
return false;
}
Aggregations