Skip to content

Instantly share code, notes, and snippets.

@igolden
Created July 17, 2020 18:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import java.sql.*;
import java.util.*;
class Main {
public static void main(String[] args) {
try {
// Connections
String url = "jdbc:postgresql://2.tcp.ngrok.io:10553/testdb";
Properties props = new Properties();
props.setProperty("user","postgres");
props.setProperty("password","postgres");
props.setProperty("ssl","false");
Connection conn = DriverManager.getConnection(url, props);
// get these from args passed into the function
// id or uuid is the ID for row to UPDATE
//
// timestamp type will update the column accordingly
int id = 1;
String timestampType = "start"; // or "end". Refers to DB columns
// Get the exact time in milliseconds
long startTime = System.currentTimeMillis() / 1000L;
String startTimeString = String.valueOf(startTime);
// Prepare SQL statement with variables
PreparedStatement st = conn.prepareStatement("UPDATE streams SET " +
timestampType +
" = ? WHERE id = ?");
st.setString(1, startTimeString);
st.setInt(2, id);
// Log statement before running
System.out.println("=====EXECUTING QUERY=======");
System.out.println(st);
System.out.println("=======================");
// execute
ResultSet rs = st.executeQuery();
// Add loop over results here if needed.
// close
rs.close();
st.close();
} catch (Exception e) {
System.out.println("Whoops world!" + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment