Guest User

Untitled

a guest
Mar 9th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 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. + " post_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
  20. + "USERNAME VARCHAR(20) NOT NULL,\n "
  21. + "CREATED_BY VARCHAR(20) NOT NULL,\n "
  22. + "CREATED_DATE DATE NOT NULL )";
  23. String insertTableSQL = "INSERT INTO TEST "
  24. + "(USERNAME, CREATED_BY, CREATED_DATE) "
  25. +"VALUES" +"('mkyeonge','SSSyestem', " + " datetime('now'))";
  26. try {
  27. dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first2,0.db");
  28. statement = dbConnection.createStatement();
  29. statement.execute(createTableSQL);
  30. System.out.println("Table \"TEST\" is created!");
  31.  
  32. } catch (SQLException e) {
  33. System.out.println(e.getMessage());
  34. }
  35. try {
  36. dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first2,0.db");
  37. statement = dbConnection.createStatement();
  38. statement.execute(insertTableSQL);
  39. System.out.println("Table \"TEST\" is Update!");
  40. }
  41. catch (SQLException f){
  42. System.out.println(f.getMessage());
  43. }
  44. finally {
  45. if (statement != null) {
  46. statement.close();
  47. }
  48. if (dbConnection != null) {
  49. dbConnection.close();
  50. }
  51. }
  52. }
  53. public static void main(String[] argv) {
  54. try {
  55. createDbUserTable();
  56. } catch (SQLException e) {
  57. System.out.println(e.getMessage());
  58. }
  59. }
  60. }
Add Comment
Please, Sign In to add comment