Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class OracleUser extends javax.swing.JFrame {
- String host = "localhost";
- String port = "1521";
- String sid = "xe";
- static String username;
- static String password;
- public OracleUser() throws SQLException {
- initComponents();
- getConnect();
- }
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- OracleUser oracleUser = new OracleUser();
- } catch (SQLException ex) {
- Logger.getLogger(OracleUser.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- }
- private void getConnect() throws SQLException {
- username = "demouser";
- password = "demouser";
- Connection connect = getOracleJDBCConnection(host, port, sid);
- if (connect != null) {
- System.out.println("Connection Established.");
- } else {
- System.out.println("Could not Get Connection");
- createuser();
- }
- }
- private void createuser() throws SQLException {
- String sql;
- Statement stm = null;
- username = "system"; //your oracle system username
- password = "123"; //your oracle system password
- Connection connect = getOracleJDBCConnection(host, port, sid);
- if (connect != null) {
- System.out.println("Connection Extablished.");
- DatabaseMetaData meta = connect.getMetaData();
- stm = connect.createStatement();
- } else {
- System.out.println("Could not Get Connection");
- return;
- }
- try {
- sql = "create user demouser identified by demouser";
- stm.execute(sql);
- sql = "grant create session, dba to demouser";
- stm.execute(sql);
- sql = "grant all privileges to demouser";
- stm.execute(sql);
- } catch (SQLException sqe) {
- System.out.println(sqe.getMessage());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- private Connection getOracleJDBCConnection(String host, String port, String sid) {
- Connection con = null;
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (java.lang.ClassNotFoundException e) {
- System.err.print("ClassNotFoundException: ");
- System.err.println(e.getMessage());
- }
- try {
- String url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
- con = DriverManager.getConnection(url, usernName, password);
- } catch (SQLException ex) {
- System.err.println("SQLException: " + ex.getMessage());
- }
- return con;
- }
- }
Add Comment
Please, Sign In to add comment