List of usage examples for java.sql Timestamp Timestamp
public Timestamp(long time)
From source file:Main.java
public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); Date date = new Date(timestamp.getTime()); // S is the millisecond SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss:S"); System.out.println(simpleDateFormat.format(timestamp)); System.out.println(simpleDateFormat.format(date)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Timestamp tstamp = new Timestamp(0); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT myTable VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "x"); prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11")); int row = prest.executeUpdate(); System.out.println(row + " row(s) affected)"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Date date = new Date(); System.out.println("Format To times:"); System.out.println(date.getTime()); Timestamp ts = new Timestamp(date.getTime()); System.out.println(ts);/*from w w w .jav a 2s. co m*/ }
From source file:Main.java
public static void main(String[] args) { long time = System.currentTimeMillis(); Date d = new Date(time); Timestamp t = new Timestamp(time); t.setNanos(123456789);//from w w w. ja va 2 s. c o m System.out.println(d); System.out.println(t); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'"); NumberFormat nf = new DecimalFormat("000000000"); System.out.println(df.format(t.getTime()) + nf.format(t.getNanos())); }
From source file:MainClass.java
public static void main(String[] args) { try {/* www . j a v a2 s . c om*/ String FILE = "c:\\systemin.txt"; FileOutputStream outStr = new FileOutputStream(FILE, true); PrintStream printStream = new PrintStream(outStr); System.setErr(printStream); Timestamp now = new Timestamp(System.currentTimeMillis()); System.out.println(now.toString() + ": This is text that should go to the file"); outStr.close(); printStream.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); System.exit(-1); } catch (IOException ex) { ex.printStackTrace(); System.exit(-1); } }
From source file:Main.java
public static final void main(String[] argv) throws Exception { Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password"); Date nowDate = new Date(); Timestamp nowTimestamp = new Timestamp(nowDate.getTime()); PreparedStatement insertStmt = conn.prepareStatement( "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)"); insertStmt.setString(1, System.getProperty("os.name")); insertStmt.setTimestamp(2, nowTimestamp); insertStmt.setTimestamp(3, nowTimestamp); insertStmt.setTimestamp(4, nowTimestamp); insertStmt.executeUpdate();//from w w w. j a v a 2 s .c o m insertStmt.close(); System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz"); PreparedStatement selectStmt = conn .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable"); ResultSet result = null; result = selectStmt.executeQuery(); while (result.next()) { System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(), result.getTimestamp(3).toString(), result.getTimestamp(4).toString())); } result.close(); selectStmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); String[] columnNames = { "id", "name", "content", "date_created" }; Object[] inputValues = new Object[columnNames.length]; inputValues[0] = new java.math.BigDecimal(100); inputValues[1] = new String("String Value"); inputValues[2] = new String("This is my resume."); inputValues[3] = new Timestamp((new java.util.Date()).getTime()); String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(insert); pstmt.setObject(1, inputValues[0]);/*ww w . j a va 2s .co m*/ pstmt.setObject(2, inputValues[1]); pstmt.setObject(3, inputValues[2]); pstmt.setObject(4, inputValues[3]); pstmt.executeUpdate(); String query = "select id, name, content, date_created from resume where id=?"; PreparedStatement pstmt2 = conn.prepareStatement(query); pstmt2.setObject(1, inputValues[0]); ResultSet rs = pstmt2.executeQuery(); Object[] outputValues = new Object[columnNames.length]; if (rs.next()) { for (int i = 0; i < columnNames.length; i++) { outputValues[i] = rs.getObject(i + 1); } } System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString()); System.out.println("name=" + ((String) outputValues[1])); System.out.println("content=" + ((Clob) outputValues[2])); System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString()); rs.close(); pstmt.close(); pstmt2.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { ResultSet rs = null;//from w w w. j a va 2 s .c om Connection conn = null; PreparedStatement pstmt = null; PreparedStatement pstmt2 = null; conn = getOracleConnection(); String[] columnNames = { "id", "name", "content", "date_created" }; Object[] inputValues = new Object[columnNames.length]; inputValues[0] = new java.math.BigDecimal(100); inputValues[1] = new String("String Value"); inputValues[2] = new String("This is my resume."); inputValues[3] = new Timestamp((new java.util.Date()).getTime()); // prepare blob object from an existing binary column String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)"; pstmt = conn.prepareStatement(insert); pstmt.setObject(1, inputValues[0]); pstmt.setObject(2, inputValues[1]); pstmt.setObject(3, inputValues[2]); pstmt.setObject(4, inputValues[3]); pstmt.executeUpdate(); String query = "select id, name, content, date_created from resume where id=?"; pstmt2 = conn.prepareStatement(query); pstmt2.setObject(1, inputValues[0]); rs = pstmt2.executeQuery(); Object[] outputValues = new Object[columnNames.length]; if (rs.next()) { for (int i = 0; i < columnNames.length; i++) { outputValues[i] = rs.getObject(i + 1); } } System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString()); System.out.println("name=" + ((String) outputValues[1])); System.out.println("content=" + ((Clob) outputValues[2])); System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString()); rs.close(); pstmt.close(); pstmt2.close(); conn.close(); }
From source file:ar.com.springbasic.app.MainApp.java
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("newSpringXMLConfig.xml"); AdminDao adminDao = (AdminDao) applicationContext.getBean("adminDao"); Timestamp ts = new Timestamp(new Date().getTime()); // ***Creo un nuevo admin*** // Admin admin = new Admin(); // admin.setCargo("Gerente"); // admin.setNombre("carlo"); // admin.setFechaCreacion(ts); try {/*w ww.j ava2 s. co m*/ // ***Guardo el admin*** // adminDao.save(admin); // ***traigo todos los campos *** // List<Admin> admins = adminDao.findAll(); // for (Object admin1 : admins) { // System.out.println(admin1); // } // ***Busco por un id*** // System.out.println(adminDao.findById(2)); // Busco por un nombre // System.out.println(adminDao.findByNombre("pepe").toString()); // Modificar y borrar // Admin admin = adminDao.findById(1); // System.out.println("Admin con id 1= " + admin); // // admin.setCargo("Subgerente"); // admin.setNombre("Adrian"); // // if (adminDao.update(admin)) { // System.out.println("Actializacion correcta " +admin); // } // if (adminDao.delete(admin.getIdAd())) { // System.out.println("Admin: "+admin.getNombre() +" eliminado correctamente"); // // } List<Admin> admins = new ArrayList<>(); admins.add(new Admin("Papulo", "Jefe de ingenieria", ts)); admins.add(new Admin("Cucuro", "Chorro", ts)); admins.add(new Admin("Zafalo", "Mujeriego", ts)); admins.add(new Admin("Mengano", "Chupa culo", ts)); int[] vals = adminDao.saveAll(admins); for (int val : vals) { System.out.println("Filas afectadas para esta operacion= " + val); } } catch (CannotGetJdbcConnectionException e) { //Error de conneccion System.out.println(e); } catch (DataAccessException b) { //Error de acceso a datos System.out.println(b); } }
From source file:module.entities.UsernameChecker.CheckOpengovUsernames.java
/** * @param args the command line arguments *///ww w . j a v a 2s .com public static void main(String[] args) throws SQLException, IOException { // args = new String[1]; // args[0] = "searchConf.txt"; Date d = new Date(); long milTime = d.getTime(); long execStart = System.nanoTime(); Timestamp startTime = new Timestamp(milTime); long lStartTime; long lEndTime = 0; int status_id = 1; JSONObject obj = new JSONObject(); if (args.length != 1) { System.out.println("None or too many argument parameters where defined! " + "\nPlease provide ONLY the configuration file name as the only argument."); } else { try { configFile = args[0]; initLexicons(); Database.init(); lStartTime = System.currentTimeMillis(); System.out.println("Opengov username identification process started at: " + startTime); usernameCheckerId = Database.LogUsernameChecker(lStartTime); TreeMap<Integer, String> OpenGovUsernames = Database.GetOpenGovUsers(); HashSet<ReportEntry> report_names = new HashSet<>(); if (OpenGovUsernames.size() > 0) { for (int userID : OpenGovUsernames.keySet()) { String DBusername = Normalizer .normalize(OpenGovUsernames.get(userID).toUpperCase(locale), Normalizer.Form.NFD) .replaceAll("\\p{M}", ""); String username = ""; int type; String[] splitUsername = DBusername.split(" "); if (checkNameInLexicons(splitUsername)) { for (String splText : splitUsername) { username += splText + " "; } type = 1; } else if (checkOrgInLexicons(splitUsername)) { for (String splText : splitUsername) { username += splText + " "; } type = 2; } else { username = DBusername; type = -1; } ReportEntry cerEntry = new ReportEntry(userID, username.trim(), type); report_names.add(cerEntry); } status_id = 2; obj.put("message", "Opengov username checker finished with no errors"); obj.put("details", ""); Database.UpdateOpengovUsersReportName(report_names); lEndTime = System.currentTimeMillis(); } else { status_id = 2; obj.put("message", "Opengov username checker finished with no errors"); obj.put("details", "No usernames needed to be checked"); lEndTime = System.currentTimeMillis(); } } catch (Exception ex) { System.err.println(ex.getMessage()); status_id = 3; obj.put("message", "Opengov username checker encountered an error"); obj.put("details", ex.getMessage().toString()); lEndTime = System.currentTimeMillis(); } } long execEnd = System.nanoTime(); long executionTime = (execEnd - execStart); System.out.println("Total process time: " + (((executionTime / 1000000) / 1000) / 60) + " minutes."); Database.UpdateLogUsernameChecker(lEndTime, status_id, usernameCheckerId, obj); Database.closeConnection(); }