Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package productTypes;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DB {
- //Create connection object
- static Connection connection = null;
- public static void init() {
- if (connection == null) {
- System.out.println("~~~~~~Oracle JDBC Connection Testing~~~~~~");
- }
- try {
- //Register JDBC Driver
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your Oracle JDBC Driver?");
- e.printStackTrace();
- return;
- }
- System.out.println("Oracle JDBC Driver Registered!");
- try {
- // Create the DB url
- String url = "jdbc:oracle:thin:@10.100.51.123:1521:orcl";
- /* //Your DB Username
- Scanner scanner = new Scanner(System.in);
- System.out.println("Type your username:\n");
- String user = scanner.nextLine();
- //Your DB password
- System.out.println("Type your password:\n");
- String pass = scanner.nextLine(); */
- //Your DB Username
- String user = "";
- //Your DB password
- String pass = "";
- //Make the connection to the url
- connection = DriverManager.getConnection(url, user, pass);
- } catch (SQLException e) {
- System.out.println("Connection Failed: Check output console");
- e.printStackTrace();
- return;
- }
- //Check if connection to DB has succeeded
- if (connection != null) {
- System.out.println("You made it, take control of your data base now!");
- } else {
- System.out.println("Failed connection!");
- }
- }
- //Create a new DB table
- public static void CreateTable() throws SQLException{
- PreparedStatement prepStatement = null;
- String CreateSQL = "DROP TABLE skroutz"
- + "CREATE TABLE skroutz("
- + "CATEGORY_NAME VARCHAR2(255) NOT NULL, "
- + "CATEGORY_URL VARCHAR2(255) NOT NULL"
- + ")";
- prepStatement = connection.prepareStatement(CreateSQL);
- prepStatement.executeUpdate();
- System.out.println("Table created");
- }
- //Insert data into table
- public static void InsertData(String categoryName, String categoryUrl) throws SQLException {
- String insertDataSQL = "INSERT INTO skroutz"
- +"(CATEGORY_NAME, CATEGORY_URL) VALUES"
- +"(?,?)";
- PreparedStatement inserPrepState = connection.prepareStatement(insertDataSQL);
- inserPrepState.setString(1, categoryName);
- inserPrepState.setString(2, categoryUrl);
- inserPrepState.executeUpdate();
- System.out.println(categoryName);
- System.out.println(categoryUrl);
- System.out.println("Records are inserted!");
- inserPrepState.close();
- }
- public static void ReadData() throws SQLException {
- String readDataSQL = "SELECT CATEGORY_NAME, CATEGORY_URL FROM SKROUTZ;";
- System.out.println();
- }
- }
Add Comment
Please, Sign In to add comment