Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Contact() {
- }
- public Contact (String name, String email, String address, int phone) {
- this.name=name;
- this.email=email;
- this.address=address;
- this.telephoneNumber=phone;
- }
- //Getters e Setters
- ...
- }
- public class ContactDAO {
- private Connection con;
- public Connection getConnection() {
- return con;
- }
- public ContactDAO() {
- ConnectionFactory conF = new ConnectionFactory();
- con=conF.getBDConnection();
- }
- public void addClient(Contact contact) {
- String sql= "INSERT INTO contatos" + "(nome,email,endereco,telefone)" +
- "values (?,?,?,?)";
- try {
- PreparedStatement stmt= con.prepareStatement(sql);
- stmt.setString(1,contact.getName());
- stmt.setString(2,contact.getEmail());
- stmt.setString(3,contact.getAddress());
- stmt.setInt(4,contact.getTelephoneNumber());
- stmt.executeUpdate();
- stmt.close();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- } finally {
- try {
- con.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public class ConnectionFactory {
- public Connection getBDConnection() {
- Connection con = null;
- System.out.println("Testing access to BD MySQLn");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", "");
- System.out.println("Connection successful!!!");
- } catch (ClassNotFoundException cnfe){
- cnfe.printStackTrace();
- System.out.println("ClassNotFoundException");
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- System.out.println("SQLException");
- }
- return con;
- }
- public void closeConnection(Connection con) {
- try {
- con.close();
- System.out.println("n Connection successfully close!!!");
- }
- catch (SQLException sqle) {
- System.out.println("SQLException");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement