Prikaz jedne poruke
Stara 6.8.2015, 14:20   #236
mica1709
Banned
 
Član od: 2.5.2011.
Lokacija: Bujanovac
Poruke: 156
Zahvalnice: 205
Zahvaljeno jedanput na jednoj poruci
Određen forumom Re: How to... Java

Drugi deo koda.

Klasa "UpisivanjeUcenikaNaKurs":

Kod:
package unos;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class UpisivanjeUcenikaNaKurs extends GridPane {
	
	public UpisivanjeUcenikaNaKurs() {
		GridPane grid = new GridPane();
		
		Stage stage = new Stage();
		Scene scene = new Scene(grid, 800, 600);
        stage.setTitle("Upisavanje učenika na kurs");
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
        
        grid.setAlignment(Pos.CENTER);
        grid.setPadding(new Insets(25, 25, 25, 25));
        grid.setHgap(5);
        grid.setVgap(5);
        
        Label ime_kursa = new Label("Izaberite ime kursa: ");
        ime_kursa.setFont(Font.font("Consolas", FontWeight.BOLD, 20));
        grid.add(ime_kursa, 0, 0);
        
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.setPrefWidth(100);
        grid.add(comboBox, 1, 0);
        
        Button btDodaj = new Button("Unesi podatke");
        btDodaj.setFont(Font.font("Verdana", FontWeight.BOLD, 13));
        btDodaj.setPrefSize(130, 30);
        grid.add(btDodaj, 1, 7);
        
        comboBox.setOnAction(new EventHandler<ActionEvent>() {
        	@Override
            public void handle(ActionEvent e) {
        		java.sql.Connection con = null;
        		Statement statement = null;
        	    String url = "jdbc:mysql://localhost/skola_jezika";
        	    String username = "root";
        	    String password = "";
        	    ArrayList<String> comboBoxVrednosti = new ArrayList<String>();
        	    ObservableList<String> data = FXCollections.observableArrayList();
        		try {
        			con = DriverManager.getConnection(url, username, password);
        			statement = con.createStatement();
        			ResultSet rs = statement.executeQuery("SELECT ime_kursa FROM kurs");
        			
        			while(rs.next()) {
        				String ime_kursa = rs.getString("ime_kursa");
        				comboBoxVrednosti.add(ime_kursa);
        			}
        			con.close();
        			
        		} catch (SQLException ex) {
        			ex.printStackTrace();
        		}
        		
        		DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxVrednosti.toArray());
        		
        	}
       });
       
       btDodaj.setOnAction(new EventHandler<ActionEvent>() {
    	   @Override
           public void handle(ActionEvent e) {
    		   if(comboBox.getValue() != null) {
    			   java.sql.Connection con = null;
           		   Statement statement = null;
           		   String url = "jdbc:mysql://localhost/skola_jezika";
           		   String username = "root";
           		   String password = "";
           		   try {
           			   con = DriverManager.getConnection(url, username, password);
                       Statement st = (Statement) con.createStatement();
                       st.execute("INSERT INTO kurs_ucenik (id_kursa)" +"VALUES ('" + comboBox.getValue() + "')");
                       con.close();
                   } catch (SQLException ex) {
                       ex.printStackTrace();
                   }
           		
           		   Alert alert2 = new Alert(AlertType.INFORMATION);
           		   alert2.setTitle("Dialog");
           		   alert2.setHeaderText("Uspešno ste uneli id kursa!");
           		   alert2.setContentText(null);
           		   alert2.showAndWait();
           		
       		   } else {
       			   Alert alert = new Alert(AlertType.ERROR);
       			   alert.setTitle("Uzbuna");
       			   String s = "Molimo Vas popunite sva polja!";
       			   alert.setContentText(s);
       			   alert.showAndWait();
       		  }
    	   }
       });
        
    }

}
mica1709 je offline   Odgovor sa citatom ove poruke