Erro qd compilo o PreparedStatement

2 respostas
J

[b]Deem uma olhada nesse meu método. quando compilo ele gera erro. verifiquem:

código:

public void Buscar(){
		ResultSet res = null;
		try{
			Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/roteiros","root","root");
			PreparedStatement sql = conexao.prepareStatement("SELECT * FROM esp WHERE nome = ?");
			int j = 0;
			espe = (String)especialidade.getSelectedItem();
			while(espe.charAt(j) != ' '){
				j++;
			}
			espe = espe.substring(0,j);
			sql = conexao.prepareStatement("SELECT * FROM esp WHERE nome ORDER BY cod");
			sql.setInt(1, Integer.valueOf(espe));
			
			if (especialidade.getSelectedItem().equals("")){
				JOptionPane.showMessageDialog(null,"Favor escolha a especialidade.","Aviso",1);
			}
			res = sql.executeQuery();
			int i = 0;
			while (res.next()){
				i++;
			}
			if (i > 1){
				res.last();
				int num_linhas = res.getRow();
				String [][] dados = new String [num_linhas][2];
				String [] colunas = {
					"Código", "Nome"
				};
	            int linha = 0;	
				res.first();
				do{
					dados[linha][0] = res.getString("cod");
					dados[linha][1] = (res.getString("nome") + " - " + res.getString("esp"));
					linha++;
				}
				while (res.next());
				res.close();
				DefaultTableModel model = new DefaultTableModel(dados, colunas);
				tabela = new JTable(model);
				exibeTabela();
			}
			else if (i == 1){
				res.first();
				exibeInfo();
			}
			else{
				JOptionPane.showMessageDialog(null,"Não existem registros com a informação especificada","Erro",0);
				Especialidade esp = new Especialidade();
				esp.digitaInfo();
			}
		}
		catch (SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"Não foi possível conectar com o banco!","Erro",0);
		}
	}

[color=blue] [/color]
fica dando o seguinte erro;

java.sql.SQLException: No parameters defined during prepareCall()

at com.mysql.jdbc.ServerPreparedStatement.getBinding(ServerPreparedStatement.java:742)

at com.mysql.jdbc.ServerPreparedStatement.setInt(ServerPreparedStatement.java:1705)

at consulta.Especialidade.Buscar(Especialidade.java:210)

at consulta.Especialidade$2.actionPerformed(Especialidade.java:116)

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.plaf.basic.BasicButtonListener.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)

Será se tem como alguem me ajudar?[/b]

2 Respostas

ricardosoares

vc está criando uma instancia de preparedStatement para receber parametro ("…WHRE nome = ?") e depois cria uma outra (com a clausula “ORDER”) sem prever o recebimento de parametros ( com o “?” ).
e, logo depois, vc esta tentando enfiar o parametro ( “sql.setInt(…” ) na instrução.
por isso,
java.sql.SQLException: No parameters defined during prepareCall()
é natural

T

O Ricardo matou o problema…

na linha abaixo
sql = conexao.prepareStatement(“SELECT * FROM esp WHERE nome ORDER BY cod”);

está faltando o valoe de comparção do where. o sql deveria estar assim:
sql = conexao.prepareStatement(“SELECT * FROM esp WHERE nome = ? ORDER BY cod”);

t+

Criado 25 de julho de 2007
Ultima resposta 26 de jul. de 2007
Respostas 2
Participantes 3