Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Coneccion {
- private static final String DRIVER = "com.mysql.jbdc.Driver";
- private static final String URL = "jbdc:mysql://localhost:3306/DBProject";
- private static final String USER = "root";
- private static final String PASS = "";
- public static Connection getConnection() throws SQLException{
- try {
- Class.forName(DRIVER);
- return (Connection) DriverManager.getConnection(URL, USER, PASS);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- public static void closeConnection(Connection con){
- if (con!=null) {
- try {
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static void closeConnection(Connection con,PreparedStatement stmt){
- closeConnection(con);
- if (stmt!=null) {
- try {
- stmt.close();
- } catch (SQLException ex) {
- Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public static void closeConnection(Connection con,PreparedStatement stmt,ResultSet rs){
- closeConnection(con,stmt);
- if (rs!=null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public class FXMLDocumentController implements Initializable {
- @FXML private TextField doDepartamento;
- @FXML private TextField doNome;
- @FXML private TextField doLocal;
- @FXML private TableView t;
- @FXML private TableColumn celNumero;
- @FXML private TableColumn celLocal;
- @FXML private TableColumn celNome;
- @FXML private Button bclick;
- private Connection con=null;
- @FXML
- private void handleButtonAction(ActionEvent event) throws SQLException {
- String sql="INSERT INTO Departamento(DepNum, Nome, Local)Values (?,?,?);";
- String nomedepartamento=doNome.getText();
- String local=doLocal.getText();
- int numerodepartamento=Integer.valueOf(doDepartamento.getText());
- try {
- PreparedStatement pst=con.prepareStatement(sql);
- pst.setInt(1,numerodepartamento);
- pst.setString(2,nomedepartamento);
- pst.setString(3,local);
- pst.execute();
- pst.close();
- System.out.println("Gravado!");
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- try {
- con=ligar.Coneccion.getConnection();
- } catch (SQLException ex) {
- Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Add Comment
Please, Sign In to add comment