2016-12-04 52 views
0

您好我正在尋找從MySQL數據庫填充以下字段的客戶端對象:
Id;ApellidoPaterno;ApellidoMaterno;Nombre; Telefono; and Username爲什麼不是ResultSet拿起我的ClientId字段? MySQL,Spring

以下是我用創建表的命令:

'Cliente', 
    'CREATE TABLE `cliente` (
    `ClientId` int(11) NOT NULL, 
    `ApellidoPaterno` varchar(255) NOT NULL, 
    `ApellidoMaterno` varchar(255) DEFAULT NULL, 
    `Nombre` varchar(255) DEFAULT NULL, 
    `Telefono` mediumtext, 
    `Username` varchar(255) NOT NULL, 
PRIMARY KEY (`ClientId`) 
) 
ENGINE=InnoDB DEFAULT CHARSET=utf8' 

而我的連接類:

package me.jmll.utm.config; 

import java.sql.*; 

public class Conexion{ 

public Connection getConexion(){ 
    Connection con = null;  
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection( 
     "jdbc:mysql://localhost:3306/miscelanialabety","root","Patito01"); 
     return con; 
    } 
    catch(Exception e){ 
     try {throw e;} 
    catch (Exception e1) { e1.printStackTrace();}} 
    return con; 
    } 
} 

而我getClientes()方法:

@Override 
     public List<Cliente> getClientes() { 
      String sql = "SELECT * FROM Cliente"; 

      try { 
       conexion = new Conexion().getConexion(); 
       PreparedStatement ps = conexion.prepareStatement(sql); 
       Cliente cliente = null; 
       List<Cliente> listaClientes = new ArrayList<Cliente>(); 
       ResultSet rs = ps.executeQuery(); 
       while (rs.next()) { 
        cliente = new Cliente(
         rs.getInt(1), 
         rs.getString("ApellidoPaterno"), 
         rs.getString("ApellidoMaterno"), 
         rs.getString("Nombre"), 
         rs.getLong("Telefono"), 
         rs.getString("Username") 
        ); 
        listaClientes.add(cliente); 
       } 
       rs.close(); 
       ps.close(); 
       return listaClientes; 
      } catch (SQLException e) { 
       throw new RuntimeException(e); 
      } finally { 
       if (conexion != null) { 
        try { conexion.close();} 
        catch (SQLException e) {} 
       } 
      } 
     } 

在線路rs.getInt(1),我試過rs.getInt("ClientId"),無濟於事,我試過指定exac t字段在查詢中返回。

結果列表包含從db請求的所有字段,「ClientId」除外,這是最重要的!我哪裏錯了?

編輯發表評論:上表中的數據如下:

ID AppelidoPaterno AppelidoMaterno Telefono Username 

1 Osorio Marquez Rafael 2227355623 RafaelOsorioMarquez 
2 Hernandez Carrillo Rodrigo 2227128907 RodrigoHernandezCarrillo 
3 Fernandez Hernandez Juan 2226770934 JuanFernandezHernandez 
4 Gomez Trujillo Diego 2220200435 DiegoGomezTrujillo 
5 Figueroa Lopez Roberto 2221106286 RobertoFigueroaLopez 
6 Almeyda Martinez Luis 2224376232 LuisAlmeydaMartinez 
7 Lozano Jimenez Cesar 2222338905 CesarLozanoJimenez 
8 Reyes Franco Julio 2225205878 JulioReyesFranco 
9 Peralta Perez Carlos 2224783388 CarlosPeraltaPerez 
15 Osorio Carrillo Gabriela 9981159756 GabrielaOsorioCarrillo 

EDIT2:Cliente類:

package me.jmll.utm.model; 

public class Cliente 
{ 
    int Id; 
    String ApellidoPaterno; 
    String ApellidoMaterno; 
    String Nombre; 
    long Telefono; 
    String Username; 
    //getter and setter methods 

    public Cliente(int Id, String ApellidoPaterno,String ApellidoMaterno, 
         String Nombre,long Telefono) { 
     this.ApellidoPaterno = ApellidoPaterno; 
     this.ApellidoMaterno = ApellidoMaterno; 
     this.Nombre = Nombre; 
     this.Telefono = Telefono; 
    } 

    public Cliente(int Id, String ApellidoPaterno,String ApellidoMaterno, 
      String Nombre,long Telefono, String Username) { 
      this.ApellidoPaterno = ApellidoPaterno; 
      this.ApellidoMaterno = ApellidoMaterno; 
      this.Nombre = Nombre; 
      this.Telefono = Telefono; 
      this.Username = Username; 
} 

    public Cliente() { 
     // TODO Auto-generated constructor stub 
    } 

    public void setId(int id) { 
     this.Id = id; 
    } 

    public int getId() { 
     return Id; 
    } 

    public void setApellidoPaterno(String apellidoPaterno) { 
     this.ApellidoPaterno = apellidoPaterno; 
    } 

    public String getApellidoPaterno() { 
     return ApellidoPaterno; 
    } 

    public void setApellidoMaterno(String apellidoMaterno) { 
     this.ApellidoMaterno = apellidoMaterno; 
    } 

    public String getApellidoMaterno() { 
     return ApellidoMaterno; 
    } 

    public void setNombre(String nombre) { 
     this.Nombre = nombre; 
    } 

    public String getNombre() { 
     return Nombre; 
    } 

    public void setTelefono(long telefono) { 
     this.Telefono = telefono; 
    } 

    public long getTelefono() { 
     return Telefono; 
    } 

    public String getUsername() { 
     return Username; 
    } 

    public void setUsername(String username) { 
     this.Username = username; 
    } 
} 
+0

你可以發佈數據庫中的最小數據嗎?數據是否存在於數據庫中? – developer

+0

嗨,是的,數據存在於數據庫中,並且我在編輯中包含了列。這麼晚纔回復很抱歉! – PatoPan

+0

一切似乎沒問題,添加'Cliente'類的代碼? – developer

回答

1

在你Cliente類的構造函數,你已經錯過了設置id場,因此將其設置爲this.id =id,如下面的代碼所示:

public class Cliente { 

     public Cliente(int Id, String ApellidoPaterno,String ApellidoMaterno, 
       String Nombre,long Telefono) { 
      this.Id = Id; //this is missed out 
      //set other fields as is 
     } 

     public Cliente(int Id, String ApellidoPaterno,String ApellidoMaterno, 
       String Nombre,long Telefono, String Username) { 
      this.Id = Id; //this is missed out 
      //set other fields as is 
     } 

     //add other code 
    } 
相關問題