Advertisement
Guest User

Untitled

a guest
May 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.lang.*;
  4. import oracle.jdbc.driver.*; //make sure this is in classpath
  5. //import java.util.Scanner;
  6. //----------------------------------------------------------------------------------
  7. public class CallGet2 {
  8.  
  9. static String servername = "147.252.224.76"; //"ferdia.student.comp.dit.ie";
  10. static String portnumber = "1521";
  11. static String sid = "ORA11GDB";
  12. static String url = "jdbc:oracle:thin:@" + servername + ":" + portnumber + ":" + sid;
  13. static String user = "lmariano";
  14. static String pass = "c07685874";
  15. static Connection conn = DriverManager.getConnection(url, user, pass);
  16. //System.out.println(url);
  17. //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  18. //Connection conn = DriverManager.getConnection(url, user, pass);
  19.  
  20.  
  21. public static void main (String args [])
  22. throws SQLException, IOException {
  23. try {
  24. Class.forName("oracle.jdbc.driver.OracleDriver");
  25. System.out.println("driver loaded");
  26. } catch (ClassNotFoundException e) {
  27. System.out.println ("Could not load the driver");
  28. }
  29. /* String servername = "147.252.224.76"; //"ferdia.student.comp.dit.ie";
  30. String portnumber = "1521";
  31. String sid = "ORA11GDB";
  32. String url = "jdbc:oracle:thin:@" + servername + ":" + portnumber + ":" + sid;
  33. String user, pass;
  34. user = "lmariano";
  35. pass = "c07685874";
  36. System.out.println(url);*/
  37. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  38.  
  39. //Connection conn = DriverManager.getConnection(url, user, pass);
  40. System.out.println ("after connection");
  41.  
  42.  
  43.  
  44. //Student.student = new Student();
  45.  
  46. //Scanner input = new Scanner (System.in);
  47.  
  48.  
  49.  
  50.  
  51.  
  52. // conn.close();
  53.  
  54.  
  55. System.out.print("\n\nLESSON CATEGORY TABLE\n\n");
  56. PreparedStatement pst2 = conn.prepareStatement("select * from lesson_category");
  57. ResultSet rs2 = pst2.executeQuery();
  58.  
  59. System.out.println("catno\tcatname\tcatdesc");
  60. while (rs2.next()) {
  61. System.out.print(rs2.getInt(1) + "\t");
  62. System.out.print(rs2.getString(2) + "\t ");
  63. System.out.print(rs2.getString(3));
  64. System.out.print("\n");
  65.  
  66. }
  67.  
  68. //rs2.close();
  69. //pst2.close();
  70. //conn.close();
  71.  
  72.  
  73.  
  74. System.out.print("\n\nEDUCATION TABLE\n\n");
  75. PreparedStatement pst3 = conn.prepareStatement("select * from education_level");
  76. ResultSet rs3 = pst3.executeQuery();
  77.  
  78. System.out.println("eduno\teduname\tedudesc");
  79. while (rs3.next()) {
  80. System.out.print(rs3.getInt(1) + "\t");
  81. System.out.print(rs3.getString(2) + "\t ");
  82. System.out.print(rs3.getString(3));
  83. System.out.print("\n\n");
  84.  
  85. }
  86.  
  87. rs3.close();
  88. pst3.close();
  89.  
  90.  
  91.  
  92. System.out.println("\n\nDelete\n\n");
  93. String fname2 = readEntry("Enter student firstname to delete in the database : ");
  94.  
  95. String sql2 = "DELETE from student where firstname like ?";
  96. PreparedStatement pst4 = conn.prepareStatement(sql2);
  97. pst4.setString(1,fname2);
  98.  
  99. pst4.executeQuery();
  100. pst4.close();
  101.  
  102.  
  103. int num = readNumber("Enter student number : ");
  104. int enumber = readNumber("Enter edu : ");
  105. int vnum = readNumber("Enter vis : ");
  106. String fnam = readEntry("Enter fname : ");
  107. String lnam = readEntry("Enter lname : ");
  108. String dob = readEntry("Enter dob : ");
  109.  
  110. String sql1 = "INSERT INTO student values (?,?,?,?,?,?)";
  111.  
  112. PreparedStatement pstmt2 = conn.prepareStatement(sql1);
  113.  
  114. pstmt2.setInt(1,num);
  115. pstmt2.setInt(2,enumber);
  116. pstmt2.setInt(3,vnum);
  117. pstmt2.setString(4,fnam);
  118. pstmt2.setString(5,lnam);
  119. pstmt2.setString(6,dob);
  120.  
  121. pstmt2.executeUpdate();
  122. pstmt2.close();
  123. display();
  124. conn.close();
  125.  
  126.  
  127. /* System.out.println("catno\tcatname\tcatdesc");
  128. while (rs2.next()) {
  129. System.out.print(rs2.getInt(1) + "\t");
  130. System.out.print(rs2.getString(2) + "\t ");
  131. System.out.print(rs2.getString(3));
  132. System.out.print("\n");
  133.  
  134. }*/
  135. //conn.close();*/
  136.  
  137.  
  138. }
  139.  
  140.  
  141. public static void display()
  142. {
  143. Connection conn = DriverManager.getConnection(url, user, pass);
  144.  
  145.  
  146.  
  147. System.out.print("\n\nSTUDENT TABLE\n\n");
  148. PreparedStatement pst = conn.prepareStatement("select * from student");
  149. ResultSet rs = pst.executeQuery();
  150.  
  151. System.out.println("stno\teduno\tvisno\tfname\tlname\tdob");
  152. while (rs.next()) {
  153. System.out.print(rs.getInt(1) + "\t");
  154. System.out.print(rs.getInt(2) + "\t ");
  155. System.out.print(rs.getInt(3) + "\t ");
  156. System.out.print(rs.getString(4) + "\t ");
  157. System.out.print(rs.getString(5) + "\t ");
  158. System.out.println(rs.getString(6));
  159. }
  160.  
  161. rs.close();
  162. pst.close();
  163.  
  164. }
  165.  
  166. //readEntry function -- to read input string
  167. static String readEntry(String prompt) {
  168. try {
  169. StringBuffer buffer = new StringBuffer();
  170. System.out.print(prompt);
  171. System.out.flush();
  172. int c = System.in.read();
  173. while (c != '\n' && c != -1) {
  174. buffer.append((char)c);
  175. c = System.in.read();
  176. }
  177. return buffer.toString().trim();
  178. } catch (IOException e) {
  179. return "";
  180. }
  181. }
  182.  
  183. //readNumber function -- to read input number
  184. static int readNumber(String prompt)
  185. throws IOException{
  186. String snum;
  187. int num = 0;
  188. boolean numok;
  189. do {
  190. snum = readEntry(prompt);
  191. try {
  192. num = Integer.parseInt(snum);
  193. numok = true;
  194. } catch (NumberFormatException e) {
  195. numok = false;
  196. System.out.println("Invalid number; enter again");
  197. }
  198. } while (!numok);
  199. return num;
  200. }
  201.  
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement