Guest User

Untitled

a guest
May 28th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package productTypes;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DB {
  11. //Create connection object
  12. static Connection connection = null;
  13.  
  14. public static void init() {
  15. if (connection == null) {
  16. System.out.println("~~~~~~Oracle JDBC Connection Testing~~~~~~");
  17. }
  18. try {
  19. //Register JDBC Driver
  20. Class.forName("oracle.jdbc.driver.OracleDriver");
  21. } catch (ClassNotFoundException e) {
  22. System.out.println("Where is your Oracle JDBC Driver?");
  23. e.printStackTrace();
  24. return;
  25. }
  26. System.out.println("Oracle JDBC Driver Registered!");
  27.  
  28. try {
  29. // Create the DB url
  30. String url = "jdbc:oracle:thin:@10.100.51.123:1521:orcl";
  31. /* //Your DB Username
  32. Scanner scanner = new Scanner(System.in);
  33. System.out.println("Type your username:\n");
  34. String user = scanner.nextLine();
  35. //Your DB password
  36. System.out.println("Type your password:\n");
  37. String pass = scanner.nextLine(); */
  38. //Your DB Username
  39. String user = "";
  40. //Your DB password
  41. String pass = "";
  42. //Make the connection to the url
  43. connection = DriverManager.getConnection(url, user, pass);
  44. } catch (SQLException e) {
  45. System.out.println("Connection Failed: Check output console");
  46. e.printStackTrace();
  47. return;
  48. }
  49.  
  50. //Check if connection to DB has succeeded
  51. if (connection != null) {
  52. System.out.println("You made it, take control of your data base now!");
  53. } else {
  54. System.out.println("Failed connection!");
  55. }
  56.  
  57. }
  58.  
  59. //Create a new DB table
  60. public static void CreateTable() throws SQLException{
  61. PreparedStatement prepStatement = null;
  62. String CreateSQL = "DROP TABLE skroutz"
  63. + "CREATE TABLE skroutz("
  64. + "CATEGORY_NAME VARCHAR2(255) NOT NULL, "
  65. + "CATEGORY_URL VARCHAR2(255) NOT NULL"
  66. + ")";
  67. prepStatement = connection.prepareStatement(CreateSQL);
  68. prepStatement.executeUpdate();
  69. System.out.println("Table created");
  70.  
  71. }
  72. //Insert data into table
  73. public static void InsertData(String categoryName, String categoryUrl) throws SQLException {
  74. String insertDataSQL = "INSERT INTO skroutz"
  75. +"(CATEGORY_NAME, CATEGORY_URL) VALUES"
  76. +"(?,?)";
  77. PreparedStatement inserPrepState = connection.prepareStatement(insertDataSQL);
  78. inserPrepState.setString(1, categoryName);
  79. inserPrepState.setString(2, categoryUrl);
  80. inserPrepState.executeUpdate();
  81. System.out.println(categoryName);
  82. System.out.println(categoryUrl);
  83. System.out.println("Records are inserted!");
  84. inserPrepState.close();
  85. }
  86. public static void ReadData() throws SQLException {
  87. String readDataSQL = "SELECT CATEGORY_NAME, CATEGORY_URL FROM SKROUTZ;";
  88. System.out.println();
  89. }
  90. }
Add Comment
Please, Sign In to add comment