Ver Mensaje Individual
  #3 (permalink)  
Antiguo 25/06/2005, 10:42
mfwjava
 
Fecha de Ingreso: junio-2005
Mensajes: 7
Antigüedad: 18 años, 10 meses
Puntos: 0
Obtener int desde un JTextField

Autor: mfwJava

Hola y gracias a todos por el tiempo que le dedican a responder a mi pregunta:

mi consulta es la siguiente :

IDE utilizado : Eclipse 3.0.2
JVM : 1.5.0

Código ejemplo

//--------comienza ----------

package finip;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;


import javax.swing.JFrame;
import javax.swing.JTextField;

public class JT extends JFrame {

//JTextField por donde ingresa el int (campo autonumerico)
static JTextField prub = new JTextField(15);

//método principal
static void JTmet(){

//JFrame de la aplicación
final JFrame tot = new JFrame("Prueba JTfield");
tot.setBounds(10,10,200,300);


//adiciono el Flow al JFrame
tot.getContentPane().setLayout(new FlowLayout());

//JLabel de entrada de datos
final JLabel labelprub = new JLabel("Entrada");

//adiciono al JFrame JLabel y JTextField
tot.add(labelprub);
tot.add(prub);

//JLabel y JFrame de salida
JLabel labelsal = new JLabel("Salida");
final JTextField salida= new JTextField(15);

//Adiciono JLabel y JTextFrame al JFrame principal
tot.add(labelsal);
tot.add(salida);

//JButton que ejecuta consulta
JButton fire = new JButton("Boton");
tot.add(fire);

//le adiciono addActionListener al JButton fire
fire.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

try {

//Comecta driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//Realiza conección
Connection con = DriverManager.getConnection("jdbc:odbc:Sego", "", "");



// create a statement, execute your query
Statement statement = con.createStatement();

/*String gb = prub.getText();
int numero=Integer.parseInt(gb);*/

int k = Integer.parseInt(prub.getText());

//Consulta SQL

ResultSet rs = statement.executeQuery("SELECT Id,Medico_solic FROM Datos where Id = k ");

String sal = new String();

if(rs.next()){

sal = rs.getString("Medico_solic");

}

salida.setText(sal);

rs.close();
con.close();

}catch (SQLException sqle) {
sqle.printStackTrace();
} catch (Exception ed) {

// Other exceptions should be properly caught, but this'll do for an example

ed.printStackTrace();}

}});

tot.setVisible(true);
tot.pack();
}
public static void main(String[] args) {
//llamada al métdo de la clase
JTmet();

}

}



//---termina codigo----


Estoy realizando esta consulta SQL :

ResultSet rs = statement.executeQuery("SELECT Id FROM Datos where Id = '"+prub.getText()+"'");

donde Id es un campo autonumerico o númerico ODBC.
la idea es extraer registros individuales dese una base de datos via JDBC.

donde prub es un JTextField.

La pregunta es la siguiente.
¿Como hago para que la consulta me lea un entero (int) o numero desde ese objeto JTextField?

he probado:

0) convertir JtextField a String y luego a int ej :

String gb = txt_busq.getText();
int numero=Integer.parseInt(gb);

pero como obtengo el valor? de esta forma '"+txt_med.getText()+"' no,
porque es para texto .

1)haciendo un cast a int sobre sql


ResultSet rs = statement.executeQuery("SELECT Id,Medico_solic FROM Datos where Id = (cast)

'"+prub.getText()+"' ");

java.sql.SQLException: [Microsoft][Controlador ODBC Microsoft Access] Error de sintaxis (falta

operador) en la expresión de consulta 'Id = (int)'1''.

2)realizando un cast :

k = Integer.parseInt(prub.getText());

ResultSet rs = statement.executeQuery("SELECT Id,Medico_solic FROM Datos where Id = 'k'");

java.sql.SQLException: [Microsoft][Controlador ODBC Microsoft Access] No coinciden los tipos de

datos en la expresión de criterios.

sin apostorfes ('')

lanza:

java.sql.SQLException: [Microsoft][Controlador ODBC Microsoft Access] Pocos parámetros. Se

esperaba 1..

Muchas gracias.