Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 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.  
  8. public class UpisStudenta2 {
  9.    
  10.    public static void main(String argv[]) {
  11.       Connection connection = otvoriKonekciju();
  12.       Statement stmt = null;
  13.       try {
  14.          stmt = connection.createStatement();
  15.          ResultSet rs_imaSlobodnih = stmt.executeQuery("SELECT TOP 1 oznGrupa, MIN((brojStud/kapacitet)) AS popunjenost FROM grupa GROUP BY oznGrupa ORDER BY popunjenost ASC");
  16.          ResultSet rs_minPopunjenaGrupa = stmt.executeQuery("SELECT 1 oznGrupa, MIN((cast(brojStud as float) / cast(kapacitet as float))) AS popunjenost FROM grupa GROUP BY oznGrupa ORDER BY popunjenost ASC, oznGrupa ASC");
  17.          ResultSet rs_postojiStudent = stmt.executeQuery("select jmbag from stud where jmbag like " + argv[0]);
  18.          ResultSet rs_studentVecUpisan = stmt.executeQuery("select jmbag from studGrupa where jmbag like " + argv[0]);
  19.          float popunjenost = rs_minPopunjenaGrupa.getFloat("popunjenost");
  20.          if(popunjenost == 1) {
  21.             System.out.println("Sve grupe su već popunjene.");
  22.             System.exit(-1);
  23.          } else if (!rs_postojiStudent.first()) {
  24.             System.out.println("Student ne postoji.");
  25.             System.exit(-1);
  26.          } else if(rs_studentVecUpisan.first()) {
  27.             System.out.println("Student je vec upisan u nastavnu grupu.");
  28.             System.exit(-1);
  29.          }
  30.  
  31.          stmt.executeUpdate("INSERT INTO studGrupa VALUES(" + argv[0] + ", " + rs_minPopunjenaGrupa.getString("oznGrupa") + ")");
  32.          stmt.executeUpdate("UPDATE studGrupa SET brojStud = brojStud + 1 WHERE oznGrupa = " + rs_minPopunjenaGrupa.getString("oznGrupa"));
  33.  
  34.          System.out.println("Student je uspjesno upisan u grupu.");
  35.          /*rs_imaSlobodnih.close();
  36.          rs_minPopunjenaGrupa.close();
  37.          rs_postojiStudent.close();
  38.          rs_studentVecUpisan.close();
  39.          stmt.close();
  40.          connection.close();*/
  41.       }  catch (SQLException exception1) {
  42.             ispisiPogresku(exception1);
  43.             System.exit(-1);
  44.       }
  45.          // I u sluèaju pogreške trebalo bi osloboditi resurse,
  46.          // ali program ovdje ionako završava.  
  47.    }
  48.    
  49.    private static void ispisiPogresku(SQLException exception) {
  50.       System.out.println(exception.getErrorCode() + "; "
  51.             + exception.getMessage() + "; "
  52.             + "State=" + exception.getSQLState());
  53.    }
  54.  
  55.    private static Connection otvoriKonekciju () {
  56.       // sastavljanje JDBC URL:
  57.       String url =  
  58.               "jdbc:sqlserver://localhost:1433;"   // ovdje staviti svoje vrijednosti
  59.             + "databaseName=labprof1;"
  60.             + "user=sa;"
  61.             + "password=;" ;           // staviti svoje vrijednosti
  62.  
  63.       // uèitavanje i registriranje SQL Server JDBC driver-a
  64.       try {
  65.          // connection = DriverManager.getConnection(connectionString);
  66.          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  67.          System.out.println("SQL Server JDBC driver je uèitan i registriran.");
  68.       } catch (ClassNotFoundException exception) {
  69.          System.out.println("Pogreška: nije uspjelo uèitavanje JDBC driver-a.");
  70.          System.out.println(exception.getMessage());
  71.          System.exit(-1);
  72.       }
  73.      
  74.       // uspostavljanje konekcije
  75.       Connection conn = null;
  76.       try {
  77.          conn = DriverManager.getConnection(url);
  78.          System.out.println("Konekcija je uspostavljena.");
  79.       } catch (SQLException exception) {
  80.          System.out.println("Pogreška: nije uspjelo uspostavljanje konekcije.");
  81.          System.out.println(exception.getErrorCode() + " " + exception.getMessage());
  82.          System.exit(-1);
  83.       }
  84.       return conn;
  85.    }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement