database connection

Asked

Viewed 50 times

-1

Hello, I’m having a problem with mysql_connector, because it’s not returning me the connection string with DB, even though I’ve already included it in the Netbeans classpath, and have done other scripts where it brings me the connection string. In this case, as follows in the code below, I made a Try catch, with the user variables ,password and url, but even so, it does not return me the connection string as normally it should. Below is the connection class and the script that worked,

Connection class:

package br.com.mySystem.dal;

import java.sql.*;


/**
 *
 * @author Daiana
 */
public class ModuloConexao {

    public static Connection conector(){
        java.sql.Connection conexao = null;

        String driver = "com.mysql.jdbc.Driver";
        String url ="jdbc:mysql://localhost:3308/project";
        String user ="root";
        String pass ="";

        try {
           Class.forName(driver);
           conexao = DriverManager.getConnection(url,user,pass );
           return conexao;
        } catch (ClassNotFoundException | SQLException e) {
            return null;
        }


    }

}

Main class:

package br.com.mySysttem.telas;
import java.sql.*;
import br.com.mySystem.dal.ModuloConexao;

/**
 *
 * @author Daiana
 */
public class TelaLogin extends javax.swing.JFrame {
 Connection conexao = null;
 PreparedStatement pst = null;
 ResultSet rs = null;

    /**
     * Creates new form TelaLogin
     */
    public TelaLogin() {
        initComponents();
        conexao = ModuloConexao.conector();
        System.out.println(conexao);
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        btn = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Publi-Systems");
        setResizable(false);

        jLabel1.setText("Usuario");

        jLabel2.setText("Senha");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        btn.setText("Login");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btn)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaLogin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;

Random script that worked:


package mytest;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daiana
 */
public class Mytest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String classNome = "com.mysql.jdbc.Driver";
        try{
       Class.forName(classNome);
            System.out.println("Driver carregado com sucesso");
        } catch(ClassNotFoundException ex){
            System.out.println("Driver falhou em carregar");
            System.out.println(ex.getMessage());
            }
    }

}

1 answer

0

In the test that worked, you are not opening the connection, just loading the driver. In the main script where the error is occurring, we can say that the driver is being loaded correctly in Class.forName (driver); however when the connection is actually being made in connection = DriverManager.getConnection (url, user, pass);an exception is being thrown, but you are ignoring the error and returning null.

First, it would be better to put one e.printStackTrace () before returning null in the Try.. catch of the connector method, so that you can clearly see what the problem is, which may be what address localhost:3308 is wrong, or the database project does not exist or password cannot be empty, or simply the bank is offline.

  • Hi, sorry for the delay in feedback, but I had found out what the problem was. What was joking me for the exception was a strange bug that mysql had with the time zone, finally, it just had to pass a conditional inside the connection string to establish that the time zone package equals UTC.

  • It was really a strange bug

Browser other questions tagged

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