How would you look for a log of a sql database from java

0

I have a list that is filled in with the fields that I entered and then saved in the database, the problem is that when I select the button see user I want you to show me only the user information that I have selected from the list but I do not know how to do it

if(e.getSource().equals(b3))
{
    manejar m=new manejar();
    m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    try {
        conexion=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=menu","sa","sa");

        statement=conexion.createStatement();
        //recorrer bd
        String pedir="SELECT DISTINCT nombre,apellido,edad FROM almacenar ";
        resulset=statement.executeQuery(pedir);
        while(resulset.next())
        {
            if((String) lista.getSelectedValue()==)//con que deberia compararlo para hacer la busqueda respectiva 
            {
            mostrar1.setText(resulset.getString(1));
            mostrar2.setText(resulset.getString(2));
            mostrar3.setText(""+resulset.getInt(3));
            }
        }
        resulset.close();
    }

     catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}
    
asked by vegetto 10.12.2017 в 00:53
source

1 answer

-1

First you must have the connector for the java database. Second load the driver and then perform the consultations

Here I give you an example to guide you

The driver

 public Connection getConexion() {
    Connection conexion = null;
     String url=null;
        try {


        Class.forName("com.mysql.jdbc.Driver");                        


 if(this.getDireccion().equalsIgnoreCase("localhost")){//se conecta en modo local
        url = "jdbc:mysql://localhost"; //personal
        conexion = DriverManager.getConnection(url,"root","clave");//conexion normal superusuario
      }else{//se conecta en modo remoto

        url="jdbc:mysql://"+this.getDireccion(); 
        conexion = DriverManager.getConnection(url,"usuario","clave");//conexion Servidor remoto


   }
        System.out.println("Conexion establecida con exito!");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();

    } catch (SQLException e) {

        e.printStackTrace();

        JOptionPane.showMessageDialog(
            new JFrame(),e.getLocalizedMessage()+
            "Base de Datos no encontrada",
            "ADVERTENCIA",
            JOptionPane.INFORMATION_MESSAGE);

        System.out.println(e.getLocalizedMessage()+"Base de Datos no encontrada");

}

    return conexion;
}

Inquiry

public void buscar_materias_alternativo(Connection con, String ci,String tabla) {
             this.getRecord().clear();//limpiando el LinkedList

System.out.println("TABLA: "+tabla+" - CEDULA: "+ci);


    this.setReconocimiento(0);

    Statement sentencia = null;
    ResultSet buscar = null;

    try {

        sentencia = con.createStatement();
        buscar = sentencia.executeQuery("SELECT * FROM control_de_estudio."+tabla+" WHERE CEDULA='"+ ci + "' ORDER BY TERMINO;");



        while(buscar.next()){

        //   System.out.println(buscar.getString("CEDULA"));
            //cargando todas las materias del alumno en memoria
             this.getRecord().add(buscar.getString("CODMAT"));   //0
             this.getRecord().add(buscar.getString("CODMAT"));  //1
             this.getRecord().add(String.valueOf(buscar.getInt("NOTDEF")));  //2
             this.getRecord().add(String.valueOf(buscar.getInt("NOTREP")));  //3
             this.getRecord().add(buscar.getString("CONDIC")); //4
             this.getRecord().add(buscar.getString("PERACA")); //5

             this.getRecord().add(String.valueOf(buscar.getInt("DEFREP")));  //6
             this.getRecord().add(String.valueOf(buscar.getInt("PORINA")));  //7
             this.getRecord().add(buscar.getString("CODESP")); //8

             this.getRecord().add(String.valueOf(buscar.getInt("NOTLAB"))); //9
             this.getRecord().add(String.valueOf(buscar.getInt("TERMINO"))); //10

        }

        sentencia.close();
        buscar.close();
        con.close();


    }catch (SQLException ex) {
            this.setReconocimiento(1);
            Logger.getLogger(registro_ingenieria.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("metodo buscar materia alternativo ");

                                       }

}
    
answered by 10.12.2017 в 15:01