fork download
  1. package com.mkyong.jdbc;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9.  
  10. public class JDBCStatementInsertExample {
  11.  
  12. private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  13. private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
  14. private static final String DB_USER = "user";
  15. private static final String DB_PASSWORD = "password";
  16. private static final DateFormat dateFormat = new SimpleDateFormat(
  17. "yyyy/MM/dd HH:mm:ss");
  18.  
  19. public static void main(String[] argv) {
  20.  
  21. try {
  22.  
  23. insertRecordIntoDbUserTable();
  24.  
  25. } catch (SQLException e) {
  26.  
  27. System.out.println(e.getMessage());
  28.  
  29. }
  30.  
  31. }
  32.  
  33. private static void insertRecordIntoDbUserTable() throws SQLException {
  34.  
  35. Connection dbConnection = null;
  36. Statement statement = null;
  37.  
  38. String insertTableSQL = "INSERT INTO DBUSER"
  39. + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) " + "VALUES"
  40. + "(1,'mkyong','system', " + "to_date('"
  41. + getCurrentTimeStamp() + "', 'yyyy/mm/dd hh24:mi:ss'))";
  42.  
  43. try {
  44. dbConnection = getDBConnection();
  45. statement = dbConnection.createStatement();
  46.  
  47. System.out.println(insertTableSQL);
  48.  
  49. // execute insert SQL stetement
  50. statement.executeUpdate(insertTableSQL);
  51.  
  52. System.out.println("Record is inserted into DBUSER table!");
  53.  
  54. } catch (SQLException e) {
  55.  
  56. System.out.println(e.getMessage());
  57.  
  58. } finally {
  59.  
  60. if (statement != null) {
  61. statement.close();
  62. }
  63.  
  64. if (dbConnection != null) {
  65. dbConnection.close();
  66. }
  67.  
  68. }
  69.  
  70. }
  71.  
  72. private static Connection getDBConnection() {
  73.  
  74. Connection dbConnection = null;
  75.  
  76. try {
  77.  
  78. Class.forName(DB_DRIVER);
  79.  
  80. } catch (ClassNotFoundException e) {
  81.  
  82. System.out.println(e.getMessage());
  83.  
  84. }
  85.  
  86. try {
  87.  
  88. dbConnection = DriverManager.getConnection(
  89. DB_CONNECTION, DB_USER,DB_PASSWORD);
  90. return dbConnection;
  91.  
  92. } catch (SQLException e) {
  93.  
  94. System.out.println(e.getMessage());
  95.  
  96. }
  97.  
  98. return dbConnection;
  99.  
  100. }
  101.  
  102. private static String getCurrentTimeStamp() {
  103.  
  104. java.util.Date today = new java.util.Date();
  105. return dateFormat.format(today.getTime());
  106.  
  107. }
  108.  
  109.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:107: error: reached end of file while parsing
	}
	 ^
1 error
stdout
Standard output is empty