自动将org.mysql更改为org.sqlite
连接等级:
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)at
位于telaprincipal $ 3.puxaemailsiscart(telaprincipal.java:359)上的ConectaSiscart.getConexao(ConectaSiscart.java:30)上的java.sql.DriverManager.getConnection(未知源)的java.sql.DriverManager.getConnection(未知源)。 java.awt.Component.processEvent(Unknown Source)上java.awt.Container的java.awt.Component.processFocusEvent(Unknown Source) java.awt.Component.dispatchEventImpl上的java.awt.Component.dispatchEventImpl(Unknown Source)上的java.proc.processEvent(Unknown Source)java.awt.Component.dispatchEvent(Unknown Source)上的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源)在java.awt.Component。 java.awt.EventQueue上的java.awt.EventQueue.dispatchEventImpl(Unknown Source)上的dispatchEvent(Unknown Source)java.awt.EventQueue上的$ 200(Unknown Source)java.awt.EventQueue上的$ 3.run(Unknown Source)$ 3 java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native Method)上的.run(未知源)java.security.ProtectionDomain上的$ 1.doIntersectionPrivilege(Unknown Source)java.awt.EventQueue上的$ 1.doIntersectionPrivilege(Unknown Source) java.awt.EventQueue上的$ 4.run(Unknown Source)在java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native Method)处获得$ 4.run(Unknown Source)。java.awt处的$ 1.doIntersectionPrivilege(Unknown Source)。 java.awt.EventDispatchThread的java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)上的java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)上的java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)的EventQueue.dispatchEvent(Unknown Source) pumpEvents(未知源)在java.a wt.EventDispatchThread.pumpEvents(Unknown Source)java.awt.EventDispatchThread.run(Unknown Source)线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException在telaprincipal $ 3.puxaemailsiscart(telaprincipal.java:367)at telaprincipal java.awt.Component.processEvent(Unknown Source)上的java.awt.Component.processFocusEvent(Unknown Source)在java.awt中的java.awt.AWTEventMulticaster.focusLost(未知源)中的$ 3.focusLost(telaprincipal.java:350) .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 java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)位于java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)位于java.awt.Component.dispatchEventImpl(Unknown Source)位于java.awt.Container.dispatchEventImpl (未知源)在java.awt。 java.awt.EventQueue.access上的java.awt.EventQueue.dispatchEventImpl(Unknown Source)上的Component.dispatchEvent(Unknown Source)java.awt.EventQueue上的$ 200(Unknown Source)java.awt上的$ 3.run(Unknown Source)。在java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native方法)处使用$ 3.run(未知源)。java.security.ProtectionDomain上的$ 1.doIntersectionPrivilege(Unknown Source)java.awt上的$ 1.doIntersectionPrivilege(Unknown Source) .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。在java.awt中的java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)在java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)上的java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)上的awt.EventQueue.dispatchEvent(Unknown Source)。 EventDispatchThread.pumpEvents(未知来源) 在java.awt.EventDispatchThread.run(Unknown Source)处的java.awt.EventDispatchThread.pumpEvents(Unknown Source)
为什么我不能理解,我有另一个.java文件中的另一个类与sql lite数据库建立连接,但是在这一刻它不会在任何时候调用,为什么日食会犯这个错误?!?!! 从他认为的地方开始“首先,我使用org.mysql工作良好,现在在第二部分中,我将尝试使用org.sqlite”
:|
我想我知道发生了什么。 当你调用DriverManager.getConnection时,它会逐个尝试所有可用的驱动程序,直到成功。 当它调用驱动程序上的连接时,如果它是错误的驱动程序,则可以返回null,如果失败则返回异常,如果成功则返回Connection对象。 当所有驱动程序失败时,DriverManager会抛出第一个异常(如果有),否则会创建并抛出“找不到合适的驱动程序”异常。
这可能是你的情况发生了什么:
- DriverManager首先尝试sqlite驱动程序; 它应该返回null,因为它是url的错误驱动程序,但是它会抛出一个异常(这是sqlite驱动程序实现中的一个错误!)
- 接下来,DriverManager正在尝试mysql驱动程序,该驱动程序由于某些其他原因(例如服务器未运行或密码错误)而无法连接,
- DriverManager发现所有的驱动程序都无法连接,所以它会抛出第一个异常,这是sqlite中的异常(由于该实现的bug)
你可以做什么:
- 从类路径中暂时删除sqlite驱动程序来解决mysql连接问题
- 最好使用一个DataSource而不是DriverManager。 Mysql例子:
MysqlDataSource ds = new MysqlDataSource();
ds.setServerName(serverName);
ds.setDatabaseName(mydatabase);
ds.setUser(username);
ds.setPassword(password);
connection = ds.getConnection();
或者您可以使用数据库库为您处理它
链接地址: http://www.djcxy.com/p/73433.html上一篇: automatically change org.mysql to org.sqlite
下一篇: MouseAdapter class