连接类:
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”
:|