Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package gestionDB;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. public class CreationTable {
  9.  
  10. private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  11. private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
  12. private static final String DB_USER = "user";
  13. private static final String DB_PASSWORD = "password";
  14.  
  15. public static void main(String[] argv) {
  16. try {
  17. createTable();
  18. } catch (SQLException e) {
  19. System.out.println(e.getMessage());
  20. }
  21. }
  22.  
  23. private static void createTable() throws SQLException {
  24. Connection dbConnection = null;
  25. PreparedStatement preparedStatement = null;
  26. String createTableSQL = "CREATE TABLE DBUSER1("
  27. + "USER_ID NUMBER(5) NOT NULL, "
  28. + "USERNAME VARCHAR(20) NOT NULL, "
  29. + "CREATED_BY VARCHAR(20) NOT NULL, "
  30. + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
  31. + ")";
  32. try {
  33. dbConnection = getDBConnection();
  34. preparedStatement = dbConnection.prepareStatement(createTableSQL);
  35. System.out.println(createTableSQL);
  36. // execute create SQL stetement
  37. preparedStatement.executeUpdate();
  38. System.out.println("Table \"dbuser\" is created!");
  39. } catch (SQLException e) {
  40. System.out.println(e.getMessage());
  41. } finally {
  42. if (preparedStatement != null) {
  43. preparedStatement.close();
  44. }
  45. if (dbConnection != null) {
  46. dbConnection.close();
  47. }
  48. }
  49. }
  50.  
  51.  
  52. private static Connection getDBConnection() {
  53. Connection dbConnection = null;
  54. try {
  55. Class.forName(DB_DRIVER);
  56. } catch (ClassNotFoundException e) {
  57. System.out.println(e.getMessage());
  58. }
  59. try {
  60. dbConnection = DriverManager.getConnection(
  61. DB_CONNECTION, DB_USER,DB_PASSWORD);
  62. return dbConnection;
  63. } catch (SQLException e) {
  64. System.out.println(e.getMessage());
  65. }
  66. return dbConnection;
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement