Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8.  
  9. public class Main {
  10. private static String getCurrentTimeStamp() {
  11. Date today = new Date();
  12. DateFormat dateFormat =new SimpleDateFormat("yy/mm/dd");
  13. return dateFormat.format(today.getTime()); }
  14. private static void createDbUserTable() throws SQLException {
  15. Connection dbConnection = null;
  16. Statement statement = null;
  17.  
  18. String createTableSQL = "CREATE TABLE TEST("
  19. + "USER_ID NUMBER(5) NOT NULL, "
  20. + "USERNAME VARCHAR(20) NOT NULL, "
  21. + "CREATED_BY VARCHAR(20) NOT NULL, "
  22. + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
  23. + ")";
  24. String insertTableSQL = "INSERT INTO TEST "
  25. + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) "
  26. +"VALUES" +"(1,'mkyong','system', " + "to_date('"
  27. + getCurrentTimeStamp() + "', 'yy/mm/dd hh24:mi:ss'))";
  28. try {
  29. dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first111.db");
  30. statement = dbConnection.createStatement();
  31. statement.execute(createTableSQL);
  32. System.out.println("Table \"TEST\" is created!");
  33.  
  34. } catch (SQLException e) {
  35. System.out.println(e.getMessage());
  36. }
  37. try {
  38. dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first111.db");
  39. statement = dbConnection.createStatement();
  40. statement.execute(insertTableSQL);
  41. System.out.println("Table \"TEST\" is Update!");
  42. }
  43. catch (SQLException f){
  44. System.out.println(f.getMessage());
  45. }
  46. finally {
  47. if (statement != null) {
  48. statement.close();
  49. }
  50. if (dbConnection != null) {
  51. dbConnection.close();
  52. }
  53. }
  54. }
  55. public static void main(String[] argv) {
  56. try {
  57. createDbUserTable();
  58. } catch (SQLException e) {
  59. System.out.println(e.getMessage());
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement