Вопрос

Connection Class:

public class ConectaSiscart { 
        static Connection connection = null;
        Statement stm = null;
        static String serverName = "192.168.0.222";    //caminho do servidor do BD  
        static String mydatabase ="risabel";        //nome do seu banco de dados  
        static String url = "jdbc:mysql://" + serverName + "/" + mydatabase;  
        static String username = "siscart";        //nome de um usuário de seu BD        
        static String password = "progsis";      //sua senha de acesso  





        public static Connection getConexao() {
            try {  
                // Carregando o JDBC Driver padrão  
                Class.forName("com.mysql.jdbc.Driver");  
                // Configurando a nossa conexão com um banco de dados//  


                ResultSet results = null;
               // connection = DriverManager.getConnection("jdbc:mysql://192.168.0.222/risabel?user=siscart&password=progsis");
                connection = DriverManager.getConnection(url, username, password);  
                System.out.println("Connection é " + connection);
                return connection;  
            }  catch (ClassNotFoundException e) {  //Driver não encontrado  
                System.out.println("O driver especificado nao foi encontrado.");  
                return null;  
            } catch (SQLException e) {  
                //Não conseguindo se conectar ao banco  
                System.out.println("Nao foi possivel conectar ao Banco de Dados.");  
                e.printStackTrace();
                return null;  

            }
}  

}

Method to do the connection, this method is attached a mouse lostfocus event, and return the item from database that i want:

private void puxaemailsiscart()  {
                ConectaSiscart puxaemail = new ConectaSiscart();
                Connection conectadomysql = ConectaSiscart.getConexao();
                String servico = null;
                    if(cboxservico.getSelectedItem() == "Registro de Imóveis") {
                    servico = "reg_cab";
                }
                Statement stm = null;
                ResultSet results = null;  
                 try {
                    stm = conectadomysql.createStatement ();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


                                try {
                    System.out.println();
                    results = stm.executeQuery ("SELECT * FROM  " + servico + " WHERE protocolo =" + tfProtocolo.getText());
                    if(results.next()) {

                    tfEmailParte.setText(results.getString("Email").toString());

                    } else { 
                        System.out.println("Protocolo nao encontrado");


                    }
                    results.close();
                    stm.close();






                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                } finally {
    System.out.println("Closing the connection.");
    if (conectadomysql != null) try { conectadomysql.close(); } catch (SQLException ignore) {}
}



            }
        });

but for some reason, in second try, it change the org.mysql, to org.sqlite.JDBC

java.sql.SQLException: invalid database address: jdbc:mysql://192.168.0.222/risabel at org.sqlite.JDBC.createConnection(JDBC.java:110) at

org.sqlite.JDBC.connect(JDBC.java:87) at

java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at ConectaSiscart.getConexao(ConectaSiscart.java:30) at telaprincipal$3.puxaemailsiscart(telaprincipal.java:359) at telaprincipal$3.focusLost(telaprincipal.java:350) at java.awt.AWTEventMulticaster.focusLost(Unknown Source) at java.awt.Component.processFocusEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at telaprincipal$3.puxaemailsiscart(telaprincipal.java:367) at telaprincipal$3.focusLost(telaprincipal.java:350) at java.awt.AWTEventMulticaster.focusLost(Unknown Source) at java.awt.Component.processFocusEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

whyyyy i cant understand, i have another class in another .java file to do a connection with a sql lite database, but in this moment its not called in any moment, why the eclipse do this mistake?!?!! from where he thinks "First i worked fine with the org.mysql, now in the second i will try with the org.sqlite"

:|

Это было полезно?

Решение

I think I figured out what's happening. When you call DriverManager.getConnection, it will try all the available drivers one by one until one succeeds. When it calls connect on a driver, it can return null if it's the wrong driver, throw an exception if it failed, or return a Connection object if it succeeds. When all the drivers fail, DriverManager throws the first exception it got, if any, or else creates and throws a "No suitable driver found" exception.

This is probably what's happening in your case:
- DriverManager is trying the sqlite driver first; it is supposed to return null because it's the wrong driver for the url, but instead it is throwing an exception (this is a bug in the sqlite driver implementation!)
- next, DriverManager is trying the mysql driver, which fails to connect for some other reason (for example, server not running, or wrong password)
- DriverManager sees that all the drivers failed to connect, so it throws the first exception it got, which is the one from sqlite (due to that implementation bug)

What you can do:
- Remove the sqlite driver temporarily from the classpath to solve the mysql connection problem
- Better, use a DataSource instead of DriverManager. Mysql example:

MysqlDataSource ds = new MysqlDataSource();
ds.setServerName(serverName);
ds.setDatabaseName(mydatabase);
ds.setUser(username);
ds.setPassword(password);
connection = ds.getConnection();

Or you can use a database library to handle it for you

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top