import java.io.*; import java.util.*; import java.sql.*; public class HW1_Create { static final String DbURL = "jdbc:oracle:thin:@dbclass.intro.cs.cmu.edu:1521:dbintro"; //Oracel server at cs.cmu static final String OraDriver = "oracle.jdbc.driver.OracleDriver"; //Oracle driver // static final String User = ""; // static final String Passwd = ""; static final String User = "christos"; static final String Passwd = "christos"; static final String fileName="PC.txt"; //file name for text data public static void main(String[] args) { Connection con = null; try { // Load the Oracle Driver Class.forName(OraDriver); // Get a Connection to the database con = DriverManager.getConnection(DbURL, User, Passwd); // Create a Statement object Statement stmt = con.createStatement(); // Create a table named as PC (varchar2(10), varchar2(10)); String sqlSt = "CREATE TABLE PC(parent varchar2(10), child varchar2(10))"; stmt.executeQuery(sqlSt); // Insert data into table PC DataInputStream in = new DataInputStream(new FileInputStream(fileName)); String line= null; String parent = null; String child = null; int i = 0; while ((line = in.readLine()) != null) { StringTokenizer tk = new StringTokenizer(line, ",\""); if (tk.hasMoreTokens()) parent = tk.nextToken(); if (tk.hasMoreTokens()) child = tk.nextToken(); // Execute a SQL - insert statement sqlSt = "INSERT INTO PC (parent, child) VALUES ('" + parent + "', '" + child + "')"; System.out.println("===" + (i++) + "===>" + sqlSt); stmt.executeQuery(sqlSt); } in.close(); con.commit(); } catch (ClassNotFoundException e) { System.out.println("Could not load database driver: " + e.getMessage()); } catch (SQLException e) { System.out.println("SQLException caught : " + e.getMessage()); } catch (Exception e) { System.out.println("Other Exception caught : " + e.getMessage()); } finally { // Always close the database connection try { if (con != null) con.close(); } catch (SQLException ignored) {} } } //end of main } //end of class