Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gestionDB;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class CreationTable {
- private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
- private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
- private static final String DB_USER = "user";
- private static final String DB_PASSWORD = "password";
- public static void main(String[] argv) {
- try {
- createTable();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- private static void createTable() throws SQLException {
- Connection dbConnection = null;
- PreparedStatement preparedStatement = null;
- String createTableSQL = "CREATE TABLE DBUSER1("
- + "USER_ID NUMBER(5) NOT NULL, "
- + "USERNAME VARCHAR(20) NOT NULL, "
- + "CREATED_BY VARCHAR(20) NOT NULL, "
- + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
- + ")";
- try {
- dbConnection = getDBConnection();
- preparedStatement = dbConnection.prepareStatement(createTableSQL);
- System.out.println(createTableSQL);
- // execute create SQL stetement
- preparedStatement.executeUpdate();
- System.out.println("Table \"dbuser\" is created!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName(DB_DRIVER);
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection(
- DB_CONNECTION, DB_USER,DB_PASSWORD);
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement