Advertisement
Guest User

trabsonrsrs

a guest
Nov 15th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  2. final String DB_URL = "jdbc:sqlserver://localhost:1434;databaseName=UniplacBD;user=sa;password=123"; //
  3.  
  4. Connection conn = null;
  5. Statement stmt = null;
  6. PreparedStatement pstmt = null;
  7.  
  8. try {
  9.  
  10. /**
  11. * ******** INICIO conectar ao banco *********
  12. */
  13. // Registra o driver JDBC e realiza a conexao
  14. Class.forName(JDBC_DRIVER);
  15. conn = DriverManager.getConnection(DB_URL);
  16. /**
  17. * ******** FIM conectar ao banco *********
  18. */
  19.  
  20. /**
  21. * ******** INICIO exemplo insert por prepareStatement *********
  22. */
  23. // cria um preparedStatement
  24. pstmt = conn.prepareStatement("insert into fornecedor (razaoSocial, endereco, complemento, bairro, cidade, estado, cep, telefone, eMail, cnpj) values (?,?,?,?,?,?,?,?,?,?)");
  25. // preenche os valores
  26. pstmt.setString(1, razãosocial.getText());
  27. pstmt.setString(2, endereço.getText());
  28. pstmt.setString(3, complemento.getText());
  29. pstmt.setString(4, bairro.getText());
  30. pstmt.setString(5, cidade.getText());
  31. pstmt.setString(6, (String)estado.getSelectedItem());
  32. pstmt.setString(7, cep.getText());
  33. pstmt.setString(8, telefone.getText());
  34. pstmt.setString(9, email.getText());
  35. pstmt.setString(10, cnpj.getText());
  36. // executa e fecha
  37. pstmt.execute();
  38. pstmt.close();
  39. /**
  40. * ******** FIM exemplo insert por prepareStatement *********
  41. */
  42.  
  43. //fecha a conexão com o banco de dados
  44. conn.close();
  45. } catch (SQLException se) {
  46. se.printStackTrace();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. if (stmt != null) {
  52. stmt.close();
  53. }
  54. } catch (SQLException se2) {
  55. }
  56. try {
  57. if (conn != null) {
  58. conn.close();
  59. }
  60. } catch (SQLException se) {
  61. se.printStackTrace();
  62. }
  63. }
  64.  
  65. // TODO add your handling code here:
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement