[J2EE] Odczyt jednego rekordu z bazy

0

Witam,
Mam taką klasę

package com.pro.web;

import java.sql.*;
import java.util.*;



public class TableBean1 {

	Connection con ;
	Statement ps;
	ResultSet rs;
	
	
	
	
	private List perInfo = new ArrayList(); 

	public List getperInfo() {
		
		  try
		  {
			  
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/baza","root","root");
		     ps = con.createStatement();
			 rs = ps.executeQuery("select * from dane");
			/*while(rs.next())
			  {*/
				/*System.out.println(rs.Int(1));*/
				perInfo.add(new perInfo(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4)));
			

			 /* }*/
			  
		  }
		  catch (Exception e)
		  {
			  System.out.println("Error Data : " + e.getMessage());
		  }
	return perInfo;
	}


	public class perInfo {

	  int id;
	  String imie;
	  String nazwisko;
	  int wiek;
	 

	public perInfo(int id,String imie,String nazwisko,int wiek) {
	
	this.id = id;
	this.imie = imie;
	this.nazwisko = nazwisko;
	this.wiek = wiek;
	

	}

	

	public int getId() {
	return id;
	}

	public String getImie() {
	return imie;
	}
	
	public String getNazwisko() {
		return nazwisko;
		}
	
	public int getWiek() {
		return wiek;
		}
	
	

	}

	}

Jak zrobić aby móc wyświetlić na stronie jsp konkretny rekord z bazy. To jaki rekord ma zostać wyświetlony ma być parametrem podawanym na innej stronie jsp np. jest formularz z pytamiem "Proszę podać id" i użytkownik podaje jakiś numer i rekord o tym id jest wyświetlany w formularzy na innej stronie. Próbowałem robić tak, że parametr zadawałem w klasie rs = ps.executeQuery("select * from dane where id = 1"); i działało, ale teraz chcę żeby użytkownik miał możliwość wyboru numeru id na stronie. Z góry dzięki za wskazówki

0

Używaj PreparedStatement. Dzięki temu możesz wsadzić do polecenia jakąś konkretną dane.

W twoim wypadku mniej więcej coś takiego:

String selStr = "select * from dane where id = ?";
PreparedStatement select = conn.prepareStatement(selStr);
select.setInt(1, id);     //1 - kolejny znak ?, id - co chcesz wstawić
select.executeQuery();
conn.close();

id to jest jakaś liczba którą wyciągsz z jsp.

0

Zrobiłem tak

package com.pro.web;

import java.sql.*;
import java.util.*;



public class TableBean1 {

        Connection con ;
        Statement ps;
        ResultSet rs;
       
       
       
       
        private List perInfo = new ArrayList();

        public List getperInfo() {
               
                  try
                  {
                         
                        Class.forName("com.mysql.jdbc.Driver");
                        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/baza","root","root");
                               
						PreparedStatement select = con.prepareStatement("select * from dane where id = ?");
						select.setInt(1,5); 
						rs = select.executeQuery();


						perInfo.add(new perInfo(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4)));
                       

                        
                         
                  }
                  catch (Exception e)
                  {
                          System.out.println("Error Data : " + e.getMessage());
                  }
        return perInfo;
        }


        public class perInfo {

          int id;
          String imie;
          String nazwisko;
          int wiek;
         

        public perInfo(int id,String imie,String nazwisko,int wiek) {
       
        this.id = id;
        this.imie = imie;
        this.nazwisko = nazwisko;
        this.wiek = wiek;
       

        }

       

        public int getId() {
        return id;
        }

        public String getImie() {
        return imie;
        }
       
        public String getNazwisko() {
                return nazwisko;
                }
       
        public int getWiek() {
                return wiek;
                }
       
       

        }

        }

Tutaj select.setInt(1,5); wstawiłem na razie dla sprawdzenia jakąś z góry ustalona liczbę i nie chce mi to działać tzn. na jsp wyświetla mi tylko nagłówki tabeli natomiast nie wyświetla tego 5 rekordu. Pojawia się błąd INFO [STDOUT] Error Data : Before start of result set
Gdzie może być błąd?

0
paralaksas napisał(a):

Zrobiłem tak

package com.pro.web;

import java.sql.*;
import java.util.*;



public class TableBean1 {

        Connection con ;
        Statement ps;
        ResultSet rs;
       
       
       
       
        private List perInfo = new ArrayList();

        public List getperInfo() {
               
                  try
                  {
                         
                        Class.forName("com.mysql.jdbc.Driver");
                        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/baza","root","root");
                               
						PreparedStatement select = con.prepareStatement("select * from dane where id = ?");
						select.setInt(1,5); 
						rs = select.executeQuery();


						perInfo.add(new perInfo(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4)));
                       

                        
                         
                  }
                  catch (Exception e)
                  {
                          System.out.println("Error Data : " + e.getMessage());
                  }
        return perInfo;
        }


        public class perInfo {

          int id;
          String imie;
          String nazwisko;
          int wiek;
         

        public perInfo(int id,String imie,String nazwisko,int wiek) {
       
        this.id = id;
        this.imie = imie;
        this.nazwisko = nazwisko;
        this.wiek = wiek;
       

        }

       

        public int getId() {
        return id;
        }

        public String getImie() {
        return imie;
        }
       
        public String getNazwisko() {
                return nazwisko;
                }
       
        public int getWiek() {
                return wiek;
                }
       
       

        }

        }

Tutaj select.setInt(1,5); wstawiłem na razie dla sprawdzenia jakąś z góry ustalona liczbę i nie chce mi to działać tzn. na jsp wyświetla mi tylko nagłówki tabeli natomiast nie wyświetla tego 5 rekordu. Pojawia się błąd INFO [STDOUT] Error Data : Before start of result set
Gdzie może być błąd?

dodaj w petli while (rs.next())
jest to błąd następnego rekordu

1 użytkowników online, w tym zalogowanych: 0, gości: 1