Guest User

Untitled

a guest
Nov 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. try {
  2.  
  3. PreparedStatement stm;
  4. stm = connex.getConexao().prepareStatement("INSERT INTO onibus "
  5. + "(ano, chassi, placa, numeroOnibus, tipoVeiculo) "
  6. + "VALUES (?,?,?,?,?);");
  7.  
  8. stm.setInt(1, onibus.getAno());
  9. stm.setString(2, onibus.getChassi());
  10. stm.setString(3, onibus.getPlaca());
  11. stm.setInt(4, onibus.getNumeroOnibus());
  12. stm.setString(5, onibus.getTipoVeiculo());
  13.  
  14. stm.execute();
  15.  
  16. JOptionPane.showMessageDialog(null, "Onibus Inserido Com Sucesso!");
  17.  
  18. } catch (HeadlessException | SQLException e) {
  19.  
  20. JOptionPane.showMessageDialog(null, "Erro ao inserir Onibus!nERRO");
  21. e.printStackTrace();
  22.  
  23. } finally {
  24. connex.desconecta();
  25. }
  26.  
  27.  
  28. }
  29.  
  30. public Statement stm;
  31. public ResultSet rst;
  32. private final String driver = "org.postgresql.Driver";
  33. private final String url = "jdbc:postgresql://localhost:5432/CONI-VP";
  34. private final String usuario = "postgres";
  35. private final String senha = "root";
  36. Connection conexao = null;
  37.  
  38. public Conexao(){
  39. try {
  40. Class.forName(driver);
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. public Conexao(Statement stm, ResultSet rst) {
  47. this.stm = stm;
  48. this.rst = rst;
  49. }
  50.  
  51. public Connection getConexao(){
  52. if(conexao == null){
  53. try {
  54. conexao = DriverManager.getConnection(url, usuario, senha);
  55. conexao.setAutoCommit(false);
  56. } catch (SQLException e) {
  57. }
  58. }
  59. return conexao;
  60. }
  61.  
  62. public void desconecta(){
  63. try {
  64. conexao.close();
  65. } catch (SQLException e) {
  66. }
  67. conexao = null;
  68. }
  69.  
  70.  
  71. }
  72.  
  73. o.setAno(Integer.parseInt(jFormatted_ano.getText()));
  74. o.setNumeroOnibus(Integer.parseInt(jFormatted_numOnibus.getText()));
  75. o.setChassi(jTextField_tipo.getText());
  76. o.setPlaca(jTextField_placa.getText());
  77. o.setTipoVeiculo(jTextField_tipo.getText());
  78. }
  79.  
  80. o.getCodOnibus();
  81. o.setAno(Integer.parseInt(jFormatted_ano.getText()));
  82. o.setNumeroOnibus(Integer.parseInt(jFormatted_numOnibus.getText()));
  83. o.setChassi(jTextField_chassi1.getText());
  84. o.setPlaca(jTextField_placa.getText());
  85. o.setTipoVeiculo(jTextField_tipo.getText());;
  86. dao.inserirOnibus(o);
  87.  
  88. jFormatted_ano.setText("");
  89. jFormatted_numOnibus.setText("");
  90. jTextField_tipo.setText("");
  91. jTextField_placa.setText("");
  92. jTextField_chassi1.setText("");
Add Comment
Please, Sign In to add comment