连接类:
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)
org.sqlite.JDBC.connect(JDBC.java:87)
java.sql.DriverManager.getConnection(未知来源)at java.sql.DriverManager.getConnection(未知来源)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(未知来源)at java.awt.Component.processFocusEvent(未知来源)at java.awt.Component.processEvent(未知来源)at java.awt.Container.processEvent(未知来源)at java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.KeyboardFocusManager.redispatchEvent(未知来源)at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知 来自java.awt.DefaultKeyboardFocusManager.dispatchEvent(未知 来自)java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.EventQueue.dispatchEventImpl(未知来源)at java.awt.EventQueue.access $ 200(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来源)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue $ 4.run(未知来源)at java.awt.EventQueue $ 4.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue.dispatchEvent(未知来源)at java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来自java.awt.EventDispatchThread.pumpEvents(未知来源) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.run(未知来源)线程中的异常 " 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(未知来源)at java.awt.Component.processFocusEvent(未知来源)at java.awt.Component.processEvent(未知来源)at java.awt.Container.processEvent(未知来源)at java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.KeyboardFocusManager.redispatchEvent(未知来源)at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知 来自java.awt.DefaultKeyboardFocusManager.dispatchEvent(未知 来自)java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.EventQueue.dispatchEventImpl(未知来源)at java.awt.EventQueue.access $ 200(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来源)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue $ 4.run(未知来源)at java.awt.EventQueue $ 4.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue.dispatchEvent(未知来源)at java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来自java.awt.EventDispatchThread.pumpEvents(未知来源) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.run(未知来源)
whyyyy我无法理解,我在另一个.java文件中有另一个类与sql lite数据库建立连接,但是在这一刻它没有被调用,为什么日食会出现这个错误?!?!!从他认为的地方"首先,我使用org.mysql工作正常,现在在第二个我将尝试使用org.sqlite"
:|
答案 0 :(得分: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();
或者您可以使用数据库库为您处理