Guest User

Untitled

a guest
Jan 10th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public class Coneccion {
  2. private static final String DRIVER = "com.mysql.jbdc.Driver";
  3. private static final String URL = "jbdc:mysql://localhost:3306/DBProject";
  4. private static final String USER = "root";
  5. private static final String PASS = "";
  6. public static Connection getConnection() throws SQLException{
  7.  
  8. try {
  9. Class.forName(DRIVER);
  10. return (Connection) DriverManager.getConnection(URL, USER, PASS);
  11. } catch (ClassNotFoundException ex) {
  12. Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
  13. }
  14. return null;
  15. }
  16.  
  17. public static void closeConnection(Connection con){
  18. if (con!=null) {
  19. try {
  20. con.close();
  21. } catch (SQLException ex) {
  22. Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24.  
  25. }
  26. }
  27. public static void closeConnection(Connection con,PreparedStatement stmt){
  28. closeConnection(con);
  29. if (stmt!=null) {
  30. try {
  31. stmt.close();
  32. } catch (SQLException ex) {
  33. Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
  34. }
  35. }
  36. }
  37. public static void closeConnection(Connection con,PreparedStatement stmt,ResultSet rs){
  38. closeConnection(con,stmt);
  39. if (rs!=null) {
  40. try {
  41. rs.close();
  42. } catch (SQLException ex) {
  43. Logger.getLogger(Coneccion.class.getName()).log(Level.SEVERE, null, ex);
  44. }
  45. }
  46. }
  47.  
  48. public class FXMLDocumentController implements Initializable {
  49.  
  50. @FXML private TextField doDepartamento;
  51. @FXML private TextField doNome;
  52. @FXML private TextField doLocal;
  53. @FXML private TableView t;
  54. @FXML private TableColumn celNumero;
  55. @FXML private TableColumn celLocal;
  56. @FXML private TableColumn celNome;
  57. @FXML private Button bclick;
  58. private Connection con=null;
  59. @FXML
  60. private void handleButtonAction(ActionEvent event) throws SQLException {
  61. String sql="INSERT INTO Departamento(DepNum, Nome, Local)Values (?,?,?);";
  62. String nomedepartamento=doNome.getText();
  63. String local=doLocal.getText();
  64. int numerodepartamento=Integer.valueOf(doDepartamento.getText());
  65. try {
  66.  
  67. PreparedStatement pst=con.prepareStatement(sql);
  68. pst.setInt(1,numerodepartamento);
  69. pst.setString(2,nomedepartamento);
  70. pst.setString(3,local);
  71. pst.execute();
  72. pst.close();
  73. System.out.println("Gravado!");
  74. con.close();
  75.  
  76. } catch (SQLException ex) {
  77. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  78. }
  79.  
  80.  
  81. }
  82.  
  83. @Override
  84. public void initialize(URL url, ResourceBundle rb) {
  85. try {
  86. con=ligar.Coneccion.getConnection();
  87. } catch (SQLException ex) {
  88. Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  89. }
  90.  
  91. }
Add Comment
Please, Sign In to add comment