Created
July 17, 2020 18:00
-
-
Save igolden/26d38a8d242a137b22ffc9d1e12dcfb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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