Guest User

Untitled

a guest
Mar 27th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import java.sql.CallableStatement;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.sql.PreparedStatement;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class UpisStudenta2 {
  12.  
  13. public static void main(String argv[]) {
  14. Connection connection = otvoriKonekciju();
  15. try {
  16. //prepared statement za provjeravanje popunjenosti
  17. PreparedStatement pstmt = connection.prepareStatement("select min(brojStud*1.0/kapacitet) from grupa where (brojStud*1.0/kapacitet)<1");
  18. ResultSet rs = pstmt.executeQuery();
  19. if ( rs.next() == false){
  20. System.out.println("Sve grupe su već popunjene");
  21. System.exit(-1);
  22. }
  23. rs.close();
  24.  
  25. Scanner scanner = new Scanner(System.in);
  26. System.out.print("Upisite jmbag: ");
  27. String jmbag = scanner.next();
  28. PreparedStatement pstmt1 = connection.prepareStatement("Select count(*) from stud where jmbag = ?");
  29. pstmt1.setString(1, jmbag);
  30. rs = pstmt1.executeQuery();
  31.  
  32. if (rs.next() == false){
  33. System.out.println("Student ne postoji");
  34. System.exit(-1);
  35. }
  36. rs.close();
  37.  
  38. // prepared statement za dohvaćanje oznake grupe
  39. PreparedStatement pstmt2 = connection.prepareStatement("SELECT top(1) oznGrupa from grupa where brojStud*1.0/kapacitet = (select min(brojStud*1.0/kapacitet) from grupa) and brojStud*1.0/kapacitet < 1 order by oznGrupa");
  40. ResultSet rs2 = pstmt2.executeQuery();
  41. while(rs2.next()){
  42. String oG = rs2.getString(1);
  43. System.out.println(oG);
  44. }
  45. rs2.close();
  46. connection.close();
  47. }
  48. catch (SQLException exception1) {
  49. int errCode = exception1.getErrorCode();
  50. if (errCode == 50503) {
  51. ResultSet rs = null;
  52. try {
  53.  
  54. int preostalo = rs.getInt("preostalo");
  55. System.out.println("Nije uspjelo raspore�ivanje " + preostalo + " studenata");
  56. }
  57. catch (SQLException exception2) {
  58. ispisiPogresku(exception2);
  59. System.exit(-1);
  60. }
  61. }
  62. else {
  63. ispisiPogresku(exception1);
  64. System.exit(-1);
  65. }
  66. }
  67. }
  68.  
  69. private static void ispisiPogresku(SQLException exception) {
  70. System.out.println(exception.getErrorCode() + "; "
  71. + exception.getMessage() + "; "
  72. + "State=" + exception.getSQLState());
  73. }
  74.  
  75. private static Connection otvoriKonekciju () {
  76. // sastavljanje JDBC URL:
  77. String url =
  78. "jdbc:sqlserver://localhost:1433;" // ovdje staviti svoje vrijednosti
  79. + "databaseName=labprof1;"
  80. + "user=sa;"
  81. + "password=IsXpHbPh;" ; // staviti svoje vrijednosti
  82.  
  83. // u�itavanje i registriranje SQL Server JDBC driver-a
  84. try {
  85. // connection = DriverManager.getConnection(connectionString);
  86. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  87. System.out.println("SQL Server JDBC driver je u�itan i registriran.");
  88. } catch (ClassNotFoundException exception) {
  89. System.out.println("Pogre�ka: nije uspjelo u�itavanje JDBC driver-a.");
  90. System.out.println(exception.getMessage());
  91. System.exit(-1);
  92. }
  93.  
  94. // uspostavljanje konekcije
  95. Connection conn = null;
  96. try {
  97. conn = DriverManager.getConnection(url);
  98. System.out.println("Konekcija je uspostavljena.");
  99. } catch (SQLException exception) {
  100. System.out.println("Pogre�ka: nije uspjelo uspostavljanje konekcije.");
  101. System.out.println(exception.getErrorCode() + " " + exception.getMessage());
  102. System.exit(-1);
  103. }
  104. return conn;
  105. }
  106. }
Add Comment
Please, Sign In to add comment