连接类别:
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:无效的数据库地址: org.sqlite.JDBC.createConnection(JDBC.java:110)处的* jdbc:mysql://192.168.0.222/risabel * org.sqlite.JDBC.connect(JDBC.java:87)在 java.sql.DriverManager.getConnection(未知源)(位于ConectaSiscart.getConexao(ConectaSiscart.java:30)在telaprincipal $ 3处的java.sql.DriverManager.getConnection(未知源).puxaemailsiscart(telaprincipal.java:359)在telaprincipal $ 3处。 focusLost(telaprincipal.java:350),java.awt.AWTEventMulticaster.focusLost(未知源),java.awt.Component.processFocusEvent(未知源),java.awt.Component.processEvent(未知源),java.awt.Container java.awt.Component.dispatchEventImpl的.processEvent(未知源)java.awt.Component.dispatchEventImpl的java.awt.Component.dispatchEventImpl(java.awt.KeyboardFocusManager.redispatchEvent的Unknown Source) (java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)中的(未知源)。java.awt.Component.dispatchEventImpl的awt.DefaultKeyboardFocusManager.dispatchEvent(未知源)java.awt.Container.dispatchEventImpl的java.awt.Component.dispatchEventImpl(未知源) EventQueue.dispatchEventImpl(来源未知)java.awt.EventQueue.access $ 200(来源未知)java.awt.EventQueue $ 3.run(来源未知)java.awt.EventQueue $ 3.run(来源未知)java.security Java的.AccessController.doPrivileged(本机方法),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源),java.awt.EventQueue $ 4.run(未知源),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)。 java.security.AccessController上的awt.EventQueue $ 4.run(未知源)。java.security.ProtectionDomain $ 1.doIntersectionPrivilege(本地方法)处的doPrivileged(本机方法)java.awt.EventQueue.dispatchEvent(未知源)处的java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)处的java.awt.EventDispatchThread.pumpEventsForFilters(未知源) (未知源)java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.run(未知源)源)线程“ AWT-EventQueue-0”中的异常telaprincipal $ 3.puxaemailsiscart(telaprincipal.java:367)处telaprincipal $ 3.focusLost(telaprincipal.java:350)处java.awt.AWTEventMulticaster.focusLost(未知来源),网址为java.awt。Component.processFocusEvent(未知源),java.awt.Component.processEvent(未知源),java.awt.Container.processEvent(未知源),java.awt.Component.dispatchEventImpl(java.awt.Container,未知源)。 java.awt.Component.dispatchEvent中的dispatchEventImpl(Unknown Source)(java.awt.KeyboardFocusManager.redispatchEvent中的java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)中的java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) java.awt.Component.dispatchEventImpl上的未知源(java.awt.Container.dispatchEventImpl上的java.awt.Component.dispatchEventImpl的未知源(java.awt.EventQueue.dispatchEventImpl上的未知源) ),java.awt.EventQueue.access $ 200(未知源)。awt.EventQueue $ 3.run(未知源)在java.awt.EventQueue $ 3.run(未知源)在java.security.AccessController.doPrivileged(本机方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源) .security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)在java.awt.EventQueue $ 4.run(未知源)在java.awt.EventQueue $ 4.run(在java.security.AccessController.doPrivileged(本机方法) java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)java.awt.EventQueue.dispatchEvent(未知源)java.awt.EventDispatchThread.pumpEventsForFilter(未知源)java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源) java中的.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)。java.awt.EventDispatchThread.pumpEvents(未知源)处的awt.EventDispatchThread.pumpEvents(未知源)。java.awt.EventDispatchThread.run中的awt.EventDispatchThread.pumpEvents(未知源)
java.sql.SQLException:无效的数据库地址: org.sqlite.JDBC.createConnection(JDBC.java:110)处的* jdbc:mysql://192.168.0.222/risabel *
org.sqlite.JDBC.connect(JDBC.java:87)在
java.sql.DriverManager.getConnection(未知源)(位于ConectaSiscart.getConexao(ConectaSiscart.java:30)在telaprincipal $ 3处的java.sql.DriverManager.getConnection(未知源).puxaemailsiscart(telaprincipal.java:359)在telaprincipal $ 3处。 focusLost(telaprincipal.java:350),java.awt.AWTEventMulticaster.focusLost(未知源),java.awt.Component.processFocusEvent(未知源),java.awt.Component.processEvent(未知源),java.awt.Container java.awt.Component.dispatchEventImpl的.processEvent(未知源)java.awt.Component.dispatchEventImpl的java.awt.Component.dispatchEventImpl(java.awt.KeyboardFocusManager.redispatchEvent的Unknown Source) (java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)中的(未知源)。java.awt.Component.dispatchEventImpl的awt.DefaultKeyboardFocusManager.dispatchEvent(未知源)java.awt.Container.dispatchEventImpl的java.awt.Component.dispatchEventImpl(未知源) EventQueue.dispatchEventImpl(来源未知)java.awt.EventQueue.access $ 200(来源未知)java.awt.EventQueue $ 3.run(来源未知)java.awt.EventQueue $ 3.run(来源未知)java.security Java的.AccessController.doPrivileged(本机方法),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源),java.awt.EventQueue $ 4.run(未知源),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)。 java.security.AccessController上的awt.EventQueue $ 4.run(未知源)。java.security.ProtectionDomain $ 1.doIntersectionPrivilege(本地方法)处的doPrivileged(本机方法)java.awt.EventQueue.dispatchEvent(未知源)处的java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)处的java.awt.EventDispatchThread.pumpEventsForFilters(未知源) (未知源)java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.pumpEvents(未知源)java.awt.EventDispatchThread.run(未知源)源)线程“ AWT-EventQueue-0”中的异常telaprincipal $ 3.puxaemailsiscart(telaprincipal.java:367)处telaprincipal $ 3.focusLost(telaprincipal.java:350)处java.awt.AWTEventMulticaster.focusLost(未知来源),网址为java.awt。Component.processFocusEvent(未知源),java.awt.Component.processEvent(未知源),java.awt.Container.processEvent(未知源),java.awt.Component.dispatchEventImpl(java.awt.Container,未知源)。 java.awt.Component.dispatchEvent中的dispatchEventImpl(Unknown Source)(java.awt.KeyboardFocusManager.redispatchEvent中的java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)中的java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) java.awt.Component.dispatchEventImpl上的未知源(java.awt.Container.dispatchEventImpl上的java.awt.Component.dispatchEventImpl的未知源(java.awt.EventQueue.dispatchEventImpl上的未知源) ),java.awt.EventQueue.access $ 200(未知源)。awt.EventQueue $ 3.run(未知源)在java.awt.EventQueue $ 3.run(未知源)在java.security.AccessController.doPrivileged(本机方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源) .security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)在java.awt.EventQueue $ 4.run(未知源)在java.awt.EventQueue $ 4.run(在java.security.AccessController.doPrivileged(本机方法) java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)java.awt.EventQueue.dispatchEvent(未知源)java.awt.EventDispatchThread.pumpEventsForFilter(未知源)java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源) java中的.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)。java.awt.EventDispatchThread.pumpEvents(未知源)处的awt.EventDispatchThread.pumpEvents(未知源)。java.awt.EventDispatchThread.run中的awt.EventDispatchThread.pumpEvents(未知源)
whyyyy我听不懂,我在另一个.java文件中有另一个类来与sql lite数据库建立连接,但是此刻它在任何时候都没有被调用,为什么eclipse会犯此错误?!他认为:“首先,我在org.mysql上工作良好,现在在第二个中,我将尝试使用org.sqlite”。
:|
我想我知道发生了什么事。当您调用DriverManager.getConnection时,它将一一尝试所有可用的驱动程序,直到一个成功。在驱动程序上调用connect时,如果驱动程序错误,则可以返回null;如果失败,则引发异常;如果成功,则返回Connection对象。当所有驱动程序均发生故障时,DriverManager会抛出它获得的第一个异常(如果有),否则将创建并引发“找不到合适的驱动程序”异常。
这可能是您的情况: -DriverManager首先尝试sqlite驱动程序;它应该返回null,因为它是错误的url驱动程序,但是它引发了异常(这是sqlite驱动程序实现中的错误!) -接下来,DriverManager正在尝试mysql驱动程序,该驱动程序无法连接某些其他原因(例如,服务器未运行或密码错误) -DriverManager看到所有驱动程序均无法连接,因此抛出了第一个异常,这是来自sqlite的异常(由于该实现错误)
您可以做什么: -暂时从类路径中删除sqlite驱动程序,以解决mysql连接问题 -更好地,使用数据源而不是DriverManager。MySQL的例子:
MysqlDataSource ds = new MysqlDataSource(); ds.setServerName(serverName); ds.setDatabaseName(mydatabase); ds.setUser(username); ds.setPassword(password); connection = ds.getConnection();
或者您可以使用数据库库为您处理