简体   繁体   English

Java-sql结果未显示在jTable中

[英]java - sql results are not displaying in the jTable

I'm new to java. 我是Java新手。 I'm following a tutorial to retrieve images and other data from db to a jTable. 我正在按照教程从db到jTable检索图像和其他数据。 I have 4 columns in the db. 我在数据库中有4列。 This was supposed to display the available data in the db to jTable. 本来应该在到jTable的数据库中显示可用数据。 But nothing is happening. 但是什么也没发生。 Not even a error is showing. 甚至没有显示错误。 I have added all my code. 我已经添加了所有代码。 Any help would be appreciated ! 任何帮助,将不胜感激 !

Course.java Course.java

package my.welcomescreen;


public class Course {
    private int id;
    private String name;
    private byte[] imag;
    private String desc;

public Course(){}

public Course(int Id, String Name, byte[] image, String description){
    this.id = Id;
    this.name = Name;
    this.imag = image;
    this.desc = description;
}

public int getID(){
    return id;
}

public void setID(int ID){
    this.id = ID;
}

public String getName(){
    return name;
}

public void setName(String Name){
    this.name = Name;
}

public byte[] getImage(){
    return imag;
}

public String getDesc(){
    return desc;
}

public void setDesc(String Description){
    this.desc = Description;
}
}

MainQuery.java MainQuery.java

package my.welcomescreen;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainQuery {

    public ArrayList<Course> BindTable(){
    ArrayList<Course> list = new ArrayList<Course>();
    db databaseCon = new db();
    Connection dbconnect = db.dbconnect();
    Connection con = databaseCon.dbconnect();
    Statement st;
    ResultSet rs;

    try{
        st = con.createStatement();
        String sql = "select id,name,img,description from courses";
        rs = st.executeQuery(sql);

        Course c;
        while(rs.next()){
            c = new Course(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getBytes("img"),
                            rs.getString("description")
                           );

        }

    } catch (SQLException ex) {
        Logger.getLogger(Admin_Panel.class.getName()).log(Level.SEVERE, null, ex);
    }

    return list;

   }


}

TheModel.java TheModel.java

package my.welcomescreen;

import javax.swing.Icon;
import javax.swing.table.AbstractTableModel;


public class TheModel extends AbstractTableModel {

    private String[] columns;
    private Object[][] rows;

    public TheModel(){}

    public TheModel(Object[][] data, String[] columnName){
        this.rows = data;
        this.columns = columnName;
    }

    public Class getColumnClass(int Column){
        if(Column == 2){
            return Icon.class;
        } else {
            return getValueAt(0,Column).getClass();
        }
    }


    public int getRowCount() {
        return this.rows.length;
    }


    public int getColumnCount() {
        return this.columns.length;
    }


public Object getValueAt(int rowIndex, int columnIndex) {
     return this.rows[rowIndex][columnIndex]; 
}

public String getColumnName(int col){
    return this.columns[col];
}

}

Main Method 主要方法

public void displayJTable(){
    MainQuery mq = new MainQuery();
    ArrayList<Course> list = mq.BindTable();
    String[] columnName = {"Id","Course Name","Image","Description"};
    Object[][] rows = new Object[list.size()][3];
    for(int i = 0; i < list.size(); i++){
        rows[i][0] = list.get(i).getID();
        rows[i][1] = list.get(i).getName();

        if(list.get(i).getImage() != null){

         ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getImage()).getImage()
         .getScaledInstance(150, 120, Image.SCALE_SMOOTH) );   

        rows[i][2] = image;
        }

        rows[i][3] = list.get(i).getDesc();
    }

    TheModel model = new TheModel(rows, columnName);
    jTable1.setModel(model);
    jTable1.setRowHeight(120);
    jTable1.getColumnModel().getColumn(3).setPreferredWidth(150);


}

I don't see you adding every course to the list in the below snippet: 我看不到您将每个课程都添加到以下片段的列表中:

 Course c;
    while(rs.next()){
        c = new Course(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getBytes("img"),
                        rs.getString("description")
                       );
      // this line below should be added
      list.add(c)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM