Guest User

Untitled

a guest
Dec 11th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DatabaseMetaData;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. public class OracleUser extends javax.swing.JFrame {
  10.  
  11. String host = "localhost";
  12. String port = "1521";
  13. String sid = "xe";
  14. static String username;
  15. static String password;
  16.  
  17. public OracleUser() throws SQLException {
  18. initComponents();
  19. getConnect();
  20. }
  21.  
  22. public static void main(String args[]) {
  23. java.awt.EventQueue.invokeLater(new Runnable() {
  24.  
  25. public void run() {
  26. try {
  27. OracleUser oracleUser = new OracleUser();
  28. } catch (SQLException ex) {
  29. Logger.getLogger(OracleUser.class.getName()).log(Level.SEVERE, null, ex);
  30. }
  31. }
  32. });
  33. }
  34.  
  35. private void getConnect() throws SQLException {
  36. username = "demouser";
  37. password = "demouser";
  38. Connection connect = getOracleJDBCConnection(host, port, sid);
  39. if (connect != null) {
  40. System.out.println("Connection Established.");
  41. } else {
  42. System.out.println("Could not Get Connection");
  43. createuser();
  44. }
  45. }
  46.  
  47. private void createuser() throws SQLException {
  48. String sql;
  49. Statement stm = null;
  50. username = "system"; //your oracle system username
  51. password = "123"; //your oracle system password
  52. Connection connect = getOracleJDBCConnection(host, port, sid);
  53. if (connect != null) {
  54. System.out.println("Connection Extablished.");
  55. DatabaseMetaData meta = connect.getMetaData();
  56. stm = connect.createStatement();
  57. } else {
  58. System.out.println("Could not Get Connection");
  59. return;
  60. }
  61.  
  62. try {
  63. sql = "create user demouser identified by demouser";
  64. stm.execute(sql);
  65. sql = "grant create session, dba to demouser";
  66. stm.execute(sql);
  67. sql = "grant all privileges to demouser";
  68. stm.execute(sql);
  69. } catch (SQLException sqe) {
  70. System.out.println(sqe.getMessage());
  71. } catch (Exception e) {
  72. System.out.println(e.getMessage());
  73. }
  74. }
  75.  
  76. private Connection getOracleJDBCConnection(String host, String port, String sid) {
  77. Connection con = null;
  78.  
  79. try {
  80. Class.forName("oracle.jdbc.driver.OracleDriver");
  81. } catch (java.lang.ClassNotFoundException e) {
  82. System.err.print("ClassNotFoundException: ");
  83. System.err.println(e.getMessage());
  84. }
  85.  
  86. try {
  87. String url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
  88. con = DriverManager.getConnection(url, usernName, password);
  89. } catch (SQLException ex) {
  90. System.err.println("SQLException: " + ex.getMessage());
  91. }
  92.  
  93. return con;
  94. }
  95. }
Add Comment
Please, Sign In to add comment