Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. The following is my code:
  2.  
  3. import java.sql.*;
  4.  
  5. public class homework4 {
  6.  
  7. public static void main(String[] args) throws SQLException, ClassNotFoundException{
  8.  
  9. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  10.  
  11. System.out.println("Driver loaded");
  12.  
  13. Connection connection = DriverManager.getConnection
  14.  
  15. ("jdbc:sqlserver://s16988308.onlinehome-server.com:1433;databaseName=CUNY_DB;"
  16.  
  17. + "integratedSecurity=false;", "cst3613", "password1");
  18.  
  19. System.out.println("Database connected");
  20.  
  21. Statement statement = connection.createStatement();
  22.  
  23. ResultSet resultSet = statement.executeQuery
  24.  
  25. ("SELECT firstName, lastName, e.courseid, c.title "
  26.  
  27. + "FROM students s, course c, enrollment e "
  28.  
  29. + "WHERE s.ssn = e.ssn AND e.courseid = c.courseid "
  30.  
  31. + "AND s.ssn " + " = '345678321'");
  32.  
  33. while (resultSet.next())
  34.  
  35. System.out.println(resultSet.getString(1) + "\t"
  36.  
  37. + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t"
  38.  
  39. + resultSet.getString(4));
  40.  
  41. resultSet.close();
  42.  
  43. connection.close();
  44.  
  45. }
  46.  
  47. }
  48.  
  49. Now nedd to modify using following requirement. I don't know how to do it on microsoft server management studio. So, you have to do it on sql server management studio.
  50.  
  51. Modify your homework 4 to enroll new course for you using [dbo].[insertStudent] store procedure from database.
  52.  
  53. You must ask user to enter SSN and course ID.
  54.  
  55. PROCEDURE [dbo].[insertStudent]
  56.  
  57. @SSN nvarchar(9),
  58.  
  59. @CourseID nvarchar(5)
  60.  
  61.  
  62.  
  63. Answer:=========================================
  64.  
  65.  
  66. package com.chegg.question6;
  67.  
  68. import java.sql.*;
  69.  
  70. import java.util.Scanner;
  71.  
  72. public class homework4 {
  73.  
  74. public static void main(String[] args) throws SQLException, ClassNotFoundException{
  75.  
  76. ResultSet resultSet = null;
  77.  
  78. PreparedStatement preparedStmt=null;
  79.  
  80. Connection connection = null;
  81.  
  82. Statement statement = null;
  83.  
  84. Scanner sc =null;
  85.  
  86. try{
  87.  
  88. sc=new Scanner(System.in);
  89.  
  90. System.out.println("Please enter the SSN for the student you want to enroll");
  91.  
  92. Long ssn=sc.nextLong();
  93.  
  94. System.out.println("Please enter the course id which the student wants to enroll in");
  95.  
  96. String courseId =sc.next();
  97.  
  98. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  99.  
  100. System.out.println("Driver loaded");
  101.  
  102. connection = DriverManager.getConnection
  103.  
  104. ("jdbc:sqlserver://s16988308.onlinehome-server.com:1433;databaseName=CUNY_DB;"
  105.  
  106. + "integratedSecurity=false;", "cst3613", "password1");
  107.  
  108. System.out.println("Database connected");
  109.  
  110. String storedProcSql = "EXEC insertStudent ?,?"; // for stored proc taking 2 parameters ssn and course id
  111.  
  112. preparedStmt = connection.prepareStatement(storedProcSql);
  113.  
  114. preparedStmt.setLong(1, ssn);
  115.  
  116. preparedStmt.setString(2,courseId);
  117.  
  118. int rows = preparedStmt.executeUpdate();
  119.  
  120. System.out.println("No. of rows inserted : " + rows );
  121.  
  122. // After successful insertion , fetch the row inserted in the following lines
  123.  
  124. statement = connection.createStatement();
  125.  
  126. resultSet = statement.executeQuery
  127.  
  128. ("SELECT firstName, lastName, e.courseid, c.title "
  129.  
  130. + "FROM students s, course c, enrollment e "
  131.  
  132. + "WHERE s.ssn = e.ssn AND e.courseid = c.courseid "
  133.  
  134. + "AND s.ssn " + " = "+ ssn); // SSN is of the type LONG here
  135.  
  136. while (resultSet.next())
  137.  
  138. System.out.println(resultSet.getString(1) + "\t"
  139.  
  140. + resultSet.getString(2) + "\t" + resultSet.getString(3) + "\t"
  141.  
  142. + resultSet.getString(4));
  143.  
  144. }
  145.  
  146. catch(SQLException sqlExec){
  147.  
  148. sqlExec.printStackTrace();
  149.  
  150. }
  151.  
  152. catch(Exception e){
  153.  
  154. e.printStackTrace();
  155.  
  156. }
  157.  
  158. finally{
  159.  
  160. resultSet.close();
  161.  
  162. connection.close();
  163.  
  164. sc.close();
  165.  
  166. }
  167.  
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement