Advertisement
Guest User

Untitled

a guest
May 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package agenda;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author labsim
  18. */
  19. public class MyConnection {
  20.  
  21. private Connection con;
  22. Statement stmt;
  23. String url = "jdbc:mysql://127.0.0.1:3306/agenda";
  24. String user = "root";
  25. String password = "aluno@etep";
  26. String nome = "Marcos";
  27. String area = "11";
  28. String num = "2562-5541";
  29.  
  30. public MyConnection() throws ClassNotFoundException {
  31.  
  32. try {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. con = DriverManager.getConnection(url, user, password);
  35. stmt = con.createStatement();
  36. System.out.println("Conectado com sucesso!");
  37.  
  38. } catch (SQLException ex) {
  39. Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
  40. }
  41.  
  42. try {
  43. stmt.execute("INSERT INTO contato VALUES (NULL,'" + nome + "','" + area + "','" + num + "');");
  44. } catch (SQLException e) {
  45. System.out.println("Error: " + e.getMessage());
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement