java database connection

Asked

Viewed 39 times

-1

Well I did a class to make the connection with fingered bank:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package semeq;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SpiriT
 */
public class ConnectionFactory {

private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/helpsemeq";
private static final String USER = "root";
private static final String PASS = "";

public static Connection getConnection(){

    try {
        Class.forName(DRIVER);
        return DriverManager.getConnection(URL, USER, PASS);     

    } catch (ClassNotFoundException | SQLException ex) {
       throw new RuntimeException("Erro na conexão: ",ex);
    }
}

public static void closeConnection(Connection con){

        try {
            if(con != null){
            con.close();
        } 

        }catch (SQLException ex) {
             Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


public static void closeConnection(Connection con, PreparedStatement stmt){

    closeConnection(con);

        try {
            if(stmt != null){
            stmt.close();
        } 

        }catch (SQLException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){

    closeConnection(con, stmt);

        try {
            if(rs != null){
            rs.close();
        } 

        }catch (SQLException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

and created a jpanel in netbeans

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package semeq;

/**
 *
 * @author SpiriT
 */
public class NewJPanel extends javax.swing.JPanel {

    /**
     * Creates new form NewJPanel
     */
    public NewJPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

but this j panel already inherits the swing how would I call this database connection to this jpanel, so that I can display data that is in the database?

It would be better to use by interface or composition/aggregation?

I’m new to java.

1 answer

0

well Oce already has its panel, now just know what Oce wants to return to its panel.. for example, the common use is the DAOS, they return something related to their own class.

for example>> Customer Class and Customer Class. the customer class has its attributes and the customer class would be responsible for making the CRUD.

Since you are using Netbeans I recommend using the famous "drag and drop" and mounting your Jframe and then worrying about popular it learning more about the DAO classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.