0

连接类:

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;  

            }
}  

}

进行连接的方法,该方法附加了一个鼠标 lostfocus 事件,并从数据库中返回我想要的项目:

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) {}
}



            }
        });

但由于某种原因,在第二次尝试中,它将 org.mysql 更改为 org.sqlite.JDBC

java.sql.SQLException:无效的数据库地址:jdbc:mysql://192.168.0.222/risabel at org.sqlite.JDBC.createConnection(JDBC.java:110) at

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

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 (未知来源)在 java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知来源)在 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. java.security.AccessController 上的 awt.EventQueue$4.run(Unknown Source)。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 (未知来源)在 java.awt.EventDispatchThread.pumpEvents(未知来源)在 java.awt.EventDispatchThread.pumpEvents(未知来源)在 java.awt.EventDispatchThread.run(未知来源)在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)来源)线程“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(未知来源)在 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.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) ) 在 java.awt.EventQueue.access$200(Unknown Source) 在 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)

为什么我无法理解,我在另一个 .java 文件中有另一个类来与 sql lite 数据库建立连接,但是此时它没有被调用,为什么 eclipse 会犯这个错误?!?!!他认为“首先我在 org.mysql 上工作得很好,现在在第二个我将尝试使用 org.sqlite”

:|

4

1 回答 1

2

我想我知道发生了什么。当你调用 DriverManager.getConnection 时,它会一一尝试所有可用的驱动,直到一个成功。当它在驱动程序上调用 connect 时,如果它是错误的驱动程序,它可以返回 null,如果它失败则抛出异常,或者如果它成功则返回一个 Connection 对象。当所有驱动程序都失败时,DriverManager 会抛出它得到的第一个异常(如果有),否则会创建并抛出“未找到合适的驱动程序”异常。

这可能是您的情况:
- DriverManager 首先尝试 sqlite 驱动程序;它应该返回 null 因为它是错误的 url 驱动程序,但它却抛出了异常(这是 sqlite 驱动程序实现中的一个错误!)
- 接下来,DriverManager 正在尝试 mysql 驱动程序,它无法连接一些其他原因(例如,服务器未运行或密码错误)
- DriverManager 看到所有驱动程序都无法连接,因此它抛出它得到的第一个异常,这是来自 sqlite 的异常(由于那个实现错误)

您可以做什么:
- 暂时从类路径中删除 sqlite 驱动程序以解决 mysql 连接问题
- 更好的是,使用 DataSource 而不是 DriverManager。mysql示例:

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

或者您可以使用数据库库为您处理它

于 2013-09-11T18:35:48.413 回答