Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class UpisStudenta2 {
- public static void main(String argv[]) {
- Connection connection = otvoriKonekciju();
- Statement stmt = null;
- try {
- stmt = connection.createStatement();
- ResultSet rs_imaSlobodnih = stmt.executeQuery("SELECT TOP 1 oznGrupa, MIN((brojStud/kapacitet)) AS popunjenost FROM grupa GROUP BY oznGrupa ORDER BY popunjenost ASC");
- 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");
- ResultSet rs_postojiStudent = stmt.executeQuery("select jmbag from stud where jmbag like " + argv[0]);
- ResultSet rs_studentVecUpisan = stmt.executeQuery("select jmbag from studGrupa where jmbag like " + argv[0]);
- float popunjenost = rs_minPopunjenaGrupa.getFloat("popunjenost");
- if(popunjenost == 1) {
- System.out.println("Sve grupe su već popunjene.");
- System.exit(-1);
- } else if (!rs_postojiStudent.first()) {
- System.out.println("Student ne postoji.");
- System.exit(-1);
- } else if(rs_studentVecUpisan.first()) {
- System.out.println("Student je vec upisan u nastavnu grupu.");
- System.exit(-1);
- }
- stmt.executeUpdate("INSERT INTO studGrupa VALUES(" + argv[0] + ", " + rs_minPopunjenaGrupa.getString("oznGrupa") + ")");
- stmt.executeUpdate("UPDATE studGrupa SET brojStud = brojStud + 1 WHERE oznGrupa = " + rs_minPopunjenaGrupa.getString("oznGrupa"));
- System.out.println("Student je uspjesno upisan u grupu.");
- /*rs_imaSlobodnih.close();
- rs_minPopunjenaGrupa.close();
- rs_postojiStudent.close();
- rs_studentVecUpisan.close();
- stmt.close();
- connection.close();*/
- } catch (SQLException exception1) {
- ispisiPogresku(exception1);
- System.exit(-1);
- }
- // I u sluèaju pogreške trebalo bi osloboditi resurse,
- // ali program ovdje ionako završava.
- }
- private static void ispisiPogresku(SQLException exception) {
- System.out.println(exception.getErrorCode() + "; "
- + exception.getMessage() + "; "
- + "State=" + exception.getSQLState());
- }
- private static Connection otvoriKonekciju () {
- // sastavljanje JDBC URL:
- String url =
- "jdbc:sqlserver://localhost:1433;" // ovdje staviti svoje vrijednosti
- + "databaseName=labprof1;"
- + "user=sa;"
- + "password=;" ; // staviti svoje vrijednosti
- // uèitavanje i registriranje SQL Server JDBC driver-a
- try {
- // connection = DriverManager.getConnection(connectionString);
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- System.out.println("SQL Server JDBC driver je uèitan i registriran.");
- } catch (ClassNotFoundException exception) {
- System.out.println("Pogreška: nije uspjelo uèitavanje JDBC driver-a.");
- System.out.println(exception.getMessage());
- System.exit(-1);
- }
- // uspostavljanje konekcije
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url);
- System.out.println("Konekcija je uspostavljena.");
- } catch (SQLException exception) {
- System.out.println("Pogreška: nije uspjelo uspostavljanje konekcije.");
- System.out.println(exception.getErrorCode() + " " + exception.getMessage());
- System.exit(-1);
- }
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement