Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 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 UpisStudenata2 {
  9.    
  10.    public static void main(String argv[]) {
  11.       Connection connection = otvoriKonekciju();
  12.       Statement stmt = null;
  13.  
  14.       try {
  15.    
  16.  
  17.          while (true) {
  18.             String jmbag = System.console().readLine().trim();
  19.             stmt = connection.createStatement();
  20.            
  21.             //provjere
  22.             ResultSet rs = stmt.executeQuery("select (select SUM(kapacitet) from labprof1.dbo.grupa ) - (select SUM(brojStud) from labprof1.dbo.grupa) ispis");
  23.             rs.next();
  24.             if(Integer.parseInt(rs.getString("ispis").trim()) <= 0){
  25.                 System.out.println("Sve grupe su već popunjene");
  26.                 break;
  27.             }
  28.        
  29.             rs = stmt.executeQuery("select isnull((select COUNT(*) from labprof1.dbo.stud where jmbag = '" + jmbag + "'), 0) ispis");
  30.             rs.next();
  31.             if(Integer.parseInt(rs.getString("ispis").trim()) == 0) {
  32.                 System.out.println("Student ne postoji");
  33.                 continue;
  34.             }
  35.            
  36.             rs = stmt.executeQuery("select isnull((select COUNT(*) from labprof1.dbo.studGrupa where jmbag =  '" + jmbag + "'), 0) ispis");
  37.             rs.next();
  38.             if(Integer.parseInt(rs.getString("ispis").trim()) >= 1) {
  39.                 System.out.println("Student je vec upisan u nastavnu grupu");
  40.                 continue;
  41.             }
  42.  
  43.             //upis
  44.             try{
  45.                 rs = stmt.executeQuery("select MIN(brojStud/CONVERT(decimal(4,2), kapacitet)), oznGrupa as oznGrupa " +
  46.                                     "from labprof1.dbo.grupa group by oznGrupa " +
  47.                                     "order by MIN(brojStud/CONVERT(decimal(4,2), kapacitet)), oznGrupa ");
  48.                 rs.next();
  49.                 String oznGrupa = rs.getString("oznGrupa").trim();
  50.                 stmt.execute("insert into labprof1.dbo.studGrupa values ('" + jmbag + "', '" + oznGrupa  + "')");
  51.                 stmt.execute("update labprof1.dbo.grupa "+
  52.                             "set brojStud = brojStud + 1 "+
  53.                             "where oznGrupa = '" + oznGrupa + "'");
  54.        
  55.                 System.out.println("Student je uspjesno upisan");
  56.    
  57.             }
  58.             catch (SQLException exception) {
  59.                 ispisiPogresku(exception);
  60.                 System.exit(-1);
  61.              }
  62.            
  63.            
  64.       }}
  65.       catch (SQLException exception1) {
  66.          ispisiPogresku(exception1);
  67.          System.exit(-1);
  68.          }
  69.    }
  70.  
  71.  
  72.    private static void ispisiPogresku(SQLException exception) {
  73.       System.out.println(exception.getErrorCode() + "; "
  74.             + exception.getMessage() + "; "
  75.             + "State=" + exception.getSQLState());
  76.    }
  77.    
  78.    private static Connection otvoriKonekciju () {
  79.       // sastavljanje JDBC URL:
  80.       String url =  
  81.               "jdbc:sqlserver://localhost:1433;"   // ovdje staviti svoje vrijednosti
  82.             + "databaseName=labprof1;"
  83.             + "user=sa;"
  84.             + "password=lozinkazabazu;" ;           // staviti svoje vrijednosti
  85.  
  86.       // ucitavanje i registriranje SQL Server JDBC driver-a
  87.       try {
  88.          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  89.          System.out.println("SQL Server JDBC driver je uèitan i registriran.");
  90.       } catch (ClassNotFoundException exception) {
  91.          System.out.println("Pogreška: nije uspjelo uèitavanje JDBC driver-a.");
  92.          System.out.println(exception.getMessage());
  93.          System.exit(-1);
  94.       }
  95.      
  96.       // uspostavljanje konekcije
  97.       Connection conn = null;
  98.       try {
  99.          conn = DriverManager.getConnection(url);
  100.          System.out.println("Konekcija je uspostavljena.");
  101.       } catch (SQLException exception) {
  102.          System.out.println("Pogreška: nije uspjelo uspostavljanje konekcije.");
  103.          System.out.println(exception.getErrorCode() + " " + exception.getMessage());
  104.          System.exit(-1);
  105.       }
  106.       return conn;
  107.    }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement