Advertisement
Guest User

Untitled

a guest
May 31st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. public class Version {
  7.  
  8. private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  9. private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
  10. private static final String DB_USER = "user";
  11. private static final String DB_PASSWORD = "password";
  12.  
  13. public static void main(String[] argv) {
  14.  
  15. try {
  16.  
  17. createDbUserTable();
  18.  
  19. } catch (SQLException e) {
  20.  
  21. System.out.println(e.getMessage());
  22.  
  23. }
  24.  
  25. }
  26.  
  27. private static void createDbUserTable() throws SQLException {
  28.  
  29. Connection dbConnection = null;
  30. Statement statement = null;
  31.  
  32. String createTableSQL = "CREATE TABLE DBUSER("
  33. + "USER_ID NUMBER(5) NOT NULL, "
  34. + "USERNAME VARCHAR(20) NOT NULL, "
  35. + "CREATED_BY VARCHAR(20) NOT NULL, "
  36. + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
  37. + ")";
  38.  
  39. try {
  40. dbConnection = getDBConnection();
  41. statement = dbConnection.createStatement();
  42.  
  43. System.out.println(createTableSQL);
  44. // execute the SQL stetement
  45. statement.execute(createTableSQL);
  46.  
  47. System.out.println("Table \"dbuser\" is created!");
  48.  
  49. } catch (SQLException e) {
  50.  
  51. System.out.println(e.getMessage());
  52.  
  53. } finally {
  54.  
  55. if (statement != null) {
  56. statement.close();
  57. }
  58.  
  59. if (dbConnection != null) {
  60. dbConnection.close();
  61. }
  62.  
  63. }
  64.  
  65. }
  66.  
  67. private static Connection getDBConnection() {
  68.  
  69. Connection dbConnection = null;
  70.  
  71. try {
  72.  
  73. Class.forName(DB_DRIVER);
  74.  
  75. } catch (ClassNotFoundException e) {
  76.  
  77. System.out.println(e.getMessage());
  78.  
  79. }
  80.  
  81. try {
  82.  
  83. dbConnection = DriverManager.getConnection(
  84. DB_CONNECTION, DB_USER,DB_PASSWORD);
  85. return dbConnection;
  86.  
  87. } catch (SQLException e) {
  88.  
  89. System.out.println(e.getMessage());
  90.  
  91. }
  92.  
  93. return dbConnection;
  94.  
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement