Erro - DriveManager.getConnection

5 respostas
B

galera to fazendo uma conexão com o access pra trazer o saldo em um JTextArea,
segue código:

if(e.getSource()== saldo){
			
			String url = "jdbc.odbc:Conta";
			try{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				JOptionPane.showMessageDialog(null,"Conexao estabelecida");
				Connection con = DriverManager.getConnection(url); //linha apresenta erro
				String sql = "SELECT Conta.numero, Conta.agencia, Conta.saldo, Conta.nome FROM Conta WHERE Conta.numero <= 2";
				ResultSet rs;
				Statement stn = con.createStatement();
				rs = stn.executeQuery(sql);
				
				txt.setText("");
				String mos = "Número / ----- / Agencia / ----- / Saldo / ----- / Titular /";
				txt.append(mos);
				
				for(int i = 0; i < 2; i++){
					
					String r = rs.getString("numero");
					String re = rs.getString("agencia");
					String res = rs.getString("saldo");
					String resu = rs.getString("nome");
					
					mos = "\n      "+r +"     " + re + "      " + res + "       " + resu + "   ";
					txt.append(mos);
					rs.next();
				}

Erro:

Erro na conexao
java.sql.SQLException: No suitable driver found for jdbc.odbc:Conta
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at Menu.actionPerformed(Menu.java:83)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(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)

Alguem sabe o que esta errado?

Obrigado

5 Respostas

D

Vc criou esta ponte ODBC chamada conta???

B

Daniel.F:
Vc criou esta ponte ODBC chamada conta???

Sim

Ele cai ali no JOptionPane dizendo que fez a conexão mas depois cai na Exception =/

dzanotto

amigo, pelo que vi sua URL está errada:

String url = "jdbc.odbc:Conta";

O correto seria:

String url = "jdbc:odbc:Conta";

Posta aí se deu certo ou então manda o stacktrace denovo!

B

Obrigado plea resposta amigo,
Agora ele faz até o laço for e depois da erro:

if(e.getSource()== saldo){
			
			String url = "jdbc:odbc:Conta";
			try{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				JOptionPane.showMessageDialog(null,"Conexao estabelecida");
				Connection con = DriverManager.getConnection(url);
				String sql = "SELECT Conta.numero, Conta.agencia, Conta.saldo, Conta.nome FROM Conta WHERE Conta.numero <= 2";
				ResultSet rs;
				Statement stn = con.createStatement();
				rs = stn.executeQuery(sql);
				
				txt.setText("");
				String mos = "Número / ----- / Agencia / ----- / Saldo / ----- / Titular /";
				txt.append(mos);
				
				for(int i = 0; i < 2; i++){
					try{
						int r = rs.getInt(1); //linha com Erro, depois daqui ele caiu na SqlException
						String re = rs.getString(2);
						String res = rs.getString(3);
						String resu = rs.getString(4);
						
						mos = "\n      "+r +"     " + re + "      " + res + "       " + resu + "   ";
						txt.append(mos);
						rs.next();
					}catch(SQLException se){
						JOptionPane.showMessageDialog(null, "Erro ao puxar os indices");
						se.printStackTrace();
					}
					
				}
				
				rs.close();
				
			}catch(ClassNotFoundException cne){
				System.out.println("Erro");
				cne.printStackTrace();
			}catch(Exception ex){
				System.out.println("Erro na conexao");
				ex.printStackTrace();
			}

		}//end function Saldo

Erro:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Estado de cursor inválido
	at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
	at Menu.actionPerformed(Menu.java:95)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(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.SQLException: [Microsoft][ODBC Driver Manager] Estado de cursor inválido
	at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
	at Menu.actionPerformed(Menu.java:95)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(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)

Banco de dados Access:

Desculpe o incomodo mas é que estou tentando aprender sozinho com a ajuda do forum

Obrigado

dzanotto

Rapaz, vc ta misturando tanta coisa aí que fica até complicado de entender… vou te mandar um método que tenho aqui pra que te sirva de exemplo:

public Curso consultarPorID(int idConsultar) throws Exception {
		
		Statement smt = null;
		ResultSet rs = null;
		Curso curso = null;
		
		try {
			smt = Conexao.getConexao().createStatement();
			rs = smt.executeQuery("SELECT * FROM curso WHERE ID_CURSO = '" + idConsultar + "'");
			
			if(rs.next()){
				int id = rs.getInt("ID_CURSO");
				String nome = rs.getString("NOME");
				String nivelExigido = rs.getString("NIVEL_ESCOLAR");
				String valorAVista = rs.getString("VALOR_A_VISTA");
				String valorCadaParcela = rs.getString("VALOR_PARCELADO");
				String dataInicio = rs.getString("DATA_INICIO");
				String dataTermino = rs.getString("DATA_TERMINO");
				String cargaHoraria = rs.getString("CARGA_HORARIA");
				int numParcelas = rs.getInt("QNTD_PARCELAS");
				
				curso = new Curso(id, nome, nivelExigido, valorAVista, valorCadaParcela,
								  dataInicio, dataTermino, cargaHoraria, numParcelas);
			}
			
			return curso;
			
		} catch (SQLException e) {
			throw new SQLException ("Houve algum erro de SQL ao tentar consultar o curso por id!\n" + e.getMessage());
		} catch (Exception e) {
			throw new Exception ("Houve algum erro ao tentar consultar o curso por id!\n" + e.getMessage());
		} finally {
			rs.close();
			smt.close();
		}
		
	}

Qualquer coisa avisa…

Criado 15 de novembro de 2008
Ultima resposta 16 de nov. de 2008
Respostas 5
Participantes 3